Announcement Announcement Module
Collapse
No announcement yet.
@ModelAttribute giving error on POST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @ModelAttribute giving error on POST

    Hi all,

    I am using spring 2.5 and I have a controller defined like:

    Code:
    @Controller
    @SessionAttributes("bookingForm")
    @RequestMapping("/home.htm")
    public class HomePageController {
    with two methods:

    Code:
    @RequestMapping(method = RequestMethod.GET)
    protected ModelAndView referenceData(HttpSession session) throws Exception {
    
    ModelAndView mav = new ModelAndView();
            BookingForm bookingForm = (BookingForm) session.getAttribute("bookingForm");
            if (bookingForm == null) {
                bookingForm = new BookingForm();  
            }
            ModelAndView mav = new ModelAndView();
            mav.addObject("bookingForm", bookingForm);
            return mav;
    and

    Code:
    @RequestMapping(method = RequestMethod.POST)
    protected ModelAndView onSubmit(@ModelAttribute( "bookingForm" ) BookingForm bookingForm, BindingResult results, HttpSession session) throws Exception {
    The problem seems is due the bookingForm object.

    The method that is answering GET is checking if the bookingForm object is already in session [e.g. page reload] and if not create a new one. Then I when I POST the page all is fine, the bookingForm object is present in the session and it can be checked.

    The problem arise when I try to POST a page without coming from the GET first [I am obtaining this by restarting the server and reloading the POSTed page in the browser]. At this point the session will not contain the bookingForm object and I would just reroute to the GET request.

    When I try to POST the page [without the GET phase first] I have the exception
    Code:
    org.springframework.web.HttpSessionRequiredException: Session attribute 'bookingForm' required - not found in session
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseSessionRequiredException
    The POST method is failing upon invocation, no lines are executed. How would you implement a system to go back to the GET method ?
    Last edited by dawez; Aug 21st, 2011, 02:40 PM. Reason: more details.

  • #2
    Hello

    Why your methods are protected? change to public

    The problem arise when I try to POST a page without coming from the GET first [I am obtaining this by restarting the server and reloading the POSTed page in the browser]. At this point the session will not contain the bookingForm object and I would just reroute to the GET request.
    According with my knowledge when the server is stopped or re started all the Session values are gone, deleted, this sessions values are persisted in the RAM if my memory doesnt fail me.

    Therefore you must load first the GET invocation and then the POST

    Add some logger line for each method and see really if when you call directly or load the POST invocation (after to re start the server) it call the GET invocation

    Comment


    • #3
      Instead of creating a method responding to the get also filling the model object I would create a method annotated with @ModelAttribute to populate the model. In this you could check if it is a get/post request and check if an attribute is already available in the model. Which method parameters you can use is stated in the reference guide.

      I want to add that the code of your method responding to get is flawed... The variable isn't stored in the attribute 'bookingForm' in the session. Spring uses another name to store the object in the session, so basically your method overrides the form each time it is executed.

      Comment


      • #4
        I forgot to include in my previous code that I am actually storing the bookingForm into a session while going from the POST of the homepage controller to the next page:

        Code:
        session.setAttribute("bookingForm", bookingForm);
        return new ModelAndView(resultView, "bookingForm", bookingForm);
        so then the bookingForm is available in the HTTP session of next pages.

        I followed your advice and not I have another method that is :

        Code:
        @ModelAttribute("bookingForm")
        protected BookingForm populateBookingForm(HttpSession session) {
            BookingForm bookingForm = (BookingForm) session.getAttribute("bookingForm");
            if (bookingForm == null) {
                bookingForm = new BookingForm();
            }
            return bookingForm;
        }
        It is like described: http://static.springsource.org/sprin...requestmapping . And this way is correctly working.
        Last edited by dawez; Jan 9th, 2012, 01:43 PM. Reason: spelling mistake

        Comment

        Working...
        X