how to retrieve multiple selected values from <select multiple > in java code? - java

code is below:
<select name="merTransactionTypeId" class="cbox" multiple>
<!--
<option value="0" <%=request.getParameter("merTransactionTypeId")!=null?"0".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>All</option>
-->
<option value="2" <%=request.getParameter("merTransactionTypeId")!=null?"2".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Reload</option>
<option value="1" <%=request.getParameter("merTransactionTypeId")!=null?"1".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Sale</option>
<option value="5" <%=request.getParameter("merTransactionTypeId")!=null?"5".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>CCMS_Recharge</option>
<option value="6" <%=request.getParameter("merTransactionTypeId")!=null?"6".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Loyalty_Award</option>
<option value="7" <%=request.getParameter("merTransactionTypeId")!=null?"7".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Loyalty_Redeem</option>
<option value="16" <%=request.getParameter("merTransactionTypeId")!=null?"16".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>FCC_Reload</option>
<option value="11" <%=request.getParameter("merTransactionTypeId")!=null?"11".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Tracking</option>
<option value="12" <%=request.getParameter("merTransactionTypeId")!=null?"12".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Fund_Transfer_From_Card</option>
</select>
i am trying to retrieve values from dropdown with code in scriplet as
<% String[] selectedTransactionTypes = request.getParameterValues("merTransactionTypeId"); %>
...but it's returning null. Please help me out.

Apparently the listbox isn't enclosed in the same <form>, or there's even no means of a <form>, or maybe you tried to access it at the wrong moment (e.g. before form submit), or maybe there's a typo in the parameter name (use getParameterNames() to view them all).
That said, I strongly recommend you to leave the old fashioned scriptlets aside and go ahead with a servlet class to preprocess and postprocess the request and taglibs/EL to control the flow and access data in JSP. It will make your code much cleaner.

Related

how can get dropdown selected value in textbox in jsp

i want selected value in input text field in jsp how to do please help? I am new with jsp.
<select class="leavetype" name="leavetype">
<option value="10">CL</option>
<option value="7">ML</option>
<option value="12">SL</option>
<option value="20">EL/PL</option>
<option value="5">CPL</option>
</select>
<input type="text" name="leave"/>
See this video:- https://www.youtube.com/watch?v=89rRD04QZE4
Hope so it will help you.
In jsp page you may write. ${param.leavetype}.
You can achieve through javascript by using onChange function. Within in function need to do like this document.getElementById ("leave").value = data.value;
Refer this Answer

How to set the selected attribute of Select tab based on the servlet passed fields in JSP?

In my web application, I am working on a screen, where I want to display retrieved data onto the screen. In my screen I have one drop down for "direction". I am setting value of this field from servlet. But I am not getting a way to display the selected option by default in the page.
<select name="indv_adr_mail_st_dir" value="${indv_adr_mail_st_dir}">
<option value="EA">East</option>
<option value="NE">North East</option>
<option value="NO">North</option>
<option value="NW">North West</option>
<option value="SE">South East</option>
<option value="SO">South</option>
<option value="SW">South West</option>
<option value="WE">West</option>
</select>
What is the way to achieve this?
The reason this doesnt work is the <select> tags selected option is designated by placing the selected attribute on a specific option, not by setting the value attribute. For instance
<select name="test">
<option value="yes">Yes</option>
<option selected value="no">No</option> <!-- this is the selected option -->
</select>
For your code you could make this happen by using a ternary operator to optionally place the selected attribute on an option
<select name="indv_adr_mail_st_dir">
<option ${indv_adr_mail_st_dir=="EA"?"selected":""} value="EA">East</option>
<option ${indv_adr_mail_st_dir=="NE"?"selected":""} value="NE">North East</option>
<option ${indv_adr_mail_st_dir=="NO"?"selected":""} value="NO">North</option>
<option ${indv_adr_mail_st_dir=="NW"?"selected":""} value="NW">North West</option>
<option ${indv_adr_mail_st_dir=="SE"?"selected":""} value="SE">South East</option>
<option ${indv_adr_mail_st_dir=="SO"?"selected":""} value="SO">South</option>
<option ${indv_adr_mail_st_dir=="SW"?"selected":""} value="SW">South West</option>
<option ${indv_adr_mail_st_dir=="WE"?"selected":""} value="WE">West</option>
</select>

Add one more option field in the Struts2 select tag

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.

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

How to put css class in to html:select element in jsp

I want to CSS only way to style a html dropdown in a JSP
<html:select name="complaintsForm" class="width_4" property="product">
<option value="">- Please Select -</option>
<html:options name="complaintsForm" property="productList" />
</html:select>
Try this code. It is working fine for me.
<select name="complaintsForm" class="width_4" property="product">
<option value="">- Please Select -</option>
<html:options name="complaintsForm" property="productList" />
</select>
Or you can use styleClass attribute instead of class.

Categories