I have problems with getting my JSP view right. What I intend to do is to send a List that contains questions and each question object is a text field and a List with alternatives.

My intention is to be able to edit multiple questions (both to be able to edit the text/name of the question and edit the containing alternatives).

My backing object is now sending an List question.

Here is my JSP which are failing with invalid property of bean class.

        <form:form commandName="question">
        <form:errors path="*">
            <fieldset class="stdframe">
                <legend>Question</legend>
            </fieldset>
        </form:errors>

        <div class="stdframe">
            <c:forEach var="q" items = "${question}" varStatus = "s">           
                <p><b>Question:</b></p>
                <p><form:input size="67" path="${q.text}"/></p>
                <br/>
                ${q.text}
                <ul>
                    <c:forEach var="alternative" items = "${q.alternatives}" varStatus = "t">
                        ${alternative.text}
                        <li><form:input path = "${alternative[$t.index].text}" /></li>
                    </c:forEach>
                </ul>
                <br/>
            </c:forEach>
                <input type="submit" class="submit" value="Save" />
                <input type="button" class="button" onClick="back()" value="Back"/>
        </div>
</form:form>

I have tried both ${q.text} and ${q[$s.index].text}. When I just print ${q.text} it shows the correct text for the question object. Same goes for alternative.

What can I do to correctly bind the form to the objects?

In addition when I store an object which contains a list of other object, will the list be stored itself in the database?

有帮助吗?

解决方案

You may need to wrap your List in a simple object with the List as a field:

class MyListWrapper { List questions; } // etc.

If you use that as your command/form object, you should be able to do something like this in the JSP:

<form:form commandName="wrapper">
// ... 
        <c:forEach var="q" items = "${wrapper.questions}" varStatus = "s">
           <p><b>Question:</b></p>
            <p><form:input size="67" path="questions[${s.index}].text"/></p>
// ... 
               <c:forEach var="alternative" items = "${q.alternatives}" varStatus = "t">
                    ${alternative.text}
                    <li><form:input path = "questions[${s.index}].alternatives[${t.index}].text" /></li>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top