Struts 2 Select Tag Example

Struts 2 Select tag is helpful for implementing the drop down list where user can select one value from the list. Either these values can be send from the bean or it can be defined in the select tag itself. This tutorial shows a simple example to implement this tag. Lets look at the below code.

1. Action Class

package javabeat.net.struts2;

import java.util.ArrayList;

public class Struts2UITagsAction{
	private String cityName;
	private String lang;
	private ArrayList<String> cities;

	public String getLang() {
		return lang;
	}
	public void setLang(String lang) {
		this.lang = lang;
	}
	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public ArrayList<String> getCities() {
		return cities;
	}
	public void setCities(ArrayList<String> cities) {
		this.cities = cities;
	}
	public String execute(){
		if (cityName == null || cityName.trim().length() == 0){
			cities = new ArrayList<String>();
			cities.add("London");
			cities.add("Newyork");
			cities.add("Tokyo");
			return "INDEX";
		}
		return "success";
	}
}

2. Select Tag Example

Select.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
	<h1>JavaBeat - Struts 2 Select Tag Demo</h1>
	<s:form action="SelectDemo">
		<s:select name="cityName" list="cities"/>
		<s:select name="lang" list="#{'Java Lang':'Java', 'C++ Lang':'C++',
                    'Scala Lang':'Scala'}"/>
		<s:submit label="Submit" />
	</s:form>
</body>
</html>

Result.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
	<h1>JavaBeat - Struts 2 Select Tag Demo</h1>
	<h4>
		Selected City :	<s:property value="cityName" /><br>
		Selected Languages :	<s:property value="lang" />
	</h4>
</body>
</html>

3. Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="uitagsdemo" extends="struts-default">
	<action name="SelectDemo" class="javabeat.net.struts2.Struts2UITagsAction" >
		<result name="success">Result.jsp</result>
		<result name="INDEX">Select.jsp</result>
	</action>
</package>
</struts>

4. Select Tag Demo

If you access the application using URL http://localhost:8080/Struts2App/SelectDemo, you would see the following output.

struts2 select tag example input screen

struts2 select tag example output screen

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Manh Exe says:

    I did as your tutorials about select but what I receive are errors as follows:
    org.apache.jasper.JasperException: tag ‘select’, field ‘list’, name ‘cityName’: The requested list key ‘cities’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)
    root cause

    tag ‘select’, field ‘list’, name ‘cityName’: The requested list key ‘cities’ could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} – [unknown location]
    org.apache.struts2.components.Component.fieldError(Component.java:237)
    org.apache.struts2.components.Component.findValue(Component.java:358)
    org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
    org.apache.struts2.components.Select.evaluateExtraParams(Select.java:105)
    org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:856)
    org.apache.struts2.components.UIBean.end(UIBean.java:510)
    org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    org.apache.jsp.Select_jsp._jspx_meth_s_005fselect_005f0(Select_jsp.java:153)
    org.apache.jsp.Select_jsp._jspx_meth_s_005fform_005f0(Select_jsp.java:111)
    org.apache.jsp.Select_jsp._jspService(Select_jsp.java:73)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:389)

    If I clear ,the Select.page will be OK.But,there isn’t this select tag (cities) on browser
    After that,I paste into Select.page and refresh page that everything is OK (notice: I still didn’t stop server)
    Please help me

Speak Your Mind

*