Struts 2 <s:select> tag example
Previous | Home | Next |
The Struts 2.0 framework provide select tag to drop down item in the list through the<s:select></s:select> tag.This tag provide the facility to get drop down element in the list .The Struts 2 select component provide the following <s:select></s:select> tag:<s:select label="Day Name" headerKey="-1" headerValue="Select Day" list="selectDayList" name="day"/>
Directory Structure of <s:select> tag Example in Struts 2.0 Using MyEclipse IDEindex.jsp<%@taglib uri="/struts-tags" prefix="s"%> <h2>Select tag example in struts2 framework</h2> <s:form action="select"> <s:select label="Day Name" headerKey="-1" headerValue="Select Day list="selectDayList" name="day"/> <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.SelectAction" method="display"> <result name="none" type="dispatcher">/index.jsp</result> </action> <action name="select" class="org.r4r.SelectAction"> <result name="success">/success.jsp</result> </action> </package> </struts>SelectAction.javapackage org.r4r; import java.util.ArrayList; import java.util.List; public class SelectAction { List<String> selectDayList; String day; public SelectAction(){ selectDayList=new ArrayList<String>(); selectDayList.add("Sunday"); selectDayList.add("Monday"); selectDayList.add("Tuesday"); selectDayList.add("Wednesday"); selectDayList.add("Thursday"); selectDayList.add("Friday"); selectDayList.add("Saturday"); } public String execute(){ return "success"; } public String display(){ return "none"; } public List<String> getSelectDayList() { return selectDayList; } public void setSelectDayList(List<String> selectDayList) { this.selectDayList = selectDayList; } public String getDay() { return day; } public void setDay(String day) { this.day = day; } }success.jsp<%@taglib uri="/struts-tags" prefix="s"%> Select Day:-<s:property value="day"/>Output
Previous | Home | Next |