I have a Spring MVC form:select whose form:options are bind with a List<Custom_Object>. The List<Custom_Object> is named as LOCALIZATION_LIST in the code below.
The path attribute of form:select is used to set the selected option.
<form:form action="editNode.do" method="post" name="editNodeForm" commandName="editElementDetails">
<table>
<tr>
<td>Data Type</td>
<td>
<form:select path="datatype" onchange="" cssClass="large" id="datatypes">
<c:if test="${! empty LOCALIZATION_LIST}">
<form:options items="${LOCALIZATION_LIST}" itemLabel="local_Name" itemValue="local_Name"/>
</c:if>
</form:select>
</td>
</tr>
</table>
</form:form>
Now my problem is that there might be a scenario when editElementDetails.datatype may contain a value which is not there in LOCALIZATION_LIST at all. So currently Spring MVC shows the first element of LOCALIZATION_LIST as selected.
Is there a way I can figure out whether the bind action for setting the selected object in form:options of form:select was successful or not?
So that when the binding was not successful, I can then add one extra form:option with the new value in the form.
Note: Answering my own question
I figured it out for now by manually searching for the incoming command object field value in the LOCALIZATION_LIST and if not found, adding an extra form:option corresponding to it.
I was hoping to find a JSTL/Spring-MVC out-of-box solution for this though.
Here is the code:
<form:form action="editNode.do" method="post" name="editNodeForm" commandName="editElementDetails">
<table>
<tr>
<td>Data Type</td>
<td>
<form:select path="datatype" onchange="" cssClass="large" id="datatypes">
<c:if test="${! empty LOCALIZATION_LIST}">
<c:set var="contains" value="false" />
<c:forEach var="item" items="${LOCALIZATION_LIST}">
<c:if test="${item eq editElementDetails.datatype}">
<c:set var="contains" value="true" />
</c:if>
</c:forEach>
<c:if test="${ !contains }">
<form:option label="${editElementDetails.datatype}" value="${editElementDetails.datatype}"/>
</c:if>
<form:options items="${LOCALIZATION_LIST}" itemLabel="local_Name" itemValue="local_Name"/>
</c:if>
</form:select>
</td>
</tr>
</table>
</form:form>
What you want to achieve is the process of validating a form submission that is what I understood (maybe got you all wrong).You can use Spring-Validator here is one nice tutorial about using it.
Spring Form validation . Hope this helps.
Related
I have requirement something similar like below in our application,
I would like to show the below details in the form of table.
Country Name Population CapitalCity Aria
US XX XX XX XX
IN YY YY YY YY
User can choose in the configuration page which columns he is interested to see.
In the backend I set model attributes as below(using spring MVC),
model.addAttribute("selColumns", "column keys");
model.addAttribute("countryDetails", "List of country details");
In the CountryDetail class, field names and selColumns key names are same.
class CountryDetails {
private String country,
population,
CapitalCity,
Aria;
}
In the UI I am trying with the following code to achieve the same.
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<c:forEach items="${selColumns}" var="item">
<th><spring:message code="${item}" /></th>
</c:forEach>
</tr>
</thead>
<tbody>
<c:forEach items="${countryDetails}" var="det">
<tr>
<c:forEach items="${selColumns}" var="item">
<td>
//Below code is not working
<c:out value="${item.det}" /></td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
Table header is working fine. But I am struggling to show row information only for the configured columns.
Code wouldn't work because it tries to find getDet() at the java side.
Could some one please help is there any way in JSP, if I give property(field) name the corresponding value would return?
Something like this
Thanks in advance,
kitty
Try this one.
<tbody>
<c:forEach items="${countryDetails}" var="det">
<tr>
<c:forEach items="${selColumns}" var="item">
<td>
<c:out value="${det[item]}" />
</td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
Note:
selColumns = ["Country", "Population", "CapitalCity", "Aria"]
CountryDetails properties = Country,Population,CapitalCity,Aria
I'm attempting to convert an input form from a to a but does not accept the list attribute, does anyone know a workaround? This is my first time using Spring and I'm at a loss here.
<tr>
<th><form:label path="livrable.composant"><s:message code="composant"/></form:label></th>
<td>
<form:input path="livrables.composants" list="composants">
<c:if test = "${not empty composants}">
<datalist id="composants">
<c:forEach var="composantValue" items="${composants}">
<option value="${composantValue.nom}"/>
</c:forEach>
</datalist>
</c:if>
<br/>
<form:errors path="livrable.composant" cssClass="error"/>
</td></tr>
Nevermind I'm an idiot, I didn't close the tag it should be:
<form:input path="livrables.composants" list="composants"/>
I managed to integrate HDIV and Spring MVC. Now HDIV generated security URLs for the static links. But when I tries to submit a link with a parameter, I always get error message. I know the reason is when the URL of a form is generated, the parameter is not a part of the URL. But I cannot find a workaround. Please help. Thanks a lot.
The form part is like this:
<c:url var="url" value="/contract/report/report" />
<form:form action="${url}" method="get">
<table >
<tr>
<td><label>Name:</label></td>
<td><select id="nameId" name="nameId">
<c:forEach var="c" items="${Users}">
<option value='${c.id}'> ${c.name}</option>
</c:forEach>
</select> </td>
<td><Button type="submit" >Submit</Button> </td>
</tr>
</table>
</form:form>
Update:
I found the workaround is I have to rewrite the select options with spring options like this:
<form:select path="contractId">
<c:forEach var="c" items="${Users}">
<form:option value="${c.id}" label="${c.name}"></form:option>
<c:forEach var="c" items="${Users}">
</form:select>
Thanks everyone.
If you want to add a parameter to the URL then this might work.
<c:url var="url" value="/contract/report/report"><c:param name="parameter" value="value" /></c:url>
Don't use c:url for forms, you don't need it:
<form:form action="${pageContext.servletContext.contextPath}/contract/report/report" method="get">
...
</form:form>
I found the workaround is I have to rewrite the select options with spring options like this:
<form:select path="contractId">
<c:forEach var="c" items="${Users}">
<form:option value="${c.id}" label="${c.name}"></form:option>
<c:forEach var="c" items="${Users}">
</form:select>
Thanks everyone.
What i have to create is a form that i migrated out of access. I created the 4 asp pages. Now what they would like is to store the user input values some how until they hit the final page before writing to the SQL database. What is the easiest way to store this data until the final page for submission? I could try an array or maybe even java script?
You could use the session object - http://msdn.microsoft.com/en-us/library/ms525095(v=vs.90).aspx
As suggested in this other answer, you could use session variables. The alternative is to just pass them from page to page using hidden form fields, eg
<input name="yourvariable" type="hidden" value="<%=Request.Form("yourvariable")%>
There's no correct answer, it's a case of which you're most comfortable with
"get_user_info.asp"
<html>
<head></head>
<body>
<form id='frm_user_info' name='frm_user_info' method='post' action='get_user_info_go.asp'>
<table cellspacing='3'>
<tr>
<td align=right>Name</td>
<td><input type='text' id='s_name' name='s_name' size='40'></td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Next'></td>
</tr>
</table>
</form>
</body>
</html>
"get_user_info_go.asp"
<%
s_name=request.form("s_name")
if (s_name="") then response.redirect("get_user_info.asp")
session("s_name")=s_name
response.redirect("next_page.asp")
%>
Is there a way of setting a initial value (One of x in the list) for a
<form:select drop down similar to the text area default value?
i.e.
<td valign="top">
<form:select id="${appNameFormId}" path="metadata.appName" items="${dbList}"
disabled="true" itemValue="appName" itemLabel="appName">
</form:select>
</td>
<td valign="top">
<TEXTAREA id="${nameFormId}" name=jobName ROWS="1" COLS="25" >${job.jobName}
</TEXTAREA>
</td>
Because SELECT value comes from command object during rendering and go back to command object after submission I suggest to assign default value to metadata.appName inside Controller (manually or by using method with #ModelAttribute annotation: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib).