@SessionAttributes 给出 HttpSessionRequiredException
我正在使用 Spring MVC 3.0.5。我正在尝试学习如何使用@SessionAttributes
。这是我的代码示例:
@Controller
@SessionAttributes("book")
public class BookController {
@RequestMapping("/book/bookForm.htm")
public ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, @Valid @ModelAttribute("book") Book book, BindingResult result) throws Exception {
ModelMap modelMap = new ModelMap();
return new ModelAndView("bookForm", modelMap);
}
}
当我尝试转到 /book/bookForm.htm 时,我收到异常:
org.springframework.web.HttpSessionRequiredException: Session attribute 'book' required - not found in session
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:722)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:758)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
如何解决此问题?
我也尝试遵循此答案:
有关该答案的一些问题:
提问者的代码和所选择的答案的代码看起来本质上是相同的...那么解决问题的添加在哪里?
尝试阅读文档,我无法理解这个注释实际上做了什么:
相关命令对象何时保存在会话上?是当我们进入控制器的方法时,还是当我们离开它时,或者每次我们操作命令对象的内容时......?命令对象什么时候开始保存在会话中?
谢谢!
I am using Spring MVC 3.0.5. I am trying to learn how to use @SessionAttributes
. Here is a sample of my code:
@Controller
@SessionAttributes("book")
public class BookController {
@RequestMapping("/book/bookForm.htm")
public ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, @Valid @ModelAttribute("book") Book book, BindingResult result) throws Exception {
ModelMap modelMap = new ModelMap();
return new ModelAndView("bookForm", modelMap);
}
}
When I try go to /book/bookForm.htm I am getting the exception:
org.springframework.web.HttpSessionRequiredException: Session attribute 'book' required - not found in session
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException(AnnotationMethodHandlerAdapter.java:722)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:758)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
How could I solve this problem?
I also have tried following this answer:
Spring Framework 3 and session attributes
Some questions regarding that answer:
The asker's code and the chosen answer's code seem essentially the same... so where is the addition that solved the problem?
Trying to read the documentation, I cannot understand what this annotation actually does:
When is the relevant command object being saved on the session? Is it when we enter the controller's method, or when we leave it, or each time we manipulate the content of the command object....? When does the command object start being saved on the session?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上,使用 Spring 注释选择的“默认方法”是您第一次调用页面时参数最少的方法。方法越具体,成为默认方法的可能性就越小。
如果您将 @RequestMapping("/book/bookForm.htm") 放在这样的方法上:
这将是默认值,因为它的参数数量最少。您必须在第一次请求页面时创建表单并将其放入会话/模型和视图/模型地图/厨房水槽中(说真的,RequestMapping 参数组合既很棒又荒谬)。之后,将根据您现在实际存储了“book”会话属性(因为它被放置在“book”键下)这一事实,适当地调用各个方法。
参数名称和值等都决定了在默认之后调用哪个方法 - 这是配置控制器响应 Web 请求的一种非常好的和灵活的方法,但它确实需要一些时间来适应。
Basically, the "default method" that's chosen with the Spring annotations is the one that has the least arguments when you invoke your page for the first time. The more specific the method, the less likely it will be the default one.
If you take your @RequestMapping("/book/bookForm.htm") and place it on a method like this:
This will be the default because it has the least amount of arguments. You have to create your form and place it into the session/model and view/model map/kitchen sink (seriously, the RequestMapping parameter combinations are both awesome and ridiculous) on the first request to the page. After that, the individual methods will be called as appropriate based on the fact that you now actually have the "book" session attribute stored (because it was placed under the "book" key).
Parameter names and values, etc, all determine which method is invoked after the default - it's a really nice and flexible way of configuring a controller to respond to web requests, but it does take some getting used to.
在控制器类中添加这些代码
行在使用 SessuinAttributes 时,您需要使用上述代码在会话中添加对象
Add these code lines in the controller class
While Using SessuinAttributes , U need to Add object in session by using above mentioned code