开发者

@SessionAttributes gives HttpSessionRequiredException

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.AnnotationMethodHandle开发者_开发技巧rAdapter$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:

  1. The asker's code and the chosen answer's code seem essentially the same... so where is the addition that solved the problem?

  2. 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!


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:

@RequestMapping("/book/bookForm.htm")
public ModelAndView setupForm() throws Exception {

    ModelMap modelMap = new ModelMap();
    // I don't remember the exact syntax here - pretending its a java.util.Map.
    modelMap.put("book", new Book());

    return new ModelAndView("bookForm", modelMap);
}

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.


Add these code lines in the controller class

@ModelAttribute("book")
    public UserObject getBookObject() {
        return new Book();

    }

While Using SessuinAttributes , U need to Add object in session by using above mentioned code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜