I am trying to learn Spring mvc and JSP, and I have a problem that how I can get the value of select list dynamically?
If I have a select list:
<select name="qty>
<option>1</option>
</select>
and in the spring mvc, I can get the it value by using #RequestParam("qty") int qty. However, If I surround the select list with a loop:
<c:forEach begin="1" end="10">
<select name="qty>
<option>1</option>
</select>
</c:forEach>
I am thinking of using an array #RequestParam("qty") int[] qty but I think it is not a good option because well the form is much more complicated that this example, and assume that the qty is associate with a specific item. So how can get the qty of the item which the user has selected during submitting the form?
Related
i have a question when i try to capture the value of dropdown list from jsp file to servlet.
<select name="extraroom">
<c:if test="${booking.roomType ne 'single'}">
<c:forEach var="i" begin="0" end="${booking.count}">
<option value="<c:out value="${i}"/>"><c:out value="${i}"/></option>
</c:forEach>
</c:if>
</select>
i use Integer.parseInt(request.getParameter("extraroom"))
i have many drop down lists according to different roomType.
but the result in java servlet always get the value of the first dropdown lists...
what should i change in servlet?
Thank you.
For a select to be retrieved by a server the select has to
be inside a form and
have a unique name identifier inside (it; best the identifier is unqiue across the entire DOM tree)
I have 1 goal which is to DISPLAY DATA from DATABASE in one SINGLE DROPDOWN LIST.
Right now in my database there are 3 data. I am able to display in 3 DROPDOWN LISTS which I do not know what. Is there a way to display in one dropdown list instead?
Below are my codes.
<c:forEach var="staff" items="${staff}">
<select name="staffId"><option value="${staff.staffId}">${staff.staffName}</option></select>
</c:forEach>
<select name="staffId">
<c:forEach var="staff" items="${staff}">
<option value="${staff.staffId}">${staff.staffName}</option>
</c:forEach>
</select>
I am not very familiar with the Spring tag and seems like i am struck in some issue which i am not able to understand as of now.
I have displaying two select tags in my jsp and they are backed by an Arraylist and map here is the code for them
<form:select path="prsBTOData[${status.index}].colors" items="${prsBTOData.colors}"
cssClass="productDetailsSelect"/>
and
<form:select path="prsBTOData[${status.index}].fonts" items="${prsBTOData.fonts}"
cssClass="productDetailsSelect" >
colors is being backed by Array list while the fonts is being backed by the Map.below is the generated HTML
<select multiple="multiple" class="productDetailsSelect" name="prsBTOData[0].colors"
id="prsBTOData0.colors">
<option selected="selected" value="Red">Red</option>
<option selected="selected" value="Green">Green</option>
<option selected="selected" value="Black">Black</option>
</select>
<input type="hidden" value="1" name="_prsBTOData[0].colors">
i am not sure why its doing multiple="multiple" and not showing any drop down but only showing the RED as selected value, while i was expecting a list with drop down options.
even not sure why this hidden field is getting generated and what is its purpose?
In form:select the items attribute is the list of items that needs to be displayed in select box. And path attribute is the property that is bound with the selected value.
As you have given an arraylist (having multiple values) as the path, spring assumed you wanted a multiple value selected drop down.
You might want to give like this (assuming you have color property for prsBTOData )
<form:select path="prsBTOData.color" items="${prsBTOData.colors}"/>
And consider using separate model objects for maintaining static/reference data (colors,fonts) as below:
<form:select path="prsBTOData.color" items="${referenceData.colors}"/>
I want to set value differently and option differently in an Select tag
ex.
<select name="myselect">
<option value="1">one</option>
</select>
that mean i have two list from DB
1)one is Id of username to which i want to keep in value of select
2)second is username which is other. this i want to keep in between option tag
Please Help
thanks
Rakesh
If you are sure that both of the list are in sync (in terms of index , i.e. user Abc from list a is at the top so id of the Abc in other list is at the top)
You can do it following way
<c:forEach var="idOfPerson" items="${listOfId}" varStatus="status">
<option value="${idOfPerson}">${listOfNames[status.index]}</option>
</c:forEach>
My form has a custom element like below, created using custom ajax:
<select jwcid="testtest <at> Any">
<option value="x">California -- CA</option>
<option value="y">Colorado -- CO</option>
<option value="z">Connecticut -- CN</option>
</select>
After the form is submitted, how do I get the value of this custom html element?
cycle.getPage().getComponents().get("testtest") ?
If I understand you correctly, you have a form element generated not by Tapestry, but by something else.
First of all, jwcid has no place in your HTML code, it's only used in Tapestry component templates. Second, the select element must have a name attribute or else your browser won't submit it at all:
<select name="name-of-element">
...
</select>
To get the submitted value on the server side, use cycle.getParameter("name-of-element") in your page/component class.