I have a SELECT tag in JSP page that contains few values :
<select name="selectNumber" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
And I want to get all that values in a Java Class.
What I have tried that :
HttpServletRequest req = (HttpServletRequest)ServletActionContext.getRequest();
String[] selectedNumbers = req.getParameterValues("selectNumber");
But that Array only contains the value I am selecting, and If I don't select any value it contains null. But I want all the option values of the select Tag in java class. Any help?
I am using Struts 2 framework.
if you want to send all the Options to Your Servlet then you must first select them all using simply using Jquery
Before Submitting the Form
$("#selectNumber option").prop('selected',true);
Note #is used for Id , which i prefer if you need to use only name then try
$('[name="selectNumber"]');
Related
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>
Here is an example, with set the value Please_select_a_value at the beginning of the rendered select list:
<s:select id="myGuiId" name="myGuiName" headerKey= "0" headerValue="Please_select_a_value" list="mySourceList" />
This generated the following HTML code:
<select name="myGuiName" id="myGuiId">
<option value="0">Please_select_a_value</option>
<option value="1">Value_1</option>
<option value="2">Value_2</option>
<option value="3">Value_3</option>
<option value="4">Value_4</option>
...
<option value="20">Value_20</option>
</select>
Is there a way, to set the value Please_select_a_value at the end of the rendered select list (if I open select list in GUI) without using some tricks in the correspondent Struts action class? Something like this in HTML code:
<select name="myGuiName" id="myGuiId">
<option value="1">Value_1</option>
<option value="2">Value_2</option>
<option value="3">Value_3</option>
<option value="4">Value_4</option>
...
<option value="20">Value_20</option>
<option value="21">Please_select_a_value</option>
</select>
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.
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 am using struts tag with . there are two opt groups with same keys but different values. when i submit my form, keys go to server to save. now on server side i want to check with which optgroup it was associated how can i do it ?
following is html code
<optgroup label="Group1">
<option value="1">opt1</option>
<option value="3">opt2</option>
<option value="4">opt3</option>
</optgroup>
<optgroup label="Group2">
<option value="1">opt4</option>
<option value="3">opt5</option>
<option value="4">opt6</option>
</optgroup>
Use different values, such as:
<option value="grp1_1">opt1</option>
...
<option value="grp2_1">opt1</option>
Then parse out the value on the server after the for is submitted.