Announcement Announcement Module
Collapse
No announcement yet.
Spring Binding Issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Binding Issue

    Hi,

    I am buidling a demo app, and have a fairly simple situation:

    Code:
    class Applicant
    {
       String name;
       String address;
       Gender gender;
    }
    
    class Gender
    {
       String code;
       String description;
    }
    The applicant edit screen requires a dropdown box, showing the possible values of Gender. I have made this available via the referenceData section of my Controller.

    When I initially tried to do a simple binding:

    Code:
    <spring&#58;bind path="applicant.gender">
                <select name="<c&#58;out value="$&#123;status.expression&#125;"/>">
                    <c&#58;forEach items="$&#123;genders&#125;" var="g">
                        <option
                            value="<c&#58;out value="$&#123;g.code&#125;"/>"
                            <c&#58;if test="$&#123;status.value == g.code&#125;"> selected</c&#58;if> >
                            <c&#58;out value="$&#123;g.description&#125;"/>
                            </option>
                    </c&#58;forEach>
                </select>
                    <%--<input type="text" name="<c&#58;out value="$&#123;status.expression&#125;"/>" id="<c&#58;out value="$&#123;status.expression&#125;"/>"
                        value="<c&#58;out value="$&#123;status.value&#125;"/>" />--%>
                    <span class="fieldError"><c&#58;out value="$&#123;status.errorMessage&#125;"/></span>
                </spring&#58;bind>
    I was presented with the error:

    Failed to convert property value of type [java.lang.String] to required type [org.appfuse.model.Gender] for property 'gender'
    After reading up further in the documentation, I discovered the spring:transform tag, I added this, and still was greeted with the same problem.

    Next, I decided to change the binding to applicant.code.gender. This worked initially, but after changing the element twice, I was presented with the Hibernate error:

    identifier of an instance of org.appfuse.model.Gender altered from Female to Male
    This meant, that the code was simply setting the gender.setCode() method, when it required to insert the new Gender obect.

    The only solution I have found so far, is to change the select attributes name to genderCode, and then in override the onBind method, to do the following:

    Code:
    Applicant applicant = &#40;Applicant&#41; command;
            String genderName = request.getParameter&#40;"genderName"&#41;;
            Gender gender = genderManager.getGender&#40;genderName&#41;;
            applicant.setGender&#40;gender&#41;;
    This currently works, however as you can see is not the most elegant solution. Especially, as I will have to eventually write pages with multiple select fields, I don't want to have to re-write this code every time.

    Is the spring:transform method suppose to do what I am having trouble with, or is there an alternative more elegant way?

    Thanks in advance,
    Pete

  • #2
    Did you try registering a custom PropertyEditor?

    Comment

    Working...
    X