Announcement Announcement Module
Collapse
No announcement yet.
Form: Accessing backing object graph Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form: Accessing backing object graph

    Hi,

    when returning an backing object in form-controller i cannot access the nested properties (stack trace telling me that String does not have property 'value'):
    PHP Code:
    class MyBean 
       List<
    Attributeattributes
       
    //getter/setter
    }

    class 
    Attribute{
       
    String value;
       
    //getter/setter
    }

    class 
    CreationController implements SimpleFormController{
      @
    Override
        
    protected Object formBackingObject(HttpServletRequest requestthrows Exception {
            
    MyBean defaultBean = new MyBean();
            
    defaultBean.setAttributes(createListOfAttributes());
            return 
    defaultBean;
        }
    ...


    When now trying to access the attribute value in the corresponding form-jsp it fails:
    PHP Code:
    <form:form method="post">


        
    Ad-name: <form:input path="adId"/><br/>

        <
    c:forEach items="attributes" var="attribute">
            ${
    attribute.value}:
        </
    c:forEach>
    .... 
    Above fails, because it sees the attribute property as a String though inside the object graph it is a custom type.

    So how can I access the object graph inside a form? It seems that inside the form-'mode' I need a special access directive to access these...


    (I am sure this is easy, but didn't find anything on that). thanks

  • #2
    I found the way. It is the same as setting a form value. You have to use the spring:bind tag (though I find this quite verbose and difficult to read markup...).

    Anyway here the solution code:
    PHP Code:
    <form:form method="post">
    ...
        <
    c:forEach items="attributes" var="attribute" varStatus="loopStatus">
             <
    spring:bind path="attributes[${loopStatus.index}].name">
                  ${
    status.value}
             </
    spring:bind>
        </
    c:forEach>
    .... 

    Comment


    • #3
      Regarding this, why do you have to enter the loop-index instead of accessig the temp-var value directly? Not very intuitive to read or write.

      To speak in code, why can't I replace
      PHP Code:
      ...
      <
      spring:bind path="attributes[${loopStatus.index}].name">
      ... 
      with
      PHP Code:
      ...
      <
      spring:bind path="attribute.name">
      ... 

      Comment


      • #4
        Hi Manuel,
        Try using
        Code:
        <c:out value="${attributes.name}"></c:out>
        I believe you have created a similar thread few hours back.I've posted a reply there.Please go through my post and see if it helps.
        Cheers,
        Sushant

        Comment

        Working...
        X