Binding multiple objects in a Spring form - spring

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>

Related

How to get values for dynamic form field names using JSTL and EL

I have 8 checkboxes and 1 button, when the user check any of the checkboxes and click the button, I want to check if any of the checkbox is checked and display it in another .jsp
I have already referred to few similar questions with no luck so far. So i tried to manage with my own logic
First.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<input type="checkbox" id="seat" name="seat${loop.index}" value="seat${loop.index}" >
<label for="seat">Seat${loop.index}</label>
</c:forEach> <br> <br>
<input type="submit" value="Save" name="savebtn">
Second.jsp
<c:forEach begin="1" end="8" varStatus="loop">
<c:if test="${not empty param.seat[loop.index]}">
<c:out value="${param.seat1} is booked"/>
</c:of>
</c:forEach>
I have 2 problems regarding the code above :
i can't get loop.index value inside the param $param.seat[loop.index] doesn't work
And even if i try to do it manually, i can only get value from seat1. I can't get value from the rest ( seat2, seat3, etc).
${param.seat[loop.index]} implies that seat is a collection, which it is not (it probably does not even exist). You are after ${param.seatX}, where you can dynamically set X. You can do that by creating a variable containing the parameter name first:
<c:set var="seatVarName" value="seat${loop.index}"/>
Now you can use this variable to get the parameter value from the implicit EL object:
${param[seatVarName]}
See also:
JSP expression language and dynamic attribute names

Assigning unique path name in spring webflow while using for each loop

I was using spring textarea to retrieve list of item. The problem doing so is I am getting commas due to repeatition of the path name. How can I make the name of the path name unique? As per my understanding the name of the path should match the variable in the modal.
<c:forEach var="note" items="${model.modelname}">
//create a click button for each items
//each click opens up a modal and
<div id="modal" class="hidden">
<div class="modal-body-content">
<form:textarea path="textPath" />
</div>
</div>
</c:forEach>
I tried using specifying index <form:textarea path="note[0].textPath" /> but I could not do anything to list the whole item.
I tried giving index to the for each loop and assign unique name to the path as shown below but this just displays the first item on the list
<c:forEach var="note" items="${model.modelname}" varStatus="status">
//create a click button for each items
//each click opens up a modal and
<div id="modal" class="hidden">
<div class="modal-body-content">
<form:textarea path="textPath[${status.index}]" />
</div>
</div>
</c:forEach>

JSTL: html snippet depending on property being null or not. (with not empty)

<c:forEach items="${list }" var="element">
<c:if test='${not empty element.radioelement}'>
<input type="radio" name="${element.name }" id="${element.id }" >
<label for="${element.id }"></label>
</c:if>
<c:if test='${not empty element.divelement}'>
<div> </div>
</c:if>
<c:if test='${not empty element.breakelement}'>
<br />
</c:if>
</c:forEach>
This is the jstl code I'm trying to find out if it works.
The list is filled with the objects of the class "tyle" which I made myself. These contain the following properties:
private String radioelement, divelement, breakelement, name, id;
I made sure they all have setters and getters.
I'm trying to make it work so that depending on which property isn't null, that piece of html will be written. (a radiobutton incase the string radioelement isn't null and so forth untill the list ends)
I'm wondering if this will work this way cus if it does I'll probably have a logical fault in my code. As it is now it only registers the breaks and makes any divelement also into a radio.

Form Submit using a Javascript to invoke webflow transition, doesn't take the updated value on form

I am trying to invoke a form submit using javascript (jquery) to invoke a webflow transition. It works and the submit invokes the desired transition. But, the updated radio button values is not reflected on the model object which is posted.
Here is the code:
<form:form method="post" action="#" commandName="infoModel" name="pageForm">
<form:input type="input" path="testMsg" id="success" />
<input type="button" id="clearSelections" value="Clear Selections">
<div class="question">
<h4><c:out value="${infoModel.questionInfo.description}"/> </h4>
<form:radiobuttons path="infoModel.answerId"
itemValue="answerId" itemLabel="answerDescription" items="${infoModel.answers}" delimiter="<br/>" />
</div>
<input type="submit" name="_eventId_saveQualitativeInput" value="Save" id="save" />
$(document).ready(function() {
$('#tabs').tabs();
//Clear selections (copy is server-side)
$('#clearSelections').click(function() {
//$('input[type="radio"]').prop('checked', false);
$('input[type="radio"]').removeAttr('checked');
$('#save').trigger('click');
});
});
</form:form>
The form:radiobutton, generates the below html:
<div class="question">
<h4>Is this a general obligation of the entity representing a full faith and credit pledge? </h4>
<span>
<input type="radio" checked="checked" value="273" name="infoModel.answerId" id="infoModel.answerId1">
<label for="infoModel.answerId1">Yes</label>
</span>
<span><br>
<input type="radio" value="274" name="infoModel.answerId" id="infoModel.answerId2">
<label for="infoModel.answerId2">No</label>
</span>
<br>
<span class="error"></span>
</div>
The input id= "success" value is registered and when the control goes to the server, the value of input id= "success" is updated in the "infoModel" object. But the value of answerId is not updated on the "infoModel" object.
Thoughts if i am missing something in the form:radiobutton element or if there is something else wrong?
Thanks in advance!
EDIT:::::::
Thanks mico! that makes sense. I stripped of some of the code first time to make it precise, but i have a list which is being used for building the radio-buttons, below is the code:
<c:forEach items="${infoModel.list["index"]}" var="qa" varStatus="rowCount">
<div class="question">
<h4><c:out value="${question.questionInfo.description}"/> </h4>
<form:radiobuttons path="list["index"][${rowCount.index}].answerId" itemValue="answerId" itemLabel="answerDescription" items="${question.answers}" delimiter="<br/>" />
<br>
</div>
</c:forEach>
Could you please suggest how i could try this one out?
NOTE: The same code works on a regular form submit on click of a button of type submit. Its the javascript form submit which is not working. I also tried to do whatever i want to do in javascript and then invoke the button.trigger('click'); form got submitted but the changes made on form in my javascript didnt reflect.
With commandName inside a form:form tag you set "Name of the model attribute under which the form object is exposed" (see Spring Documentation). Then in path you should tell the continuation of the path inside the model attribute.
With this said I would only drop the extra word infoModel from path="infoModel.answerId" and have it rewritten as path="answerId" there under the form:radiobutton.

How to edit a list of child entities in Spring 3 MVC

How would I edit an entity with child rows in Spring 3 MVC?
I'd like a form like:
<forms:form>
<p>Parent name <forms:input path="model.name" type="text" /></p>
<p>Children:
<ul>
<s:foreach in="${model.children}" var="${child} varStatus="row">
<li>
name: <forms:input path="model.children[${row.index}].name" />
<button name="?">delete</button>
</li>
</s:foreach>
</ul>
</p>
<p><button name="?">add child</button></p>
</forms:form>
I'm having a lot of trouble getting this to work in Spring 3.
I would love to be able to:
edit child properties inline on the parent's form, with validation etc.
delete children inline on the parent's form
add children inline on the parent's form
Have you checked out jqGrid?
If you want DIY, then have a nested loop and create the crud links in the inner loop.
An HTML table might be appropriate for presentation.
Here is part of the "child" loop.
<tbody style="background: #ccc">
<c:forEach items="${parent.children}" var="work">
<tr>
<td>${work.id}</td>
<td>${work.title}</td>
<spring:url var="editWorkUrl" value="/work/edit/${work.id}" />
<spring:url var="deleteWorkUrl" value="/work/delete/${work.id}" />
<td>Edit
</td>
<td>Delete
</td>
</tr>
</c:forEach>
</tbody>

Resources