select box with database interaction in JSTL - java

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>

Related

How to delete an Item from a List in JSTL?

We can delete items from list in Simple java class.
Now I want to delete an Item from a list which comes from controller.
I want to know that is there any replacement of list.remove(int) in JSTL ?
please suggest.
You can't remove the item in the c:forEach tag but you can use c:if tag to filter 'removedItem' from the options.
Check this. http://www.java2s.com/Code/Java/JSTL/JSTLModifyacollection.htm
And for example, if you have following scenerio:-
<select>
<option value="0">Select</option>
<c:forEach items="${list} var="list">
<c:if test="${list.value != 'removedItem'}">
<option value="${list.value}">${list.displayText}</option>
</c:if>
</c:forEach>
</select>
Hope it helps.
you can't remove exactly, but you can filter it out using <c:if>, as
<select>
<option value="0">Select</option>
<c:forEach items="${list} var="myList">
<c:if test="${myList.value != 'something'}"> // if you know value then you can put in place of something, and filter it out.
<option value="${myList.value}">${myList.displayText}</option>
</c:if>
</c:forEach>
</select>
this is just an example, how you can remove it using JSTL , by filtering.
if you dont want dropdown, then you can remove <select> and <option> and start from <c:forEach>, and where we are storing data in option, replace it by something.
explination of above code:
as you can see, i loop the list, and where value is not something it add one <option> in select dropdown, in short, it removes from dropdown by filtering it.

Spring mvc and dynamic submit form

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?

how to get values of several dropdown lists from jsp to servlet

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)

how can i make field dependent to another field in jsp?

<td>Branch</td>
<td>
<select name="branch" type="text">
<option value="0">Select Branch</option>
<option value="CE">CE</option>
<option value="IT">IT</option>
</td>
<td>Batch</td>
i.e if i have branch field in my jsp form
so my branch firld contains 2 inputs as 1.CE & 2.IT.
So when i click on CE ,so it have to show me A1,A2,....A5 & when i click on IT, it will have to show B1...B5 from which we can select any one of the field.
So, pls help me for above program.
Here is my code.
Pls help me what code should i write in "Batch" field""
you have to register onChange() event of the branch and call an javascript function which will populate the batch field accordingly.

Display data from database in SINGLE dropdownlist

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>

Categories