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.
Related
i have one struts 2 select tag like below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc"></s:select>
it rendered as below
<select data-inputs="myListInput" >
<option value="myListVal1" >myListDesc1</option>
<option value="myListVal2" >myListDesc2</option>
<option value="myListVal3" >myListDesc3</option>
</select>
i want to add one more option field with null value
<option value="" ></option>
to the Option field with modification in the list.
In struts 1 it is possible like below
<html:option value=""></html:option>
like that is it possible in the struts 2
note:i couldn't able to modifiy the list before the page load in the java
There is an Attribute called emptyOption in the select tag.
It worked as below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc" emptyOption="true" ></s:select>
if you want to add one more option field with null value at the begining of the list you can use headerKey & headerValue attribute of s:select tag.
Check example given below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc" headerKey="0" headerValue="" ></s:select>
So the first element of your list will be empty & it's value will be 0(zero) here.
It will be render as
<select data-inputs="myListInput" >
<option value="" >0</option>
<option value="myListVal1" >myListDesc1</option>
<option value="myListVal2" >myListDesc2</option>
<option value="myListVal3" >myListDesc3</option>
</select>
You can set any value on place of 0 (zero).
Hope this will help you.
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)
Could anyone please explain me how to avoid the duplicate enters of records in to the select box options during editing the page.I am using java to develop web application I am very new to that,I need to remove the duplicates from the select box option while editing.Could anyone please help me .
Sample Code
<td><select property="employeestatus" value="" id="employeestatus">
<c:if test="${employee.employeestatus!=null}">
<option value="${employee.employeestatus}">${employee.employeestatus}</option>
</c:if>
<option value="">Select</option>
<option value="Active">Active</option>
<option value="Terminated">Terminated</option>
<option value="Deceased">Deceased</option>
<option value="Resigned">Resigned</option>
<option value="InActive">InActive</option>
</select>
<html:errors property="employeestatus"/>
<p id="employeestatus" style="color:red"/>
</td>
I would suggest to add the selected attribute on the option corresponding to your "employeestatus" variable instead of duplicating this option on the first line.
See HTML option selected Attribute
By the <html:errors ...>, I gess you use Struts 1. If it's true, I would use the
<html:select ...> tag and
<html:options or optionsCollection ...>
with java Map store in some context (servlet, session or request) to configure options. That struts tag will select the good option corresponding to your variable value.
See Struts <html:select ...> doc
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>
How to compare the strings in JSP EL. I tried to do
<select name="groupa" style="width: 170px">
<option value ="-1">no group</option>
<c:forEach var="gr" items="${sessionScope['entrantsAcceptor'].groups}">
<option value="${gr.idGroup}" <c:if test="${gr.code == param.group}">checked</c:if> >${gr.code}</option>
</c:forEach>
</select>
But it doesn't work(there are no any checked in options of select(though I pass correct argument)). Also I have tried to do
${gr.code eq param.group}
but it also has no effect
The EL looks fine and should work fine. Rightclick the page in webbrowser and choose View Source. Do you see the checked attribute being set in the right place in the generated HTML?
But this is after all not the right attribute. On a HTML <option> element you need to set the selected attribute, not the checked attribute.
<c:if test="${gr.code == param.group}">selected</c:if>