Announcement Announcement Module
Collapse
No announcement yet.
Using data in Session for form binding Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using data in Session for form binding

    Hi,

    I have a map of reference data stored in my session where the key is the value which is submitted in a form, and the value is the object which needs to be put onto the backing object for the form.

    What I need to be able to do is get Spring binding to look up the object for the key which has been submitted. In other words, what I think I need to do is have some way for the session to be passed into the PropertyEditor, then I can write the code to handle the look up.

    Is there a way of doing this, or is there a better way to handle this situation?

    Cheers,
    Jacqui

  • #2
    A simple mechanism I have used in the past is to include a HttpSession declaration in my annotated controller method signature.

    You might end up with something like:

    Code:
    @RequestMapping("/myurl")
    public void myMethod(@ModelAttribute MyForm myForm, HttpSession session, Model model) {
    
    String sessionAttributeName = myForm.getSessionAttributeName();
    
    MyAttribute myAttribute = session.getAttribute(sessionAttributeName);
    
    model.put(myAttribute);
    
    }
    This assumes your form backing class is MyForm, and you are retrieving a session variable of type MyAttribute.

    Comment

    Working...
    X