`
hhr_michael
  • 浏览: 72784 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类
最新评论

struts2+hibernate3+spring2读书笔记7(Struts 2 标签库)

阅读更多

                                      第8章 Struts 2 标签库    
一. 控制标签
实例需求:Strut2的控制标签属于UI标签,它主要用于完成流程控制,例如循环和分支等操作。本实例对Struts2的几个控制标签:iterator、if/elseif/else、append、generator、sort、merge和subset的使用进行了实例的操作。(注:建立的开发环境与前面几章类似,在这里就不举例了。)

1. Iterator标签
各属性介绍:
(1)id:可选项,集合中引用的元素的id,对于UI和表单标签可以用来做html的id属性。
(2)value:可选项,需要进行迭代的迭代源,或者对象本身将会被放置到一个新的列表中。
(3)status:可选项,指定迭代时的IteratorStatus实例。利用它可获得当前对象的索引。其中属性:count是指当前迭代的元素、index指当前迭代元素的索引、even指当前迭代元素的索引是否为偶数(第一条为1)、odd指当前迭代元素的索引是否为奇数(第一条为1)、first指当前迭代元素是否为第一个元素、last指当前迭代元素是否为最后一个元素
         
在iterator的value属性中给定了水果集合,指定id后,迭代输出fruitName(水果名),完整代码如下:

    
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Iterator 标签实例</title>
</head>
<body>
	<table border="1">
		<s:iterator value="{'banana','apple','orange','cherry'}" id="fruitName">
			<tr>
				<td>
					<s:property value="fruitName"/>
				</td>
			</tr>
	
		</s:iterator>
	</table>
</body>
</html>


在以上的代码中进行修改,输出status的各属性信息,修改后的代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Iterator 标签实例</title>
</head>
<body>
	<table border="1">
	       <tr>
	       		<td>索引</td>
	       		<td>水果名称</td>
	       		<td>是否为第一个</td>
	       		<td>是否为最后一个</td>
	       		<td>是否为奇数</td>
	       		<td>是否为偶数</td>
	       </tr>
		<s:iterator value="{'banana','apple','orange','cherry'}" id="fruitName" status="st">
			<tr>
			    <td> <s:property value="#st.index"/> </td>
				<td> <s:property value="fruitName"/> </td>
				<td> <s:property value="#st.first"/> </td>
				<td> <s:property value="#st.last"/> </td>
				<td> <s:property value="#st.odd"/> </td>
				<td> <s:property value="#st.even"/> </td>
			</tr>
	
		</s:iterator>
	</table>
</body>
</html>

Iterator标签还可以迭代key-value对的对象,例如Map对象,可在该标签的value属性中通过冒号(”:”)指定key-value对,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Iterator 标签实例</title>
</head>
<body>
	<table border="1">
	       <tr>
	       		<td>水果Id</td>
	       		<td>水果名称</td>
	       		
	       </tr>
		<s:iterator value="#{'1':'banana','2':'apple','3':'orange','4':'cherry'}" id="fruitName" status="st">
			<tr>
			    <td> <s:property value="key"/> </td>
				<td> <s:property value="value"/> </td>
			</tr>
	
		</s:iterator>
	</table>
</body>
</html>


2. If/elseif/else标签(if/elseif/else标签都是用来做分支控制的)实例代码如下:

        
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="true" %>
<%@ taglib prefix="s" uri="/struts-tags" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>if/elseif/else标签实例</title>
</head>
<body>
		<s:set name="score" value="87" />
		<s:if test='${score<60}'>
			您的分数小于60,不及格
		</s:if>
		<s:elseif test='${score<85}'>
			您的分数在60和85之间,良好
		</s:elseif>
		<s:else>
			您的分数在85分以上,优秀
		</s:else>
</body>
</html>
备注:struts2在2.0.11以后的版本都不支持el表达式,但在struts2标签之外使用el表达式是被允许的。


3. Append标签(将多小集合对象进行拼接,变成一个新的集合对象,append标签带有id属性,用来指明拼接后的对象的新集拿的id,多个集合对象通过其子标签<s:param/>指明) 代码如下:
      
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>append标签实例</title>
</head>
<body>
	<s:append id="totalFruitList">
		<s:param value="{'banana','apple','orange','cherry'}" id="fruitList1"/>
		<s:param value="{'香蕉','苹果','桔子','樱桃'}" id="fruitList2"/>
	</s:append>
	<table border="1">
	
		<tr>
			<td>水果名称</td>
		</tr>
	 <s:iterator value="#totalFruitList" id="fruitName">
		<tr>
			<td><s:property value="fruitName"/></td>
		</tr>
	</s:iterator>
	</table>
	
</body>
</html>


4.Generator标签(该标签用于将字符串通过分隔符分隔后,转换为一个集合)
     该标签包含的属性是:
(1)value:必填,指定需要被解析的字符串
            (2)separator:必填,分隔符,用于指定将字符串转换成集合所用的分隔符
            (3)count:可选,生成集合中元素的总数
(4)id:可选,若指定,则生成集合被放置到pageContext属性中 
    (5)converter:可选,指定一个转换器,将字符串转换为集合
(6)last:当前迭代元素是否为最后一个元素


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>generator标签实例</title>
</head>
<body>
	<s:generator  val="'banana,apple,orange,cherry'" separator="," id="fruits" count="3">
		<table border="1">
			<tr>
				<td>水果名称</td>
			</tr>
			<s:iterator>
			<tr>
				<td><s:property /></td>
			</tr>
			</s:iterator>
		</table>
	</s:generator>
</body>
</html>

5.sort标签(用于对指定的集合进行排序)

  该标签有两个属性:comparator(必填)和source(可选),前者用于指定用于排序的Comparator实例,后者用于指定需要进行排序的集合,若未指定,默认对ValueStack顶端的元素进行排序。


首先,要有一个自己的Comparator类:NumComparator,代码如下:

  
package amigo.struts.tag;

import java.util.Comparator;
/**
 *自定义的数字比较器 
 */
public class NumComparator implements Comparator {

	
	public int compare(Object element1, Object element2) {
		// TODO Auto-generated method stub
		int resultCode=0;
		int num1=Integer.parseInt(element1.toString());
		int num2=Integer.parseInt(element2.toString());
		if(num1>num2){
			resultCode=1;	
		}else if(num1<num2){
			resultCode=-1;
		}
		return resultCode;
	}

}
接下来在SortTag.jsp中使用sort标签,将comparator设置为该NumComparator实例,该jsp页面的完整代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>sort标签实例</title>
</head>
<body>
   <!-- 使用bean标签定义一个NumComparator实例 -->
		<s:bean id="numComparator" name="amigo.struts.tag.NumComparator"></s:bean>
		<table border="1">
			<tr>
				<td>数字排序</td>
			</tr>
			<s:sort source="{2,3,7,4,1,9,5,6,8}" comparator="numComparator">
			<s:iterator>
				<tr>
					<td><s:property /></td>
				</tr>
			</s:iterator>
			</s:sort>
		</table>
</body>
</html>



6.Merge标签(该标签与append标签类似,它也将多个集合拼接在一起,只是拼接方式不同而已,append标签是将第一个集合的元素全部拼接完成后,再开始拼接第二个集合中的所有元素,接着第三个,第四个…..而merge标签则是先拼接第一个集合、第二个集合、第三个集合…..中的第一个元素,接着开始)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>merge标签实例</title>
</head>
<body>
		<s:merge id="totaoFruitList">
			<s:param value="{'banana','apple','orange','cherry'}" id="fruitList1"></s:param>
			<s:param value="{'香蕉','苹果','桔子','樱桃'}" id="fruitList2"></s:param>
		</s:merge>
		
		<table border="1">
			<tr>
				<td>水果名称</td>
			</tr>
			<s:iterator value="#totaoFruitList" id="fruitName">
				<tr>
					<td><s:property value="#fruitName"/></td>
				</tr>
			</s:iterator>
		</table>
</body>
</html>

7.Subset标签(该标签用于取得集合的子集)
    它的具体属性是:
(1) source:可选,指定源集合,若没有指定,则取得ValueStack栈顶的集合。
(2) start:可选,指明从哪个元素开始抓取,第一个元素为0.
(3) Count:可选,子集中元素的个数,不指定时,取得从start开始的全部元素。
(4) Decider:可选,指定由开发者自己决定是否选中该元素。

  具体代码如下:

 
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>subset标签</title>
</head>
<body>
	<s:subset source="{'香蕉','苹果','桔子','樱桃','芒果','葡萄'}" start="2" count="2">
		<table border="1">
			<tr>
				<td>水果名称</td>
			</tr>
			<s:iterator>
				<tr>
					<td><s:property /></td>
				</tr>
			</s:iterator>
		</table>
	
	</s:subset>
</body>
</html>


二. 数据标签
      数据标签也是非ui标签,它主要用于提供各种数据访问的功能。常见的有:action标签、bean标签、set标签、property标签、param标签和url标签
    
1. 使用action标签
其属性包括:
name:必填,指定标签调用的Action
id:可选,指明该Actino的引用id
namespace:可选,指定该Action所在的namespace
executeResult:可选,指定是否将Action的处理结果返回到该页面,默认为false,即不将处理结果返回
ignoreContextParams:可选,表示是否将页面的请求参数传入到Action中,默认为false,即不忽略页面的请求参数。

(1) 建立Action类:ActionTagAction.java 代码如下:

         
  package amigo.struts.tag.dataTags;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Action标签的实例,在方法中验证用户名和密码
 * */
public class ActionTagAction extends ActionSupport {
		private static final long serialVersionUID=1L;
		/**用户名*/
		private String username;
		/**密码*/
		private String password;
		public String getUsername() {
			return username;
		}
		public void setUsername(String username) {
			this.username = username;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		
		public String execute(){
			ActionContext ctx = ActionContext.getContext();
			if(this.getUsername()!=null&&"amigo".equals(this.getUsername())&&this.getPassword()!=null&&"amigo".equals(this.getPassword())){
				return this.SUCCESS;
			}else{
				return this.ERROR;
			}
			
		}
}


(2) 编写登录成功页success.jsp

  
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录成功页面</title>
</head>
<body>
		登录成功
</body>
</html>
(3)编写登录失败页error.jsp

      
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录失败页面</title>
</head>
<body>
		登录失败
</body>
</html>

         (4)配置struts.xml
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<include file="struts-default.xml"/>
	<package name="amigo" extends="struts-default">
		<action name="Login" class="amigo.struts.tag.dataTags.ActionTagAction">
		<result name="success">/dataTags/success.jsp</result>
		<result name="error">/dataTags/error.jsp</result>
		</action>
	</package>
</struts>


           (5)编写action标签的实例页面actionTag.jsp
       
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>action标签实例</title>
</head>
<body>
		展示action 标签的使用<br/>
		(1)executeResult、ignoreContextParams都为false<br/>
		<s:action name="Login"></s:action><br/>
		
		(2)executeResult为true、ignoreContextParams为false<br/>
		<s:action name="Login" executeResult="true"></s:action><br/>
		
		(3)executeResult、ignoreContextParams都为true<br/>
		<s:action name="Login" executeResult="true" ignoreContextParams="true"></s:action>
</body>
</html>

   2.使用bean标签(该标签用于创建javaBean实例)
      Bean标签具有两个属性,即name和id,其中name为必填属性,指定要实例化的javaBean的实现类,而Id为可选属性,若指定了该属性,bean将会被放入StackContext中,在后续的代码中可以通过id来访问该JavaBean实例

      (1)编写javaBean类User.java
         
 package amigo.struts.tag.dataTags;
/**
 * 用户的JavaBean类
 * */
public class User {
     /** 用户名*/
	private String username;
	
	/** 密码*/
	private String password;
	
	/**性别*/
	private String gender;
	
	/**联系电话*/
	private String tel;
	
	/**Email*/
	private String email;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
}

          (2)编写bean标签的实例页面beanTag.jsp(注意:param标签的value值为字符串时,需要用单引号(’)将其括起来)
            
          
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>bean标签实例</title>
</head>
<body>
	<!-- bean标签指定id属性,用于后续用户信息的输出 注意:value里还需要单引号 -->
	<s:bean name="amigo.struts.tag.dataTags.User" id="user">
	<s:param name="username" value="'amigo'" ></s:param>
	<s:param name="password" value="'1234'" ></s:param>
	<s:param name="gender" value="'女'" ></s:param>
	<s:param name="tel" value="'13666666666'" ></s:param>
	<s:param name="email" value="'hhr1231@163.com'"></s:param>
	</s:bean>
	
	<!-- 输出用户信息 -->
	<table border="1" width="80%">
		<tr align="center">
			<td colspan="4">用户信息</td>
		</tr>
		
		<tr align="center">
			<td>用户名:</td>
			<td><s:property value="#user.username"/></td>
			<td>密码:</td>
			<td><s:property value="#user.password"/></td>
		</tr>
		
		<tr align="center">
			<td>性别:</td>
			<td><s:property value="#user.gender"/></td>
			<td>联系电话:</td>
			<td><s:property value="#user.tel"/></td>
		</tr>
		
		<tr align="center">
			<td>Email:</td>
			<td colspan="3"><s:property value="#user.email"/></td>
		</tr>
	</table>
	
</body>
</html>


    3 使用param标签  代码如下:

      
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>param标签实例</title>
</head>
<body>
		<!-- 嵌套在bean标签中使用 -->
	<s:bean name="amigo.struts.tag.dataTags.User" id="user">
	<s:param name="username" value="'amigo'"></s:param>
	</s:bean>	
	
	 嵌套在include标签中使用 <br/>
	<s:include value="success.jsp">
	<s:param name="username" value="'amigo'"></s:param>
	</s:include><br/>
	 嵌套在componen标签中使用<br/>
	  <ui:component>
	  	<ui:param name="username">amigo</ui:param><br/>
	  	<ui:param name="username">amigo2</ui:param><br/>
	  	<ui:param name="username">amigo3</ui:param>
	  </ui:component> 
</body>
</html>

4. 使用set标签(该标签用于将属性放在指定的范围内)
       具体属性如下:
Name:必填,生成的新变量的名字
Scope:可选,指明变量存放的范围,值可为action、page、request、session和application,默认在StackContext中
Value:可选,指定赋给变量的值,若没有该属性,则将ValueStack栈顶的值赋给新变量
Id:可选,指定引用该变量时的引用id


          
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>set标签实例</title>
</head>
<body>
	  <!-- bean标签指定id属性,用于后续用户信息的输出 -->
	  <s:bean name="amigo.struts.tag.dataTags.User" id="user">
	  	<s:param name="username" value="'amigo'"></s:param>
	  	<s:param name="password" value="'1234'"></s:param>
	  </s:bean>
	  未指定scope</br>
	  <s:set name="newUsr" value="#user" />
	  	<table border="1" width="80%">
	  		<tr align="center">
	  			<td>用户名:</td>
	  			<td><s:property value="#newUsr.username"/></td>
	  			<td>密码:</td>
	  			<td><s:property value="#newUsr.password"/></td>
	  		</tr>
	  	</table>
	  	
	  	指定scope为request<br/>
	  	<s:set name="newUsr" value="#user" scope="request"/>
	  		<table border="1" width="80%">
	  			<tr align="center">
	  				<td>用户名:</td>
	  				<td><s:property value="#attr.newUsr.username"/></td>
	  				<td>密码:</td>
	  				<td><s:property value="#attr.newUsr.password"/></td>
	  			</tr>
	  		</table>
	  	指定scope为request的另一和属性获取方式<br/>
	  	<table border="1" width="80%">
	  		<tr align="center">
	  			<td>用户名:</td>
	  			<td>${requestScope.newUsr.username}</td>
	  			<td>密码:</td>
	  			<td>${requestScope.newUsr.password}</td>
	  		</tr>
	  	</table>
	  
</body>
</html>


   5.使用property标签
   标签说明:
Default:可选,用于指定当属性为null时输出的值。
         Escape:可选,值可为true或false,用于指定是否escape HTML代码,默认为false
         Value:可选,指定需要输出的属性值,没有指定时,输出ValueStack栈顶的值。
         Id:可选,指定引用该元素时的引用id

      
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>property标签实例</title>
</head>
<body>
	<s:bean name="amigo.struts.tag.dataTags.User" id="user">
		<s:param name="username">amigo</s:param>
		<s:property value="%{username}"/><br/>
	</s:bean>
	
	<s:property value="username" default="默认名称"/><br/>
	获得Stack Context中的username:<s:property value="#user.username"/><br/>
	
	获得ValueStack中的fruitName:<br/>
	<s:iterator value="{'banana','apple','orange','cherry'}" id="fruitName">
		<s:property value="fruitName"/><br/>
	</s:iterator>
</body>
</html>

6.url标签(该标签用于输出URL地址,若需要在url后带参数,可使用param元素)
  标签说明:

        Id:指定元素的引用ID
        Anchor:指定url的锚点
    Encode:指定是否需要对请求参数进行编码
includeContext:指定是否将当前的上下文路径包含在URL地址中,默认为true,即包括上下文路径。
Value:指定生成url的地址值,若未指定时,使用action中的值
Action:指定生成的URL的地址为哪个Action,如果没有时,使用value作为其值。
Method:指定使用action的方法。
Namespace:指定命名空间
includeParams:指定是否包含请求的参数,值可为none、get或all
scheme:设置scheme


实例代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>url标签实例</title>
</head>
<body> 
		指定action属性,不指定value属性<br/>
		<s:url action="Login">
			<s:param name="username" value="'amigo'"></s:param>
			<s:param name="password" value="'amigo'"></s:param>
		</s:url>
		<br/>
		指定action属性和value属性时,优先value属性<br/>
		<s:url action="Login" value="success.jsp">
			<s:param name="username" value="'amigo'"></s:param>
			<s:param name="password" value="'amigo'"></s:param>
		</s:url>
		<br/>
		action属性和value属性都不指定时,链接到本页面<br/>
		<s:url>
				<s:param name="username" value="'阿蜜果'"></s:param>
			<s:param name="password" value="'amigo'"></s:param>
		</s:url>
</body>
</html>

7.Push标签(该标签用于将位于ValueStack的值放置到栈顶的值该标签包括value和id两个属性,其中value属性为必填属性,该属性指定了需要放到ValueStack栈顶的值,id表示该标签的引用ID)

实例:

  
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>push标签实例</title>
</head>
<body>
		<s:bean name="amigo.struts.tag.dataTags.User" id="user">
			<s:param name="username" value="'amigo'"></s:param>
			<s:param name="password" value="'1234'"></s:param>
		</s:bean>
		
		不使用push 时对属性的访问<br/>
		<table border="1" width="80%">
			<tr align="center">
				<td>用户名:</td>
				<td><s:property value="#user.username"/></td>
				<td>密码:</td>
				<td><s:property value="#user.password"/></td>
			</tr>
		</table>
		
		使用push 标签简化值的访问<br/>
		<s:push value="#user">
			<table border="1" width="80%">
				<tr align="center">
					<td>用户名:</td>
					<td><s:property value="username"/></td>
					<td>密码:</td>
					<td><s:property value="password"/></td>
				</tr>
			</table>
		
		</s:push>
		
		
</body>
</html>

8.Include标签

(1)编写include标签的实例页面includeTag.jsp
 
 <%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>include标签实例</title>
	</head>
	<body>
		<s:include value="includeFile.jsp" id="includeFile">
			<s:param name="username" value="'阿蜜果'"/>
			<s:param name="password" value="'1234'"/>
		</s:include>
	</body>
</html>

(2)编写被包含的页面includeFile.jsp

        
引用
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>include标签实例-被包括的文件</title>
</head>
<body>
<!-- 用JSP表达式语言输出参数信息 -->
用户名:${s.username}<br/>
密码:${param.password}<br/>
</body>
</html>

9.date标签(该标签用于格式化输出一个日期,还可以计算指定日期和当前日期的时差)
   属性如下:
      Name:必填,指定需要进行格式化的日期值
  Id:可选,指定引用该元素的ID
  Format:可选,指定特定的格式来格式化日期
      Nice:可选 ,值可为true或false,指定是否输出指定日期和当前时间之间的时差,默认false即不输出。

实例如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>data标签实例</title>
</head>
<body>
	<%
		java.util.Date now= new java.util.Date(2008-1900,7-1,13);
		pageContext.setAttribute("now",now);
	 %>
	 
	 当前日期,格式化为yyyy-MM-dd:<s:date name="#attr.now" format="yyyy-MM-dd"/><br/>
	 当前日期,未指定format属性:<s:date name="#attr.now" /><br/>
	 当前日期,指定了nice为true,未指定format:<s:date name="#attr.now" format="yyyy-MM-dd" nice="true"/><br/>
</body>
</html>

10.Debug标签(该标签用于辅助开发人员进行测试,它在页面上生成“[Debug]”的超级链接,点击后,开发人员可以查看Stack Context和ValueStack中的信息)

     实例如下:

 
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>debug标签实例</title>
</head>
<body>
		<s:debug></s:debug>
</body>
</html>


三. UI标签

1. 编写表单标签的实例页面formTag.jsp

   
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="true"%>
<%@ taglib prefix="s" uri="/struts-tags" %> 
<% 
	request.setAttribute("username","阿密果");
%>   

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>表单标签实例</title>
</head>
<body>
		<s:form action="Login.action" method="post">
			<s:textfield name="username" label="没带value值的文本框"></s:textfield>
			<s:textfield name="username" value="amigo" label="带value值的文本框"></s:textfield>
			<s:textfield name="username" value="${username}" label="value值通过el表达式获得"></s:textfield>
		
		</s:form>
</body>
</html>
备注:struts2在2.0.11以后的版本都不支持el表达式,但在struts2标签之外使用el表达式是被允许的。

2. 使用selecrt标签(该标签用于生成下拉列表,该标签的list属性可指定集合,这个集合用于生成下拉列表框的选项)
该标签的属性如下:
  List:指定集合,集合可以是map对象,还可以是对象的集合。
  Listkey:指定集合中的某个元素为复选框的value.
  listValue:指定集合元素中的某个属性作为复选框的标签。
  Multiple:设置列表框是否允许多选。
   
   具体实例代码如下:

(1)select 标签的实例页面selectTag.jsp

             <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>select 标签实例</title>
</head>
<body>
	<s:form action="testAction" method="post">
	<!-- 选项的值与显示的值一样的单选框 -->
		<s:select name="bookType" label="请选择图书类型" labelposition="top"
		list="{'计算机','社会科学','财经','文学','数学','升学考试'}" />
		
		
	<!-- 选项的值与显示的值不一样的单选框 -->
	<s:select name="bookType" label="请选择图书类型" labelposition="top"
	list="#{'1':'计算机','2':'社会科学','3':'财经','4':'文学','5':'数学','6':'升学考试'}" />	
	
	<!-- 可多选框 -->
	<s:select name="bookType" label="请选择图书类型" labelposition="top" multiple="true"
	list="{'计算机','社会科学','财经','文学','数学','升学考试'}" />
	
		<!-- 使用集合里放多个javaBean实例来生成下拉列表框 -->
	<s:bean name="amigo.struts.tag.uiTags.BookTypeService" id="typeService"/>
	<s:select name="bookType" label="请选择图书类型" labelposition="top" 
	list="#typeService.BookTypes" 
	listKey="typeId"
	listValue="typeName"
	/>
	
     </s:form>
</body>
</html>  

(2)BookType类的代码如下:

 
 package amigo.struts.tag.uiTags;

public class BookType {
	private String typeId;
	private String typeName;
	public String getTypeId() {
		return typeId;
	}
	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}
	public String getTypeName() {
		return typeName;
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	
	public BookType(String typeId,String typeName){
		  this.typeId=typeId;
		  this.typeName=typeName;
		
	}
	
}

BookTypeService类的代码如下:

package amigo.struts.tag.uiTags;

public class BookTypeService {
		public BookType[] getBookTypes(){
			return new BookType[] {
					new BookType("1","计算机"),
					new BookType("2","社会科学"),
					new BookType("3","财经")
					
			};
			
		}
}

3. 使用radio标签(该标签用于生成单选框,与HTML的radio标签作用相同,它与select标签的属性相类似)
实例代码如下:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
	<head>
		<title>radio标签实例</title>
	</head>
	<body>
		<s:form action="testAction" method="post">
			<!-- 选项的值与显示值一样的单选框: -->
			<s:radio name="bookType" 
				label="请选择图书类型" 
				labelposition="top"
				list="{'计算机', '社会科学', '财经', '文学', '数学', '升学考试'}" />
			
			<!-- 选项的值与显示值不一样的单选框: -->
			<s:radio name="bookType" 
				label="请选择图书类型" 
				labelposition="top"
				list="#{'1':'计算机', '2':'社会科学', '3':'财经', '4':'文学', '5':'数学', '6': '升学考试'}" />
			
			<!-- 使用集合里放多个JavaBean实例来生成单选框: -->
			<s:bean name="amigo.struts.tag.uiTags.BookTypeService" id="typeService"/>
			<s:radio name="bookType" 
				label="请选择图书类型" 
				labelposition="top"
				list="#typeService.bookTypes" 
				listKey="typeId"
				listValue="typeName" />
		</s:form>
	</body>
</html>
4. 使用checkboxlist标签(该标签可以一次创建多个复选框列表,它的属性与单选框类似)
具体实例如下:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
	<head>
		<title>checkboxlist标签实例</title>
	</head>
	<body>
		<s:form action="testAction" method="post">
			<!-- 选项的值与显示值一样的复选框: -->
			<s:checkboxlist name="bookType" 
				label="请选择图书类型" 
				labelposition="top"
				list="{'计算机', '社会科学', '财经', '文学', '数学', '升学考试'}" />
			
			<!-- 选项的值与显示值不一样的复选框: -->
			<s:checkboxlist name="bookType" 
				label="请选择图书类型" 
				labelposition="top"
				list="#{'1':'计算机', '2':'社会科学', '3':'财经', '4':'文学', '5':'数学', '6': '升学考试'}" />
			
			<!-- 使用集合里放多个JavaBean实例来生成复选框: -->
			<s:bean name="amigo.struts.tag.uiTags.BookTypeService" id="typeService"/>
			<s:checkboxlist name="bookType" 
				label="请选择图书类型" 
				labelposition="top"
				list="#typeService.bookTypes" 
				listKey="typeId"
				listValue="typeName" />
		</s:form>
	</body>
</html>
5. 使用combobox标签(该标签用于生成一个单行文本框和下拉列表框的组合,并且两个表单元素对应一个请求参数,但是只有单行文本框中才包含请求参数,而下拉列表框只用于辅助输入,并没有name属性,当然也不会产生请求参数。)
具体代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>combobox标签实例</title>
</head>
<body>
	<s:form action="testAction" method="post">
		<s:combobox 
			name="bookType"
			label="请选择图书类型"
			labelposition="top"
			list="{'计算机','社会科学','财经','文学','数学','升学考试'}"/>
	</s:form>
</body>
</html>

6. 使用doubleselect标签(该标签会生成一个级联列表框)
具体属性如下:
Name:指定第一个下拉列表框的name属性
List:指定用于输出第一个下拉列表框的选项的集合。
listKey:与select等标签的该属性类似,但它是指第一个下拉列表框的。
llistValue: 与select等标签的该属性类似,但它是指第一个下拉列表框的。
doubleName: 指定第二个下拉列表框的name属性
doubleList: 指定用于输出第二个下拉列表框的选项的集合。
doubleList Key: 与select等标签的listKey属性类似,但它是指第二个下拉列表框的。
doubleList Value: 与select等标签的listValue属性类似,但它是指第二个下拉列表框的。


具体实例代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>doubleselect标签实例</title>
</head>
<body>
	<s:form action="testAction" name="form0" method="post">
		<s:set name="bookTypes" value="#{'计算机':{'Java','C#','struts2'},
		'文学':{'言情小说','恐怖','散文','历史小学'},
		'升学考试':{'考研','考博','高考'}}">
		</s:set>
		<s:doubleselect label="请选择类别"
			name="bookType"
			list="#bookTypes.keySet()" 
			doubleName="subBookType" 
			doubleList="#bookTypes[top]"></s:doubleselect>
	</s:form>
</body>
</html>

备注:在使用该标签时,记住要将标签放在<s:form../>标签中使用,并记住要为<s:form../>标签指定name属性,若没指定,运行该页面会报错。

7. 使用datetimepicker标签(该标签用于生成一个日期、时间下拉列表框,当选择好日期或时间后,选中的日期或时间的值将会自动放入文本框。)具体属性如下:
displayFormat:该属性指定日期的显示格式
displayWeeks:指定日历能显示星期数。
startDate:指定最开始使用的日期。
endDate:指定日期集的最后可用日期。
formatLength:指定日期显示的格式
language:指定日期显示的locale,中文时为zh_CN
toggleDuration:指定日期选择框出现、隐藏的切换时间。
toggleType:指定日期选择框出现隐藏的方式。
Type:指定日期选择框的类型
Value:指定当前的日期
weeksStartsOn:指定日期选择框中哪天才是一周的第一天

实例代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>datetimepicker标签实例</title>
<s:head/>
</head>
<body>
	<s:form action="test" theme="simple" method="POST"> 
	日期选择,使用默认的displayFormat(默认为yyyy-MM-dd),指定toggleType(默认为plain)和value属性(值为today,即今天):<br>
	<s:datetimepicker name="birthDate" label="出生日期" toggleType="fade" value="today"/><hr>
	日期选择,指定了format属性<br>
	<s:datetimepicker name="birthDate" label="出生日期" displayFormat="yyyy/MM/dd"/><hr>
	日期选择,指定了weekStartsOn属性(不指定时默认为0,即从周日开始)<br>
	<s:datetimepicker name="birthDate" label="出生日期" weekStartsOn="1"/><hr>
	
	时间选择(设置type为time)<br>
	<s:datetimepicker name="start" label="选择出发时间" type="time"  /><hr>
	
	</s:form>
</body>
</html>
备注:在使用datetimepicker时需要<s:head/>加上,若不加上的话,打开该页面时会报javascript错误

8. 使用optiontransferselect标签(该标签会创建两个下拉列表,会生成两个<select../>标签,两个下拉列表中的元素能相互移动,提交表单的时候,两个<select../>标签的值都会提交)具体属性如下:
    addAllToleftLable:设置全部移动到左边按钮上的文本
   addAllToRightLable:设置全部移动到右边按钮上的文本
   addToLefLable:设置向左移动按钮的文本
   addToRightLable: 设置向右移动按钮的文本
   allowAddAllToLeft:设置是否允许出现全部左移的按钮
   allowAddAllToRight: 设置是否允许出现全部右移的按钮
   leftTitle:设置左边列表框的标题
rightTitle:设置右边列表框的标题
allowSelectAll:设置是否允许出现全部选择的按钮
selectAllLabel:设置全部选择按钮上的文本
name:设置第一个下拉选择框的name属性
value: 设置第一个下拉选择框的value属性
list:指定第一个下拉列表的集合
listKey:设置创建第一个下拉选择框的选项的value的属性
listValue: 设置创建第一个下拉选择框的选项的label的属性
multiple:设置第一个下拉列表框是否允许多选
doubleName: 设置第二个下拉选择框的name属性
doubleValue: 设置第二个下拉选择框的value属性
doubleList: 指定第二个下拉列表的集合
doubleListKey: 设置创建第二个下拉选择框的选项的value的属性
doubleListValue: 设置创建第二个下拉选择框的选项的label的属性
doubleMultiple: 设置第二个下拉列表框是否允许多选

具体代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>optiontransferselect标签实例</title>
<s:head/>
</head>
<body>
	<s:form>
		<!-- 使用简单集合对象来生成可移动的下拉列表框 -->
		<s:optiontransferselect
		 label="请选择你喜欢的图书类型"
		 name="bookType"
		 leftTitle="--所有图书--"
		 list="{'计算机','社会科学','财经','文学','数学'}"
		 multiple="true"
		 addToLeftLabel="左移"
		 selectAllLabel="全部选择"
		 addToRightLabel="右移"
		 rightTitle="--喜欢图书--"
		 doubleList="{'散文','历史'}" 
		 doubleName="loveBookType" 
		 doubleMultiple="true" />
	</s:form>
</body>
</html>

9. 使用optgroup标签(该标签用于生成一个下拉列表框的选项组,因此,它需要放在<s:select../>标签中使用,可以在一个<s:select../>标签中添加多个<s:optgroup../>标签,该标签的属性与<s:select../>标签类似)
具体实例代码如下:

 
  <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>optgroup实例</title>
</head>
<body>
		<s:form action="testAction" method="post">
			<s:select label="选择您喜欢的图书类型"
				name="bookType"
				list="#{'1':'计算机','2':'社会科学','3':'财经','4':'数学'}"
				listKey="key"
				listValue="value">
				<s:optgroup label="小说"
				list="#{'5':'言情小说','6':'武打小说','7':'恐怖小说'}"
				listKey="key"
				listValue="value" />
			    <s:optgroup label="升学考试"
			    list="#{'8':'考研','9':'考博','10':'高考'}"
			    listKey="key"
			    listValue="value"/>
			
			</s:select>
		</s:form>
</body>
</html>

10. 使用updownselect标签(该标签的作用与select标签类似,常用属性为list、listKey和listValue等,不过它与select标签的不同之处在于:它支持选项的上下移动,针对这个特点)
          其它的具本属性:
          allowMoveUp:是否显示向上移动的按钮,默认为true
  allowMoveDown: 是否显示向下移动的按钮,默认为true
          allowSelectAll:是否显示全选按钮,默认为true
          moveUpLabel:向上移动的按钮的文本
          moveDownLabel: 向下移动的按钮的文本
  selectAllLabel:全选按钮的文本


具体代码如下:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
	<head>
		<title>updownselect标签实例</title>
		<s:head/>
	</head>
	<body>
		<s:form>
			<!-- 使用简单集合来生成可上下移动选项的下拉选择框 -->
			<s:updownselect name="bookType1" label="请选择图书类型" 
				labelposition="top"
				list="{'计算机', '社会科学', '财经', '文学', '数学', '升学考试'}" />
				
			<!-- 使用简单Map对象来生成可上下移动选项的下拉选择框,且使用emptyOption="true"增加一个空选项-->
			<s:updownselect name="bookType2" label="请选择图书类型"
				labelposition="top"
				moveUpLabel="上移"
				moveDownLabel="下移"
				list="#{'1':'计算机', '2':'社会科学', '3':'财经', '4':'文学', '5':'数学', '6': '升学考试'}"
				listKey="key"
				listValue="value"
				emptyOption="true" />
			
			<!-- 使用集合里放多个JavaBean实例来生成下拉列表框: -->
			<s:bean name="amigo.struts.tag.uiTags.BookTypeService" id="typeService"/>
			<s:updownselect name="bookType3" label="请选择图书类型"
				labelposition="top"
				selectAllLabel="全选"
				multiple="true"
				list="#typeService.bookTypes" 
				listKey="typeId"
				listValue="typeName" />
		</s:form>
	</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics