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.

To auto select a default value for drop down box, read this artcle : Auto select drop down box value in Struts 2

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

Struts2 drop down box example

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

Struts2 drop down box example

Reference

  1. Struts 2 select documentation
Tags :
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to the charity, thanks.

Related Posts

Popular Posts