I'm developing a spring application, now I've added a dropdownlist to one of my jsp pages using:
<form:select multiple="single" path="users[y.count-1].X" items="${Y}" itemValue="id" itemLabel="name"/>
Now I'd like to add the default value "Nothing selected", however I can't seem to find how to do this.
I've tried:
<form:select multiple="single" path="users[y.count-1].X" items="${Y}" itemValue="id" itemLabel="name">
<form:option value="Nothing selected" />
</form:select>
But "Nothing selected" isn't displayed in my dropdownlist.
You should be able to do
<form:select multiple="single" path="users[y.count-1].X" >
<form:option value="Nothing selected" />
<form:options items="${Y}" itemValue="id" itemLabel="name" />
</form:select>
Just add this line before your options:
<form:option selected="true" value="..." />
Related
I am working on an application that needs to display a set of questions as a drop down(the user needs to answer 3 questions) in the jsp page.
It works fine, but the issue is for each time I display the drop down list(the questions are same), the questions selected by default are different - the actual question I set in the controller. I need to be able to just display the first or "Select" string as the default option.
My jsp:
<tr>
<br />
<form:label path="qs1">
<span class="">qs 1</span>
</form:label>
<br />
<form:select path="qs1" multiple="false">
<form:option value="${obj.qs0}" label="${obj.qs0}" />
<form:option value="${obj.qs1}" label="${obj.qs1}" />********In this instance this option is shown in the drop down
<form:option value="${obj.qs2}" label="${obj.qs2}" />
<form:option value="${obj.qs3}" label="${obj.qs3}" />
</form:select>
<br />
</tr>
<tr>
<br />
<form:label path="as1">
<span class="">as 1</span>
</form:label>
<br />
<form:input class="" path="as1" />
<br />
</tr>
<tr>
<br />
<form:label path="qs2">
<span class="">qs 2</span>
</form:label>
<br />
<form:select path="qs2" multiple="false">
<form:option value="${obj.qs0}" label="${obj.qs0}" />
<form:option value="${obj.qs1}" label="${obj.qs1}" />
<form:option value="${obj.qs2}" label="${obj.qs2}" />********In this instance this option is shown in the drop down
<form:option value="${obj.qs3}" label="${obj.qs3}" />
</form:select>
<br />
</tr>
<tr>
<br />
<form:label path="as2">
<span class="">as 2</span>
</form:label>
<br />
<form:input class="" path="as2" />
<br />
</tr>
I even tried selected="selected" for the right ones, but it did not work. Any suggestions would be helpful. Thanks.
EDIT: I should have been clear. I did try the option Amit suggested, as of now the obj.qs0 has the value instead of mentioning that in the JSP. I have just added that option along with the questions. The issue is I need to set the questions to the Questions object like below
Questions qs = new Questions();
qs.setQs0("----------Select-------------");
qs.setQs1("what is the name");
qs.setQs2("what is the color");
and so on.
So in the jsp I am setting the path
<form:select path="qs1" multiple="false">
<form:select path="qs2" multiple="false">
So those questions(q1, q2, and so on) are selected automatically and I cannot change the selection to always point to the qs0(where I have the default option). I also need to set the paths differently as I need to be able to capture the different questions & answers the users select.
1) To display "Select" as default, you can add below line :-
<form:select path="qs1" multiple="false">
**<form:option value="NONE" label="--- Select ---" />**
<form:option value="${obj.qs0}" label="${obj.qs0}" />
<form:option value="${obj.qs1}" label="${obj.qs1}" />
<form:option value="${obj.qs2}" label="${obj.qs2}" />
<form:option value="${obj.qs3}" label="${obj.qs3}" />
</form:select>
2) Use the LinkedHashMap or LinkedList to store the drop-down values in order to maintain ordering.
Say I have a simple cfform that looks like this:
<cfform id="fruitForm" method="post" action="">
<cfinput type="radio" name="fruit" id="fruit_apple" value="Apple" /><label for="fruit_apple">Apple</label><br />
<cfinput type="radio" name="fruit" id="fruit_orange" value="Orange" /><label for="fruit_orange">Orange</label><br />
<cfinput type="radio" name="fruit" id="fruit_pear" value="Pear" /><label for="fruit_pear">Pear</label><br />
<cfinput type="submit" name="submitFruit" id="submitFruit" value="Submit" />
</cfform>
How can I use the built-in cfform validation to ensure that at least one radio button in this group is selected? I've tried adding a validate="required" to each of the radio buttons but it doesn't work. Is there any simple way to "require" one of the buttons to be selected using cfform validation?
Do yourself a favor and don't use cfform for validation. Write your own server and client side validation, but according to the cfinput documentation if you add a required="true" attribute to each radio button ColdFusion will do the client side validation for you.
Note: The user can bypass this validation and still submit a form without checking a radio button. You need to have server side validation as well.
<cfform id="fruitForm" method="post" action="">
<cfinput type="radio" name="fruit" id="fruit_apple" value="Apple" required="true" /><label for="fruit_apple">Apple</label><br />
<cfinput type="radio" name="fruit" id="fruit_orange" value="Orange" required="true" /><label for="fruit_orange">Orange</label><br />
<cfinput type="radio" name="fruit" id="fruit_pear" value="Pear" required="true" /><label for="fruit_pear">Pear</label><br />
<cfinput type="submit" name="submitFruit" id="submitFruit" value="Submit" />
</cfform>
This works for me:
<cfform action="abc.cfm" method="post">
<cfinput type="radio" required="yes" message="pick something" name="x" value="1">radio 1
<cfinput type="radio" required="yes" message="pick something" name="x" value="">radio 2
<input type="submit" />
</cfform>
In fact, you don't even need the message attribute. It will still validate.
I'm using Spring 3.1.1.RELEASE. Using the form:select tag, how do I specify which option should be selected? I want the first one to be selected, but when I put
<form:select path="parentOrganization">
<form:option value="" label="Select an Org Type or Parent Org" />
<form:options items="${orgTypesWoParents}" itemValue="id" itemLabel="description" />
<form:option value="" label="=======================" />
</form:select>
the last one is selected. I want to preserve the order of how everything is displayed, which is why I've arranged things in this way.
<form:select path="parentOrganization">
<form:option value="" selected="selected" label="Select an Org Type or Parent Org" />
<form:options items="${orgTypesWoParents}" itemValue="id" itemLabel="description" />
<form:option value="" label="=======================" />
</form:select>
I am trying to split my Radio Button list into "n" number of columns based on number of Rows. This is what I was trying, here programs is my list which currently has 17 Radio Buttons. I am trying to divide (17/2) rows and 2 columns currently.
<c:set var="numRows" value="${fn:length(programs)/2-0.5}" />
<c:set var="totalRows" value="${fn:length(programs)}" />
<c:forEach begin="0" end="${numRows }" varStatus="i">
<c:out value="i=${i.index} " />
<c:set var="rowStart" value="${i.index * numColumns}" />
<fmt:formatNumber var="numColumns" value="${fn:length(programs) / numRows}"
maxFractionDigits="0" />
<c:forEach begin="0" end="${numColumns - 1}" varStatus="j" >
<c:set var="index" value="${rowStart + j.index}"/>
<c:out value="j=${j.index} " />
<c:choose>
<c:when test="${index lt fn:length(programs)}">
<c:forEach var="prgip" items="${programs}">
<c:forEach begin="0" end="${totalRows }" varStatus="k" var="prgip" items="${programs}">
<input type="radio" value="${prgip.program_id}" name="program"> ${prgip.program_name}
<c:out value="k=${k.count} " />
</c:forEach>
</c:forEach>
</c:when>
<c:otherwise> </c:otherwise>
</c:choose>
</c:forEach>
</c:forEach>
Thanks in Advance
-Mim Jones.
My code to iterate list and show the rows
<c:if test="${not empty ftp}">
<c:forEach var="event" items="${ftp}">
<tr><td><input type="text" name="hostName" id="hostName" value="${event.hostName}"size="30" maxlength="200"/></td>
<td><input type="text" name="directory" id="directory" value="${event.directory}" size="30" maxlength="200"/></td>
<td><input type="text" name="userName" id="userName" value="${event.userName}" size="20" maxlength="20"/></td>
<td><input type="text" name="password" id="password" value="${event.password}" size="20" maxlength="20" onblur="checkEnableButton();"/></td>
<td><input type="button" id="delete" onclick="if(confirm( 'Do you want to delete')) deleteRow(this);" value="-" /></td></tr>
</c:forEach>
</c:if>
this works good..but, if my list is empty I need to show a blank row..? and what should I do, if I want to delete and add rows dynamicaaly from JSP page..?
Your current use of <c:if test="${not empty ftp}">...</c:if> can be removed, since the forEach loop will also do the same check and skip any processing if it is indeed empty.
As for displaying a row if empty, you could just do something like:
<c:if test="${empty ftp}">
...display empty table row here...
</c:if>
If you're wanting to do things dynamically, you will need to develop some JavaScript. JSTL is done on the server, but JavaScript will allow you to do dynamic client side processing.