Display data from database in SINGLE dropdownlist - java

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>

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.

How to retain the value of the listbox selected when user login to a form

I am working in an application where i have a list box with some values there ,i am selecting a value from there and then i am submitting it.The value is inserted into the database properly.Now what i want ,i want when the user again login to the from he should be able to see that selected values in the listbox.I am able to pick the value from the database when the user is loggin into the system .But i can not manage to show it selected ,Here is my code
<tr>
<th>What would be the single biggest motivator in a potential new opportunity?</th>
<td>
<select id="motivator" name="motivator" multiple="multiple">
<% while(rs8.next()){
motivatorString = rs8.getString("motivators");
%>
<option value ="<%=motivatorString %>" ><%=motivatorString %> </option>
<%} %>
</select>
</td>
</tr>
How can i achieve that ??
Use attribute selected="selected" for option like:
<option value ="<%=motivatorString %>" selected="selected_only_if__motivatorString_value_equals_value_from_db"><%=motivatorString %> </option>
selected_only_if__motivatorString_value_equals_value_from_db is just a placeholder to tell u that replace it with "selected" when the string matches the value in DB

How to get the Selected INDEX of a dropdown list from a servlet?

Say I have
<select id="year" name="year">
<option value=""></option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
</select>
In a form that I pass to my servlet. How can I get the selected INDEX of the list? I have tried looking online but all I can see are answers on how to get the VALUE.
I want the index, because I want to be able to save and load the parameters of a form, but I have dynamically created select lists with values that change when other values in the form change. So simply getting the current index to reload the form would be a lot simpler.
I need to use a servlet because I want to be able to save and load this data from a file, and I don't believe you can locally save files using only javascript. I know I can LOAD using only javascript though, but the problem lies in the saving for now.
I know you can get the selected value of the list by doing something like
request.getParameter("year");
But I've found no way so far of getting the selected index.
Thanks for any help!
How can I get the selected INDEX of the list?
You can't - all that gets sent to the server when you submit the form is the value of the selected option (e.g. year=2009).
You'd have to add some client-side JavaScript to populate a hidden field in the form with the selected index whenever the selection changes.

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)

select box with database interaction in JSTL

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>

Categories