下面是一个struts2 标签的一个例子
Action: TagAction.java: 用于页面要显示的一些数据,如:下拉列表的框的集合之类的
package com.snta.struts2.action;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class TagsAction extends ActionSupport{
private String username;
private String userpwd;
private int a;
private float b;
private double c;
private Date registerDate;
private List<String> selList=new ArrayList<String>();
private Map<String, String> provinceList=new HashMap<String, String>();
private Set<Pair> pairList=new HashSet<Pair>();
private Map<String, List<String>>cscadeMap=new HashMap<String, List<String>>();
public TagsAction() {
super();
pairList.add(new Pair("1","河南"));
pairList.add(new Pair("2","长沙"));
provinceList.put("1", "上海");
provinceList.put("2", "北京");
provinceList.put("3", "广东");
this.registerDate=new Date();
List<String> list1=new ArrayList<String>();
list1.add("CTIServer");
list1.add("Campagin");
list1.add("Sloate");
List<String> list2=new ArrayList<String>();
list2.add("J2EE");
list2.add("UML");
list2.add("Web2.0");
cscadeMap.put("项目", list1);
cscadeMap.put("技术", list2);
}
public String execute()throws Exception{
return "success";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public float getB() {
return b;
}
public void setB(float b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
public Date getRegisterDate() {
return registerDate;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
public List<String> getSelList() {
return selList;
}
public void setSelList(List<String> selList) {
this.selList = selList;
}
public Map<String, String> getProvinceList() {
return provinceList;
}
public void setProvinceList(Map<String, String> provinceList) {
this.provinceList = provinceList;
}
public Set<Pair> getPairList() {
return pairList;
}
public void setPairList(Set<Pair> pairList) {
this.pairList = pairList;
}
public Map<String, List<String>> getCscadeMap() {
return cscadeMap;
}
public void setCscadeMap(Map<String, List<String>> cscadeMap) {
this.cscadeMap = cscadeMap;
}
}
Pojo:Pair.java 做为集合的元素使用
package com.snta.struts2.action;
public class Pair {
private String id;
private String name;
public Pair() {
super();
}
public Pair(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
页面tag_1.jsp
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<s:head theme="ajax"/>
<base href="<%=basePath%>">
<title>My JSP 'tag_1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<STYLE type="text/css">
div.tag{
border:1px solid #445566;
font-size:12px;
background-color:#889900;
}
div.content{
border:1px solid #445566;
font-size:10px;
background-color:#889988;
}
</STYLE>
</head>
<body>
<br>
<div class="tag">
<s:form action="tag" namespace="/snt" theme="simple">
cscadeMap:<s:property value="cscadeMap"/>
文本框
<s:textfield name="username" />用户名<br>
<s:password name="userpwd" />口令
</s:form>
</div>
<br>
<div class="tag">
<s:form theme="simple">
下拉列表框
<s:select
tooltip="Choose Your Favourite Color"
label="Favorite Color"
list="{'Red', 'Blue', 'Green'}"
name="favoriteColor"
emptyOption="true"
headerKey="None"
headerValue="None"/>
省份
<s:select list="{'河南','上海','南京'}"
label="province"
name="province"
emptyOption="false"
headerKey="1"
headerValue="请选择省份"/>
</s:form>
</div>
<br>
<div class="tag">
动态下拉列表框架
<s:select
tooltip="ChooseYour Province"
label="provinceList"
list="pairList"
name="pairList"
listKey="id"
listValue="name"
emptyOption="false"
headerKey="0"
headerValue="请选择省份"/>
<div>
List Set 实现下拉列表框
${pairList }
</div>
</div>
<br>
<div class="tag">
动态下拉列表框架
<s:select
tooltip="ChooseYour Province"
label="provinceList"
list="provinceList"
name="provinceList"
listKey="key"
listValue="value"
emptyOption="false"
headerKey="0"
headerValue="请选择省份"
multiple="false"
value="0"/>
<div>
Map 实现下拉列表框
${provinceList }
<ul>
<li>headerKey不可以为空,不可以为-1。 </li>
<li>struts2中的select 标签中,必须设置的属性只有一个,即是list</li>
</ul>
</div>
</div>
<br>
<div class="tag">
<s:url action="tag" namespace="/snt" id="actionUrl" includeParams="all">
<s:param name="username">tom</s:param>
<s:param name="userpwd">123</s:param>
</s:url>
<s:a href="%{actionUrl}" >
tag.action
</s:a>
<br>
<a href='<s:url action="tag" namespace="/snt">
<s:param name="username">tom</s:param>
</s:url>'>tag.action</a>
<div>
URL标签
<ul>
<li>username:<s:property value="username"/> </li>
<li>userpwd:<s:property value="userpwd"/></li>
</ul>
</div>
</div>
<br>
<div class="tag">
日期标签
<s:datetimepicker
adjustWeeks="true"
label="请选择日期"
type="date"
value="today"
toggleType="explode"
toggleDuration="300"
displayFormat="yyyy-MM-dd H:mm:ss"
formatLength="long"
name="registerDate"/>
<s:datetimepicker
label="请选择时间"
type="time"
value="%{'2009-08-21 12:23'}"
toggleType="explode"
toggleDuration="300"
displayFormat="yyyy-MM-dd H:mm:ss"
name="registerDate"/>
<div>
日期标签
wipe
explode
<s:property value="registerDate"/>
</div>
</div>
<br>
<div class="tag">
隐藏域
<s:hidden name="id" value="1" />
</div>
<div class="tag">
使用简单集合来生成多个复选框
<s:checkboxlist name="a" label="请选择您喜欢的图书" labelposition="top"
list="{'Spring2.0宝典' , '轻量级J2EE企业应用实战' , '基于J2EE的Ajax宝典'}"/>
<br>
使用简单Map对象来生成多个复选框
<s:checkboxlist name="b" label="请选择您想选择出版日期" labelposition="top"
list="#{'Spring2.0宝典':'2006年10月' , '轻量级J2EE企业应用实战':'2007月4月' , '基于J2EE的Ajax宝典':'2007年6月'}"
listKey="key"
listValue="value"/>
<br>
动态集合属性
<s:checkboxlist name="province"
list="pairList"
label="省份"
listKey="id"
listValue="name"
/>
pairList:<s:property value="pairList"/>
</div>
<br>
<div class="tag">
<s:combobox label="请选择您喜欢的图书" theme="css_xhtml" labelposition="top"
list="{'Spring2.0宝典' , '轻量级J2EE企业应用实战' , '基于J2EE的Ajax宝典'}"
size="20" maxlength="20" name="book"/>
</div>
<br>
<div class="tag">
<s:select label="选择您喜欢的图书"
name="book"
list="#{'Spring2.0宝典':'李刚','轻量级J2EE企业应用实战':'李刚','基于J2EE的Ajax宝典':'李刚'}"
listKey="value"
listValue="key">
<s:optgroup label="Rod Johnson"
list="#{'Expert One-on-One J2EE Design and Development':'Johnson'}"
listKey="value"
listValue="key"/>
<s:optgroup label="David Flanagan"
list="#{'JavaScript: The Definitive Guide':'David'}"
listKey="value"
listValue="key"/>
</s:select>
</div>
<div class="tag">
<s:optiontransferselect
label="请选择你喜欢的图书"
name="cnbook"
leftTitle="中文图书:"
rightTitle="外文图书"
list="{'Spring2.0宝典','轻量级J2EE企业应用实战','基于J2EE的Ajax宝典'}"
multiple="true"
addToLeftLabel="向左移动"
selectAllLabel="全部选择"
addAllToRightLabel="全部右移"
headerKey="cnKey"
headerValue="--- 选择中文图书 ---"
emptyOption="true"
doubleList="{' J2EE Design and Development', 'The Definitive Guide'}"
doubleName="enBook"
doubleHeaderKey="enKey"
doubleHeaderValue="--- 选择外文图书 ---"
doubleEmptyOption="true"
doubleMultiple="true"
/>
</div>
<br>
<div class="tag">
<s:updownselect name="a" label="请选择您喜欢的图书" labelposition="top"
moveUpLabel="向上移动"
list="{'Spring2.0宝典' , '轻量级J2EE企业应用实战' , 'JavaScript: The Definitive Guide'}"/>
<!-- 使用简单Map对象来生成可上下移动选项的下拉选择框
且使用emptyOption="true"增加一个空选项-->
<s:updownselect name="b" label="请选择您想选择出版日期" labelposition="top"
moveDownLabel="向下移动"
list="#{'Spring2.0宝典':'2006年10月' , '轻量级J2EE企业应用实战':'2007月4月' , '基于J2EE的Ajax宝典':'2007年6月'}"
listKey="key"
emptyOption="true"
listValue="value"/>
</div>
<br>
<div class="tag">
<s:form action="tag" method="post" namespace="/snt" theme="simple" name="from1" id="form1">
<s:doubleselect
label="请选择您喜欢的图书"
name="author"
list="{'李刚', 'David'}"
doubleList="top == '李刚' ? {'Spring2.0宝典', '轻量级J2EE企业应用实战' , '基于J2EE的Ajax宝典'} : {'JavaScript: The Definitive Guide'}"
doubleName="book"/>
<s:set name="fset" value="%{cscadeMap}"/>
<s:doubleselect name="prj" list="#fset.keySet()" doubleName="brantch" doubleList="#fset[top]" />
</s:form>
</div>
<br>
<div class="tag">
<s:tree label="计算机图书" id="book" theme="ajax"
showRootGrid="true" showGrid="true" treeSelectedTopic="treeSelected">
<s:treenode theme="ajax" label="李刚" id="yeeku">
<s:treenode theme="ajax" label="Spring2.0宝典" id="spring"/>
<s:treenode theme="ajax" label="轻量级J2EE企业应用实战" id="lightweight"/>
<s:treenode theme="ajax" label="基于J2EE的Ajax宝典" id="ajax"/>
</s:treenode>
<s:treenode theme="ajax" label="David" id="David">
<s:treenode theme="ajax" label="JavaScript: The Definitive Guide" id="javascript"/>
</s:treenode>
<s:treenode theme="ajax" label="Johnson" id="Johnson">
<s:treenode theme="ajax" label="Expert One-on-One J2EE Design and Development" id="j2ee"/>
</s:treenode>
</s:tree>
</div>
<br>
<div class="tag">
<s:append id="newList">
<s:param value="{'Spring2.0宝典','轻量级J2EE企业应用实战','基于J2EE的Ajax宝典'}" />
<s:param value="{'新东方IT培训', '东方标准职业教育'}" />
</s:append>
<table border="1" width="240">
<s:iterator value="#newList" status="st">
<tr <s:if test="#st.odd">style="background-color:#bbbbbb"</s:if>>
<td><s:property/></td>
</tr>
</s:iterator>
</table>
</div>
</body>
</html>