Struts 2 <s:combobox> tag example

Struts 2 <s:combobox> tag example

Previous Home Next

 

Struts 2 framework tag library provide a <s:combobox> tag. This tag show the list value in drop down with single line textfield . 
This tag give facility to user to type value in the textfield and choose drop down list value.
The Struts 2 combobox component provide the following <s:combobox></s:combobox> tag:
<s:combobox label="Month Name" headerKey="-1" headerValue="--- Select ---" list="monthList" name="month" />

 
Directory Structure of <s:combobox> tag Example in Struts 2.0 Using MyEclipse IDE



index.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<h2>Struts2 ComboBox Tag Example</h2>
<s:form action="combo">
<s:combobox list="monthList" headerKey="-1" headerValue="---select---" name="month" label="Slect Month Name" />
<s:combobox label="Select Name" headerKey="-1" headerValue="--- Select ---" list="#{'1':'A', '2':'B', '3':'C', '4':'D'}" name="name" />
<s:submit></s:submit>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

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>
<package name="demo" extends="struts-default">
<action name="" class="org.r4r.ComboboxAction" method="display">
<result name="none" type="dispatcher">/index.jsp</result>
</action>
<action name="combo" class="org.r4r.ComboboxAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

ComboboxAction.java

package org.r4r;

import java.util.ArrayList;
import java.util.List;

public class ComboboxAction {
	List<String> monthList;
	String month;
	String name;
	
	public ComboboxAction(){
		monthList=new ArrayList<String>();
		monthList.add("January");
		monthList.add("February");
		monthList.add("March");
		monthList.add("Aprail");
		monthList.add("May");
		monthList.add("June");
		monthList.add("july");
		monthList.add("August");
		monthList.add("September");
		monthList.add("October");
		monthList.add("November");
		monthList.add("December");
	}
	public String execute(){
		return "success";
	}
	public String display(){
		return "none";
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<String> getMonthList() {
		return monthList;
	}
	public void setMonthList(List<String> monthList) {
		this.monthList = monthList;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	

}

success.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

Month Name Select:-<s:property value="month"/><br/>
Name Select:-<s:property value="name"/>

Output






Previous Home Next