0

In Spring MVC, I can wire session with my method. That's OK.

@RequestMapping(value = "/{cid}/read")
public @ResponseBody
boolean markAsRead(@PathVariable("cid") Comment comment, HttpSession session) {
    User user = ((User) session.getAttribute("user"));
    ... }

Can I wire above user definition to the method definition? I mean instead of wiring session

@RequestMapping(value = "/{cid}/read")
public @ResponseBody
boolean markAsRead(@PathVariable("cid") Comment comment, User user) {
    //No need to inject HttpSession and 
    //no need to call user = ((User) session.getAttribute("user"));
    ... }
1

1 Answer 1

0

You should be able to retrieve it using the @ModelAttribute tag, and annotating the user as a session attribute, this way:

@SessionAttributes("user")
public class MyController {
    @RequestMapping(value = "/{cid}/read")
    public @ResponseBody
    boolean markAsRead(@PathVariable("cid") Comment comment, @ModelAttribute("user") User user) {
        //No need to inject HttpSession and 
        //no need to call user = ((User) session.getAttribute("user"));
    ... }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.