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

 

http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/