Comparing strings in EL - java

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>

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 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 to avoid the duplicate entry of select box option while edit page?

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

JSP Combo box getting wrong value

I am populating the combo box using data from db. Also i am checking the default n setting it as selected.
<select name="managerID">
<option value="0">None</option>
<c:forEach items="${employees}" var="employee">
<option value="${employee.id}" <c:if test="${edit.managerId == employee.id}">selected="selected"</c:if>>${employee.firstName} ${employee.lastName}, ${employee.email}</option>
</c:forEach>
</select>
the problem is that when i submit i always get the default value and not the user selected one.
hes sorry to have troubled you I found what was wrong with the code.
I rechecked the code there was a hidden field with name managerID and it contained the default value.

Using jsp to detect the value of a html select menu

I am new to a Java environment and JSP. My question is, is is possible to detect the value of a select dropdown option and create a small if statement depending on this value?
For example:
<select id="my_select">
<option value="-1">Option 1</option>
<option value="0">Option 2</option>
</select>
<c:if test="${id eq -1}">
Show this
</c:if>
<c:if test="${id gt -1}>
Show this
</c:if>
Thanks guys!
No, you have to first realize the lifecycle of the JSP. The output is formed on the server and sent to the client. Then the client performs some actions and possibly submits a form back to the server.
In your case you have two options:
use javascript
submit the form holding the dropdown, and depending on ${param.id} show one or the other

Categories