0

在 Spring MVC 中,我可以使用我的方法连接会话。没关系。

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

我可以将上面的用户定义连接到方法定义吗?我的意思是而不是接线会话

@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"));
    ... }
4

1 回答 1

0

您应该能够使用@ModelAttribute标签检索它,并将用户注释为会话属性,这种方式:

@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"));
    ... }
}
于 2013-07-15T15:22:39.943 回答