Struts 2 <s:select> drop down box example
In Struts 2 , you can use the <s:select> tag to create a HTML drop down box.
<s:select label="What's your favor search engine" headerKey="-1" headerValue="Select Search Engines" list="searchEngine" name="yourSearchEngine" />
Resulting the following HTML code…
<td class="tdLabel"> <label for="resultAction_yourSearchEngine" class="label"> What's your favor search engine: </label> </td> <td> <select name="yourSearchEngine" id="resultAction_yourSearchEngine"> <option value="-1">Select Search Engines</option> <option value="google.com">google.com</option> <option value="bing.com">bing.com</option> <option value="yahoo.com">yahoo.com</option> <option value="baidu.com">baidu.com</option> </select> </td>
The syntaxs are self explanatory, but the “headerKey” and “headerValue“. The “headerKey” is a key for the first item in the drop down list, and the “headerValue” is the value expression for the first item in the drop down list.
Struts 2 <s:select> example
A full Struts 2 example to create drop down box via <s:select>, and populate the select options via Java list and OGNL list, stored the selected values and display it in another page.
1. Action
Action class to generate and hold the selected drop down box options.
SelectAction.java
package com.mkyong.common.action; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class SelectAction extends ActionSupport{ private List<String> searchEngine; private String yourSearchEngine; private String yourMonth; public String getYourMonth() { return yourMonth; } public void setYourMonth(String yourMonth) { this.yourMonth = yourMonth; } public List<String> getSearchEngine() { return searchEngine; } public void setSearchEngine(List<String> searchEngine) { this.searchEngine = searchEngine; } public String getYourSearchEngine() { return yourSearchEngine; } public void setYourSearchEngine(String yourSearchEngine) { this.yourSearchEngine = yourSearchEngine; } public String getDefaultSearchEngine() { return "yahoo.com"; } public SelectAction(){ searchEngine = new ArrayList<String>(); searchEngine.add("google.com"); searchEngine.add("bing.com"); searchEngine.add("yahoo.com"); searchEngine.add("baidu.com"); } public String execute() { return SUCCESS; } public String display() { return NONE; } }
2. Result page
Render the drop down box via “<s:select>” tag, and populate the select options via Java list and OGNL list
select.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 drop down box example</h1> <s:form action="resultAction" namespace="/"> <h4> <s:select label="What's your favor search engine" headerKey="-1" headerValue="Select Search Engines" list="searchEngine" name="yourSearchEngine" value="defaultSearchEngine" /> </h4> <h4> <s:select label="Select a month" headerKey="-1" headerValue="Select Month" list="#{'1':'Jan', '2':'Feb', '3':'Mar', '4':'Apr'}" name="yourMonth" value="2" /> </h4> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 drop down box example</h1> <h4> Favor search engine : <s:property value="yourSearchEngine"/> </h4> <h4> Selected month : <s:property value="yourMonth"/> </h4> </body> </html>
3. struts.xml
Link all together ~
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="selectAction" class="com.mkyong.common.action.SelectAction" method="display"> <result name="none">pages/select.jsp</result> </action> <action name="resultAction" class="com.mkyong.common.action.SelectAction"> <result name="success">pages/result.jsp</result> </action> </package> </struts>
5. Demo
http://localhost:8080/Struts2Example/selectAction.action

http://localhost:8080/Struts2Example/resultAction.action


This simply does not work. The “list” parameter is not filled from the List in the ActionSupport class. If you replace list=”searchEngine” with list=”{‘google.com’,'bing.com’,'yahoo.com’,'baidu.com’}” it works.
ok, ok, ok, It does work. :\ BUT, you can’t call the select.jsp directly, like making it your welcome-file, ala web.xml. You must call it through an action, ala selectAction.action. If you make index.jsp your welcome-file and put:
document.entryForm.submit();
it should work.
using parenthesis instead of angle brackets .jsp tags…
(body)
(s:form theme=”simple” name=”entryForm” action=”selectAction”)
(/s:form)
(/body)
(script language=”javascript” type=”text/javascript”)
document.entryForm.submit();
(/script)
Hai Mr.mkyong, i want to know where i need to put the following code:
What’s your favor search engine:
Select Search Engines
google.com
bing.com
yahoo.com
baidu.com
in this example? Pls help me!!! I’m stuck with this example.
Is there any way to create listboxes in struts 2
I tried your example but received the following error message. Did you not receive this when you tested it?
org.apache.jasper.JasperException: tag ‘select’, field ‘list’, name ‘yourSearchEngine’: The requested list key ‘searchEngine’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]
Dear Sakthivel,
I am getting the same error. Did you find out the solution for this?
I am using struts 2.3.4.1
Regards,
Nakul
use localhost:8080/Struts2Example/selectAction.action instead of localhost:8080/Struts2Example/pages/select.jsp
because if we access the jsp file directly, we’re not going through the struts2 framework workflow and the Action Class would not be initialized when jsp file is rendering resulting jsp file not finding the List object as the error indicates.
hope this helps :)
Hiii,
I want this functioning in Spring Framework MVC , I want a dropdown list in jsp which fetches the data from the data base,.,all i want is to showing data in dropdown list which is already stored in my database..
it seems the code i wrote in my previous comment is not being displayed.
s:select list=”#{menuList,’All’}”
Hi,
I want something like this.
Here menuList will give me a list say Apple,Banana. I want to merge a option ‘All’ with this list from jsp page.
When i write above code in jsp, it takes Apple,Banana as one option and All as second.
I want Apple,Banana,All as 3 seperate options.
Any suggestions?
Hi Mkyong,
Can you give me the solution for validating the struts2 drop down box using java script? I have tried many possible ways , but i could not figure it out.
Dear Mkyong,
I am a student who learn Struts 2.0. I know the Struts 1.3 very well. Now I am trying to develop my all Ideas through Struts 2.0. Here a problem with select box, I want to set value field and text field, both are different.
Now I’m working with NetBeans IDE 7.1, so I don’t know how to run the downloaded source code. I’m also trying to do, this source code and my knowledge with NetBeans but a error is occur in web.xml.
So I kindly request you to a step by step information to solve the both problems, like how to configure my PC, witch software are required and how to set values in select box.
Hi Alonzo !!! Check yr web.xml. It might have the welcome page or index.jsp mapped to it. Becoz It is not calling the action class. It is calling the jsp file directly.. :(
Hi Tapas,
Even m getting the same error what Alonzo is getting. The problem is the page where m got to use the select tag is my second page in the application i.e after the login page. So please walk me thru the structure of web.xml and struts.xml. i guess the web.xml will anyhow point to the login page. What should i do after the login is success?
how to integreate Struts2 with Jquery ?
pls give idea ?
Nice tutorials, I am using Struts2 jQuery 2.3.1 for one of my projects. How would you create 3 drop down menus where 3rd one depends on 2nd, and 2nd depends on the 1st one? Hints?
Struts 2 comes with a “doubleselect” component which can handle two drop down boxes automatically. Where the 2nd depends on the 1st, see
http://www.mkyong.com/struts2/struts-2-sdoubleselect-example/
If you really cant find a ready solution for 3 drop down boxes, then just do it manually.
1. In Strust 2 , create 3 normal select components.
2. In jQuery, attach event and set the value depend on the other drop down list, jQuery comes with powerful DOM traversing function, which should be quite easy, see below article to set the drop down box value in jQuery.
http://www.mkyong.com/jquery/how-to-set-a-dropdown-box-value-in-jquery/
Yeah, but how can I call an action when 1st drop down is selected and pass its value as parameter? I m not sure how to do that.
Thanks again.
Please reviewed doubleselect codes for hints, or you can use jQuery to call a ajax call to load the data dynamically.
Mkyong,
I tried your example but received the following error message. Did you not receive this when you tested it?
org.apache.jasper.JasperException: tag ‘select’, field ‘list’, name ‘yourSearchEngine’: The requested list key ‘searchEngine’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]