在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"));
... }
答案 0 :(得分: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"));
... }
}