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.
Related
Here is the view that I wish to reference registerViewTwo.jsp
<%#page import="java.util.Calendar"%>
<%#page import="net.tanesha.recaptcha.ReCaptcha"%>
<%#page import="net.tanesha.recaptcha.ReCaptchaFactory"%>
<%#page import="com.ewusu.util.Props"%>
<head>
<title>Register</title>
</head>
<jsp:directive.include file="includes/top.jsp" />
<form:form method="post" id="fm1" cssClass="fm-v clearfix" commandName="${commandName}" htmlEscape="true">
<table style="padding-top: 10px; padding-bottom: 20px;">
<tr><td>First Name:</td><td> <form:input cssClass="required" cssErrorClass="error" id="firstname" size="50"
path="firstname" autocomplete="false" htmlEscape="true" /></td> <td><form:errors path="firstname" cssClass="errors" /></td> </tr>
<tr> <td>Last Name:</td> <td><form:input cssClass="required" cssErrorClass="error" id="lastname" size="50"
path="lastname" autocomplete="false" htmlEscape="true" /></td> <td><form:errors path="lastname" cssClass="errors" /></td> </tr>
<tr> <td>Email:</td> <td><form:input cssClass="required" cssErrorClass="error" id="emailaddress" size="50"
path="emailaddress" autocomplete="false" htmlEscape="true" /></td> <td><form:errors path="emailaddress" cssClass="errors" /></td> </tr>
<tr> <td>Password:</td> <td><form:password cssClass="required" cssErrorClass="error" id="password" size="50"
path="password" autocomplete="false" htmlEscape="true" /></td> <td><form:errors path="password" cssClass="errors" /></td> </tr>
<tr> <td>Gender:</td>
<td><form:select path="gender">
<form:option value="NONE" label="- Select Gender -"/>
<form:option value="Male" label="Male"/>
<form:option value="Female" label="Female"/>
</form:select></td>
<td><form:errors path="gender" cssClass="errors" /></td> </tr>
<tr> <td>Birthday:</td>
<td><form:select path="day">
<form:option value="0" label="- Day: -"/>
<% for(int i = 1; i <= 31; i++) { %>
<form:option value="<%=i %>" label="<%=String.valueOf(i) %>"/>
<% }%>
</form:select>
<form:select path="month">
<form:option value="0" label="- Month: -"/>
<%
String[] months = new String[] {
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
for(int i = 0; i < months.length; i++) {
%>
<form:option value="<%=i+1 %>" label="<%=months[i] %>"/>
<% } %>
</form:select>
<form:select path="year">
<form:option value="0" label="- Year: -"/>
<%
int year = Calendar.getInstance().get(Calendar.YEAR) - 5;//seriously how many 5 yrs olds use the net?!?
int minYear = year - 90; //sure 80 year olds have better things to do with their time
for(; year >= minYear; year--) {
%>
<form:option value="<%=year %>" label="<%=String.valueOf(year) %>"/>
<% } %>
</form:select></td>
<td><form:errors path="day" cssClass="errors" /></td>
</tr>
<tr> <td>Remember me:</td>
<td><form:checkbox path="rememberLogin" value="1"/></td>
<td><form:errors path="rememberLogin" cssClass="errors" /></td>
</tr>
</table>
<%
ReCaptcha c = ReCaptchaFactory.newReCaptcha(Props.PUBLIC_RECAPTCHA_KEY, Props.PRIVATE_RECAPTCHA_KEY, false);
out.print(c.createRecaptchaHtml(null, null));
%>
<form:errors path="recaptcha" cssClass="errors"/>
<div>
<span style="padding-right: 10"> <input class="btn-submit" type="submit" name="_eventId_submit" value="submit" /> </span>
<span> <input class="btn-submit" type="submit" name="_eventId_cancel" value="cancel"/> </span>
</div>
<input type="hidden" name="lt" value="${flowExecutionKey}" />
</form:form>
<jsp:directive.include file="includes/bottom.jsp" />
Here is the web.xml file cas servlet mapping
**<servlet-mapping>
<servlet-name>cas</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<!-- Added reguser servlet mapping 4th May 2014 -->
<servlet-mapping>
<servlet-name>cas</servlet-name>
<url-pattern>/reguser</url-pattern>
</servlet-mapping>**
<!-- Section removed to fit Stackoverflow -->
<session-config>
<!-- Default to 5 minute session timeouts -->
<session-timeout>5</session-timeout>
</session-config>
Here is a snippet from my cas-servlet.xml file which maps the url reguser to the webflow registertwo-webflow.xml
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter"
p:flowExecutor-ref="flowExecutor"
p:flowUrlHandler-ref="flowUrlHandler" />
<bean id="flowUrlHandler" class="org.jasig.cas.web.flow.CasDefaultFlowUrlHandler" />
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
<webflow:flow-execution-attributes>
<webflow:always-redirect-on-pause value="false" />
</webflow:flow-execution-attributes>
</webflow:flow-executor>
<webflow:flow-registry id="flowRegistry" flow-builder-services="builder">
<webflow:flow-location path="/WEB-INF/login-webflow.xml" id="login" />
<webflow:flow-location path="/WEB-INF/remind-webflow.xml" id="remind" />
<webflow:flow-location path="/WEB-INF/register-webflow.xml" id="register" />
<!-- Added new register for Customer and Spec 4th May 2014 -->
**<webflow:flow-location path="/WEB-INF/registertwo-webflow.xml" id="reguser" />**
<webflow:flow-location path="/WEB-INF/update-webflow.xml" id="update" />
</webflow:flow-registry>
Here is my webflow xml file registertwo-webflow.xml and I am trying to call registerViewTwo.jsp in the second to last line shown in this file.
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<on-start>
<evaluate expression="initialFlowSetupAction" />
</on-start>
<!-- VIEW STATES -->
<view-state id="register" view="registerViewTwo" model="customer">
<!-- <var name="customer" class="com.ewusu.model.Customer"/> -->
I am trying to call the registerTwoView.jsp in the registertwo-webflow register state, but when I start the Tomcat webserver and try to access the relevant URL, I receive a null pointer exception (see below)
SEVERE: Servlet.service() for servlet [cas] in context with path [/sso] threw exception [Request processing failed; nested exception is org.springframework.webflow.execution.FlowExecutionException: Exception thrown in state 'register' of flow 'reguser'] with root cause
java.lang.NullPointerException
at org.springframework.webflow.mvc.servlet.ServletMvcView.doRender(ServletMvcView.java:50)
at org.springframework.webflow.mvc.view.AbstractMvcView.render(AbstractMvcView.java:180)
at org.springframework.webflow.engine.ViewState.render(ViewState.java:314)
at org.springframework.webflow.engine.ViewState.doEnter(ViewState.java:208)
at org.springframework.webflow.engine.State.enter(State.java:194)
at org.springframework.webflow.engine.Flow.start(Flow.java:535)
at org.springframework.webflow.engine.impl.FlowExecutionImpl.start(FlowExecutionImpl.java:364)
at org.springframework.webflow.engine.impl.FlowExecutionImpl.start
The strange thing is that the existing registerView.jsp can be accessed using the registertwo-webflow.xml file just fine. I hope I have provided enough information as I cannot understand what is wrong
I'm new to Spring and I've been having some trouble trying to show a form errors.
I have the following form:
<form:form action="loginform.html" commandName="loginForm" onsubmit="goWait();">
<table><tr>
<td>User Name: </td>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>Password: </td>
<td><form:password path="password"/> </td>
<tr>
<td colspan="2">
<input type="submit" value="Login" style="butt-login">
</td>
</tr>
</table>
</form:form>
The form, as can be seen, is backed by a LoginForm Bean that validates it.
I want to show all the errors from the validation, outside this form. Something like this:
<div>
<ul>
<li><form:errors path="userName"/></li>
<li><form:errors path="password"/></li>
</ul>
</div>
This div should be outside the , so I can't use the tag.
How can I do this?
You could just capture the html produced by form:errors and display it somewhere after the form:
<form:form ...>
<c:set var="err">
<form:errors path="*" element="div" id="err" cssClass="hidden" />
</c:set>
</form:form>
....
<c:out value="${err}" />
If you want to show the errors before the form, you can do that with a bit of jQuery
Yes, you can user form:errors tag outside form:form tag
<form:errors path="yourBindedObject.*" />
<form:form action="loginform.html" commandName="loginForm" onsubmit="goWait();">
<table><tr>
<td>User Name: </td>
<td><form:input path="userName" /></td>
</tr>
<tr>
<td>Password: </td>
<td><form:password path="password"/> </td>
<tr>
<td colspan="2">
<input type="submit" value="Login" style="butt-login">
</td>
</tr>
</table>
</form:form>
If you want to show all errors associated with any fields just pass a star to the path attribute.
<form:errors path="*" />
But if you want to show global error messages just pass an empty string:
<form:errors path="" />
More information related: http://www.mkyong.com/spring-mvc/spring-mvc-form-errors-tag-example/
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>
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.
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="..." />