Announcement Announcement Module
No announcement yet.
FormController's and Request/Session Attributes Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • FormController's and Request/Session Attributes


    In my FormController class (subclass of SimpleFormController) I need to
    have access to session attributes of the request in the body of onSubmit().
    I haven't been able to determine how to get to these. Any ideas?

    I tried overriding the processFormSubmission() method just to capture
    and save the HttpRequest object into a data member, but... I'm worried that
    the FormController is instantiated once and shared between all requests
    rather than being created per-request (thus saving the request object
    as I've done it here is useless for all but a single-user system!). Is this
    fear justified?

    If anyone know how FormControllers are created (singleton/shared, or per-request),
    and even better...can recommend a good way to gain access to the request
    from within onSubmit(), your thoughts would be very helpful!


  • #2
    An overloaded onSubmit() method taking the HttpServletRequest is also available ( ttp.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)), so using that, you should be able to get the session.

    About the controllers being instantiated only once, yes that's correct. There is a way to avoid that however. You have to set the singleton attribute of the controller bean(s) to false and use the appropriate HandlerMapping. SimpleUrlHandlerMapping for example has two ways to set the urlMap, via Properties (url patterns mapped to bean names) and using a Map (url patterns mapped to bean references). If you have set your singleton attribute to false, use the Properties way of setting the url mapping (bean references are only set once). I think the BeanNameUrlHandlerMapping is also okay.



    • #3

      I don't know how I missed the other flavor of onSubmit()! Obviously not enough caffine.

      Thanks for the info and pointing me in the right direction!