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"));
    ... }
user706071
  • 805
  • 3
  • 10
  • 25
  • 1
    [stackoverflow has been resolved this problem ](https://stackoverflow.com/questions/15032063/spring-mvc-bind-request-attribute-to-controller-method-parameter/15035677#15035677) – chenyueling Mar 09 '16 at 12:46

1 Answers1

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"));
    ... }
}
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125