Results 1 to 2 of 2

Thread: BindUtils.bind two objects - is it possible?

  1. #1
    Join Date
    Jul 2005
    Posts
    1

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    1,905

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •