Announcement Announcement Module
Collapse
No announcement yet.
BindUtils.bind two objects - is it possible? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • BindUtils.bind two objects - is it possible?

    Hi,

    I am trying to bind two objects (instances of same class) to two input boxes. For instance I have classA:
    ClassA {
    String name;
    }

    In controller class, method handleRequest I have following code:
    ModelAndView model = new ModelAndView();
    ClassA obj1 = new ClassA();
    ClassA obj2 = new ClassA();
    ....
    BindUtils.bind(request, obj1, "object1");
    BindUtils.bind(request, obj2, "object2");
    ....
    model.addObject("object1", obj1);
    model.addObject("object2", obj2);

    On Jsp page I have something like:
    <spring:bind path="object1.name">
    <input type="text" name="${status.expression}" value="${status.value}" />
    </spring:bind>

    <spring:bind path="object2.name">
    <input type="text" name="${status.expression}" value="${status.value}" />
    </spring:bind>


    Now, when I fill value to one input and submit this JSP page, then name property of objects obj1/obj2 has same value. This is because rendered html is something like:
    <html>
    ...
    <input type="text" name="name" value="" />
    <input type="text" name="name" value="" />
    ..
    </html>

    As you can see, I have two inputs with same name, so that is explanation why I have same name property in both cases.

    Where I am making mistake? I expected in html something like:
    <input type="text" name="object1.name" value="" />
    <input type="text" name="object2.name" value="" />

    Then object1.name will be unique on page.

    I have tried to manualy create input with objectName prefix, something like:
    <spring:bind path="object1.name">
    <input type="text" name="object1.${status.expression}" value="${status.value}" />
    </spring:bind>

    <spring:bind path="object2.name">
    <input type="text" name="object2.${status.expression}" value="${status.value}" />
    </spring:bind>

    But in that case BindUtils.bind(request, obj1, "object1"); doesn't give me expected result.

    Is there any other way how to do this?
    I know I can create wrapper class (which will aggregate obj1/.obj2) and create commandObject (using SimpleFormController), but is there any other way?

    Regards

  • #2
    Check out http://forum.springframework.org/sho...ght=checkboxes

    Comment

    Working...
    X