Announcement Announcement Module
Collapse
No announcement yet.
Binding entry fields to object which is property of command object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding entry fields to object which is property of command object

    If the command object has a property type of any custom class then on the JSP page we can access the properties of that object using regular JSTL expressions, namely

    Command bean is classA, has "objB" property type of classB.
    Command bean is set w/ name "formA" in the controller.
    ClassB has properties "prop1"type of String and "prop2" type of int.
    We can access the properties of classB on the web page using
    <c:out value="formA.objB.prop1" /> and <c:out value="formA.objB.prop2" />.

    Is there any way to bind web page entry fields directly to "prop1" and "prop2" properties of "objB" ( being the property of command bean) ?

    Can you provide the code fragment explaining this use case ?

    Thanks,
    Len.

  • #2
    Hi,
    If the command object has a property type of any custom class then on the JSP page we can access the properties of that object using regular JSTL expressions, namely

    Command bean is classA, has "objB" property type of classB.
    Command bean is set w/ name "formA" in the controller.
    ClassB has properties "prop1"type of String and "prop2" type of int.
    We can access the properties of classB on the web page using
    <c: out value="formA.objB.prop1" /> and <c :out value="formA.objB.prop2" />.
    The value is already bound and hence you are able to access the value in the c:out tag.
    Is there any way to bind web page entry fields directly to "prop1" and "prop2" properties of "objB" ( being the property of command bean) ?
    Code:
    <form:form commandName="formA">
    <form:input path="objB.prop1"/>
    <form:input path="objB.prop2"/>
    <!-- any text entered by the user in the text firleds gets automatically bounded to the command bean's property ,spring automatically binds this into you commandbean -->
    if you require only the data to be presented to the user in a non editable / as just a text use C:out as you have done, since c: out tag is not aware of the form bean or the command bean you would have to give the full path
    formA.objB.prop1 to access its value.

    Comment


    • #3
      Sami,

      I will check out how these tags work. At the same time I have another question. I usually specified name of "command" bean in the form controller constructor using setCommandName() method. I noticed in your code that you specified name of command bean via the "commandName" attribute of form tag. Does it mean that I will not need to do this in constructor in this case ?


      Also can you comment on my questions that I posted in "StringArrayPropertyEditor usage" thread, which I opened on the same day and unfortunately nobody answered.

      Thanks,
      Len.

      Comment

      Working...
      X