首页 新闻 论坛 问答 博客 招聘 更多 ▼
专栏 文摘 圈子 搜索 您还未登录 ! 我的应用 登录 注册 hhr-michael永久域名 http://hhr-michael.javaeye.com.struts2+hibernate3+spring2读书笔记8(OG ... | struts2+hibernate3+spring2读书笔记6( ... 2010-06-08
struts2+hibernate3+spring2读书笔记7(Struts 2 标签库)
文章分类:Java编程
第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(水果名),完整代码如下:
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>Iterator 标签实例</title>
9.</head>
10.<body>
11. <table border="1">
12. <s:iterator value="{'banana','apple','orange','cherry'}" id="fruitName">
13. <tr>
14. <td>
15. <s:property value="fruitName"/>
16. </td>
17. </tr>
18.
19. </s:iterator>
20. </table>
21.</body>
22.</html>
<%@ 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的各属性信息,修改后的代码如下所示:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>Iterator 标签实例</title>
9.</head>
10.<body>
11. <table border="1">
12. <tr>
13. <td>索引</td>
14. <td>水果名称</td>
15. <td>是否为第一个</td>
16. <td>是否为最后一个</td>
17. <td>是否为奇数</td>
18. <td>是否为偶数</td>
19. </tr>
20. <s:iterator value="{'banana','apple','orange','cherry'}" id="fruitName" status="st">
21. <tr>
22. <td> <s:property value="#st.index"/> </td>
23. <td> <s:property value="fruitName"/> </td>
24. <td> <s:property value="#st.first"/> </td>
25. <td> <s:property value="#st.last"/> </td>
26. <td> <s:property value="#st.odd"/> </td>
27. <td> <s:property value="#st.even"/> </td>
28. </tr>
29.
30. </s:iterator>
31. </table>
32.</body>
33.</html>
<%@ 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对,代码如下:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>Iterator 标签实例</title>
9.</head>
10.<body>
11. <table border="1">
12. <tr>
13. <td>水果Id</td>
14. <td>水果名称</td>
15.
16. </tr>
17. <s:iterator value="#{'1':'banana','2':'apple','3':'orange','4':'cherry'}" id="fruitName" status="st">
18. <tr>
19. <td> <s:property value="key"/> </td>
20. <td> <s:property value="value"/> </td>
21. </tr>
22.
23. </s:iterator>
24. </table>
25.</body>
26.</html>
<%@ 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标签都是用来做分支控制的)实例代码如下:
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8" isELIgnored="true" %>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.
5.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
6.<html>
7.<head>
8.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
9.<title>if/elseif/else标签实例</title>
10.</head>
11.<body>
12. <s:set name="score" value="87" />
13. <s:if test='${score<60}'>
14. 您的分数小于60,不及格
15. </s:if>
16. <s:elseif test='${score<85}'>
17. 您的分数在60和85之间,良好
18. </s:elseif>
19. <s:else>
20. 您的分数在85分以上,优秀
21. </s:else>
22.</body>
23.</html>
<%@ 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/>指明) 代码如下:
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>append标签实例</title>
9.</head>
10.<body>
11. <s:append id="totalFruitList">
12. <s:param value="{'banana','apple','orange','cherry'}" id="fruitList1"/>
13. <s:param value="{'香蕉','苹果','桔子','樱桃'}" id="fruitList2"/>
14. </s:append>
15. <table border="1">
16.
17. <tr>
18. <td>水果名称</td>
19. </tr>
20. <s:iterator value="#totalFruitList" id="fruitName">
21. <tr>
22. <td><s:property value="fruitName"/></td>
23. </tr>
24. </s:iterator>
25. </table>
26.
27.</body>
28.</html>
<%@ 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:当前迭代元素是否为最后一个元素
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>generator标签实例</title>
9.</head>
10.<body>
11. <s:generator val="'banana,apple,orange,cherry'" separator="," id="fruits" count="3">
12. <table border="1">
13. <tr>
14. <td>水果名称</td>
15. </tr>
16. <s:iterator>
17. <tr>
18. <td><s:property /></td>
19. </tr>
20. </s:iterator>
21. </table>
22. </s:generator>
23.</body>
24.</html>
<%@ 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,代码如下:
Java代码
1.package amigo.struts.tag;
2.
3.import java.util.Comparator;
4./**
5. *自定义的数字比较器
6. */
7.public class NumComparator implements Comparator {
8.
9.
10. public int compare(Object element1, Object element2) {
11. // TODO Auto-generated method stub
12. int resultCode=0;
13. int num1=Integer.parseInt(element1.toString());
14. int num2=Integer.parseInt(element2.toString());
15. if(num1>num2){
16. resultCode=1;
17. }else if(num1<num2){
18. resultCode=-1;
19. }
20. return resultCode;
21. }
22.
23.}
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页面的完整代码如下所示:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>sort标签实例</title>
9.</head>
10.<body>
11. <!-- 使用bean标签定义一个NumComparator实例 -->
12. <s:bean id="numComparator" name="amigo.struts.tag.NumComparator"></s:bean>
13. <table border="1">
14. <tr>
15. <td>数字排序</td>
16. </tr>
17. <s:sort source="{2,3,7,4,1,9,5,6,8}" comparator="numComparator">
18. <s:iterator>
19. <tr>
20. <td><s:property /></td>
21. </tr>
22. </s:iterator>
23. </s:sort>
24. </table>
25.</body>
26.</html>
<%@ 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标签则是先拼接第一个集合、第二个集合、第三个集合…..中的第一个元素,接着开始)
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>merge标签实例</title>
9.</head>
10.<body>
11. <s:merge id="totaoFruitList">
12. <s:param value="{'banana','apple','orange','cherry'}" id="fruitList1"></s:param>
13. <s:param value="{'香蕉','苹果','桔子','樱桃'}" id="fruitList2"></s:param>
14. </s:merge>
15.
16. <table border="1">
17. <tr>
18. <td>水果名称</td>
19. </tr>
20. <s:iterator value="#totaoFruitList" id="fruitName">
21. <tr>
22. <td><s:property value="#fruitName"/></td>
23. </tr>
24. </s:iterator>
25. </table>
26.</body>
27.</html>
<%@ 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:可选,指定由开发者自己决定是否选中该元素。
具体代码如下:
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>subset标签</title>
9.</head>
10.<body>
11. <s:subset source="{'香蕉','苹果','桔子','樱桃','芒果','葡萄'}" start="2" count="2">
12. <table border="1">
13. <tr>
14. <td>水果名称</td>
15. </tr>
16. <s:iterator>
17. <tr>
18. <td><s:property /></td>
19. </tr>
20. </s:iterator>
21. </table>
22.
23. </s:subset>
24.</body>
25.</html>
<%@ 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 代码如下:
Java代码
1. package amigo.struts.tag.dataTags;
2.
3.import com.opensymphony.xwork2.ActionContext;
4.import com.opensymphony.xwork2.ActionSupport;
5.
6./**
7. * Action标签的实例,在方法中验证用户名和密码
8. * */
9.public class ActionTagAction extends ActionSupport {
10. private static final long serialVersionUID=1L;
11. /**用户名*/
12. private String username;
13. /**密码*/
14. private String password;
15. public String getUsername() {
16. return username;
17. }
18. public void setUsername(String username) {
19. this.username = username;
20. }
21. public String getPassword() {
22. return password;
23. }
24. public void setPassword(String password) {
25. this.password = password;
26. }
27.
28. public String execute(){
29. ActionContext ctx = ActionContext.getContext();
30. if(this.getUsername()!=null&&"amigo".equals(this.getUsername())&&this.getPassword()!=null&&"amigo".equals(this.getPassword())){
31. return this.SUCCESS;
32. }else{
33. return this.ERROR;
34. }
35.
36. }
37.}
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
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4.<html>
5.<head>
6.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7.<title>登录成功页面</title>
8.</head>
9.<body>
10. 登录成功
11.</body>
12.</html>
<%@ 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
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4.<html>
5.<head>
6.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7.<title>登录失败页面</title>
8.</head>
9.<body>
10. 登录失败
11.</body>
12.</html>
<%@ 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
Java代码
1.<!DOCTYPE struts PUBLIC
2."-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
3."http://struts.apache.org/dtds/struts-2.0.dtd">
4.
5.<struts>
6. <include file="struts-default.xml"/>
7. <package name="amigo" extends="struts-default">
8. <action name="Login" class="amigo.struts.tag.dataTags.ActionTagAction">
9. <result name="success">/dataTags/success.jsp</result>
10. <result name="error">/dataTags/error.jsp</result>
11. </action>
12. </package>
13.</struts>
<!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
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>action标签实例</title>
9.</head>
10.<body>
11. 展示action 标签的使用<br/>
12. (1)executeResult、ignoreContextParams都为false<br/>
13. <s:action name="Login"></s:action><br/>
14.
15. (2)executeResult为true、ignoreContextParams为false<br/>
16. <s:action name="Login" executeResult="true"></s:action><br/>
17.
18. (3)executeResult、ignoreContextParams都为true<br/>
19. <s:action name="Login" executeResult="true" ignoreContextParams="true"></s:action>
20.</body>
21.</html>
<%@ 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
Java代码
1. package amigo.struts.tag.dataTags;
2./**
3. * 用户的JavaBean类
4. * */
5.public class User {
6. /** 用户名*/
7. private String username;
8.
9. /** 密码*/
10. private String password;
11.
12. /**性别*/
13. private String gender;
14.
15. /**联系电话*/
16. private String tel;
17.
18. /**Email*/
19. private String email;
20.
21. public String getUsername() {
22. return username;
23. }
24.
25. public void setUsername(String username) {
26. this.username = username;
27. }
28.
29. public String getPassword() {
30. return password;
31. }
32.
33. public void setPassword(String password) {
34. this.password = password;
35. }
36.
37. public String getGender() {
38. return gender;
39. }
40.
41. public void setGender(String gender) {
42. this.gender = gender;
43. }
44.
45. public String getTel() {
46. return tel;
47. }
48.
49. public void setTel(String tel) {
50. this.tel = tel;
51. }
52.
53. public String getEmail() {
54. return email;
55. }
56.
57. public void setEmail(String email) {
58. this.email = email;
59. }
60.}
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值为字符串时,需要用单引号(’)将其括起来)
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>bean标签实例</title>
9.</head>
10.<body>
11. <!-- bean标签指定id属性,用于后续用户信息的输出 注意:value里还需要单引号 -->
12. <s:bean name="amigo.struts.tag.dataTags.User" id="user">
13. <s:param name="username" value="'amigo'" ></s:param>
14. <s:param name="password" value="'1234'" ></s:param>
15. <s:param name="gender" value="'女'" ></s:param>
16. <s:param name="tel" value="'13666666666'" ></s:param>
17. <s:param name="email" value="'hhr1231@163.com'"></s:param>
18. </s:bean>
19.
20. <!-- 输出用户信息 -->
21. <table border="1" width="80%">
22. <tr align="center">
23. <td colspan="4">用户信息</td>
24. </tr>
25.
26. <tr align="center">
27. <td>用户名:</td>
28. <td><s:property value="#user.username"/></td>
29. <td>密码:</td>
30. <td><s:property value="#user.password"/></td>
31. </tr>
32.
33. <tr align="center">
34. <td>性别:</td>
35. <td><s:property value="#user.gender"/></td>
36. <td>联系电话:</td>
37. <td><s:property value="#user.tel"/></td>
38. </tr>
39.
40. <tr align="center">
41. <td>Email:</td>
42. <td colspan="3"><s:property value="#user.email"/></td>
43. </tr>
44. </table>
45.
46.</body>
47.</html>
<%@ 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标签 代码如下:
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>param标签实例</title>
9.</head>
10.<body>
11. <!-- 嵌套在bean标签中使用 -->
12. <s:bean name="amigo.struts.tag.dataTags.User" id="user">
13. <s:param name="username" value="'amigo'"></s:param>
14. </s:bean>
15.
16. 嵌套在include标签中使用 <br/>
17. <s:include value="success.jsp">
18. <s:param name="username" value="'amigo'"></s:param>
19. </s:include><br/>
20. 嵌套在componen标签中使用<br/>
21. <ui:component>
22. <ui:param name="username">amigo</ui:param><br/>
23. <ui:param name="username">amigo2</ui:param><br/>
24. <ui:param name="username">amigo3</ui:param>
25. </ui:component>
26.</body>
27.</html>
<%@ 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
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8" %>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>set标签实例</title>
9.</head>
10.<body>
11. <!-- bean标签指定id属性,用于后续用户信息的输出 -->
12. <s:bean name="amigo.struts.tag.dataTags.User" id="user">
13. <s:param name="username" value="'amigo'"></s:param>
14. <s:param name="password" value="'1234'"></s:param>
15. </s:bean>
16. 未指定scope</br>
17. <s:set name="newUsr" value="#user" />
18. <table border="1" width="80%">
19. <tr align="center">
20. <td>用户名:</td>
21. <td><s:property value="#newUsr.username"/></td>
22. <td>密码:</td>
23. <td><s:property value="#newUsr.password"/></td>
24. </tr>
25. </table>
26.
27. 指定scope为request<br/>
28. <s:set name="newUsr" value="#user" scope="request"/>
29. <table border="1" width="80%">
30. <tr align="center">
31. <td>用户名:</td>
32. <td><s:property value="#attr.newUsr.username"/></td>
33. <td>密码:</td>
34. <td><s:property value="#attr.newUsr.password"/></td>
35. </tr>
36. </table>
37. 指定scope为request的另一和属性获取方式<br/>
38. <table border="1" width="80%">
39. <tr align="center">
40. <td>用户名:</td>
41. <td>${requestScope.newUsr.username}</td>
42. <td>密码:</td>
43. <td>${requestScope.newUsr.password}</td>
44. </tr>
45. </table>
46.
47.</body>
48.</html>
<%@ 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
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>property标签实例</title>
9.</head>
10.<body>
11. <s:bean name="amigo.struts.tag.dataTags.User" id="user">
12. <s:param name="username">amigo</s:param>
13. <s:property value="%{username}"/><br/>
14. </s:bean>
15.
16. <s:property value="username" default="默认名称"/><br/>
17. 获得Stack Context中的username:<s:property value="#user.username"/><br/>
18.
19. 获得ValueStack中的fruitName:<br/>
20. <s:iterator value="{'banana','apple','orange','cherry'}" id="fruitName">
21. <s:property value="fruitName"/><br/>
22. </s:iterator>
23.</body>
24.</html>
<%@ 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
实例代码:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>url标签实例</title>
9.</head>
10.<body>
11. 指定action属性,不指定value属性<br/>
12. <s:url action="Login">
13. <s:param name="username" value="'amigo'"></s:param>
14. <s:param name="password" value="'amigo'"></s:param>
15. </s:url>
16. <br/>
17. 指定action属性和value属性时,优先value属性<br/>
18. <s:url action="Login" value="success.jsp">
19. <s:param name="username" value="'amigo'"></s:param>
20. <s:param name="password" value="'amigo'"></s:param>
21. </s:url>
22. <br/>
23. action属性和value属性都不指定时,链接到本页面<br/>
24. <s:url>
25. <s:param name="username" value="'阿蜜果'"></s:param>
26. <s:param name="password" value="'amigo'"></s:param>
27. </s:url>
28.</body>
29.</html>
<%@ 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)
实例:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>push标签实例</title>
9.</head>
10.<body>
11. <s:bean name="amigo.struts.tag.dataTags.User" id="user">
12. <s:param name="username" value="'amigo'"></s:param>
13. <s:param name="password" value="'1234'"></s:param>
14. </s:bean>
15.
16. 不使用push 时对属性的访问<br/>
17. <table border="1" width="80%">
18. <tr align="center">
19. <td>用户名:</td>
20. <td><s:property value="#user.username"/></td>
21. <td>密码:</td>
22. <td><s:property value="#user.password"/></td>
23. </tr>
24. </table>
25.
26. 使用push 标签简化值的访问<br/>
27. <s:push value="#user">
28. <table border="1" width="80%">
29. <tr align="center">
30. <td>用户名:</td>
31. <td><s:property value="username"/></td>
32. <td>密码:</td>
33. <td><s:property value="password"/></td>
34. </tr>
35. </table>
36.
37. </s:push>
38.
39.
40.</body>
41.</html>
<%@ 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
Java代码
1. <%@ page contentType="text/html; charset=UTF-8" %>
2.<%@ taglib prefix="s" uri="/struts-tags" %>
3.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4.<html>
5. <head>
6. <title>include标签实例</title>
7. </head>
8. <body>
9. <s:include value="includeFile.jsp" id="includeFile">
10. <s:param name="username" value="'阿蜜果'"/>
11. <s:param name="password" value="'1234'"/>
12. </s:include>
13. </body>
14.</html>
<%@ 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即不输出。
实例如下:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>data标签实例</title>
9.</head>
10.<body>
11. <%
12. java.util.Date now= new java.util.Date(2008-1900,7-1,13);
13. pageContext.setAttribute("now",now);
14. %>
15.
16. 当前日期,格式化为yyyy-MM-dd:<s:date name="#attr.now" format="yyyy-MM-dd"/><br/>
17. 当前日期,未指定format属性:<s:date name="#attr.now" /><br/>
18. 当前日期,指定了nice为true,未指定format:<s:date name="#attr.now" format="yyyy-MM-dd" nice="true"/><br/>
19.</body>
20.</html>
<%@ 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中的信息)
实例如下:
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>debug标签实例</title>
9.</head>
10.<body>
11. <s:debug></s:debug>
12.</body>
13.</html>
<%@ 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
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8" isELIgnored="true"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<%
5. request.setAttribute("username","阿密果");
6.%>
7.
8.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
9.<html>
10.<head>
11.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12.<title>表单标签实例</title>
13.</head>
14.<body>
15. <s:form action="Login.action" method="post">
16. <s:textfield name="username" label="没带value值的文本框"></s:textfield>
17. <s:textfield name="username" value="amigo" label="带value值的文本框"></s:textfield>
18. <s:textfield name="username" value="${username}" label="value值通过el表达式获得"></s:textfield>
19.
20. </s:form>
21.</body>
22.</html>
<%@ 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:设置列表框是否允许多选。
具体实例代码如下:
Java代码
1.(1)select 标签的实例页面selectTag.jsp
2.
3. <%@ page language="java" contentType="text/html; charset=UTF-8"
4. pageEncoding="UTF-8"%>
5.<%@ taglib prefix="s" uri="/struts-tags" %>
6.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
7.<html>
8.<head>
9.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10.<title>select 标签实例</title>
11.</head>
12.<body>
13. <s:form action="testAction" method="post">
14. <!-- 选项的值与显示的值一样的单选框 -->
15. <s:select name="bookType" label="请选择图书类型" labelposition="top"
16. list="{'计算机','社会科学','财经','文学','数学','升学考试'}" />
17.
18.
19. <!-- 选项的值与显示的值不一样的单选框 -->
20. <s:select name="bookType" label="请选择图书类型" labelposition="top"
21. list="#{'1':'计算机','2':'社会科学','3':'财经','4':'文学','5':'数学','6':'升学考试'}" />
22.
23. <!-- 可多选框 -->
24. <s:select name="bookType" label="请选择图书类型" labelposition="top" multiple="true"
25. list="{'计算机','社会科学','财经','文学','数学','升学考试'}" />
26.
27. <!-- 使用集合里放多个javaBean实例来生成下拉列表框 -->
28. <s:bean name="amigo.struts.tag.uiTags.BookTypeService" id="typeService"/>
29. <s:select name="bookType" label="请选择图书类型" labelposition="top"
30. list="#typeService.BookTypes"
31. listKey="typeId"
32. listValue="typeName"
33. />
34.
35. </s:form>
36.</body>
37.</html>
(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类的代码如下:
Java代码
1. package amigo.struts.tag.uiTags;
2.
3.public class BookType {
4. private String typeId;
5. private String typeName;
6. public String getTypeId() {
7. return typeId;
8. }
9. public void setTypeId(String typeId) {
10. this.typeId = typeId;
11. }
12. public String getTypeName() {
13. return typeName;
14. }
15. public void setTypeName(String typeName) {
16. this.typeName = typeName;
17. }
18.
19. public BookType(String typeId,String typeName){
20. this.typeId=typeId;
21. this.typeName=typeName;
22.
23. }
24.
25.}
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类的代码如下:
Java代码
1.package amigo.struts.tag.uiTags;
2.
3.public class BookTypeService {
4. public BookType[] getBookTypes(){
5. return new BookType[] {
6. new BookType("1","计算机"),
7. new BookType("2","社会科学"),
8. new BookType("3","财经")
9.
10. };
11.
12. }
13.}
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标签的属性相类似)
实例代码如下:
Java代码
1.<%@ page contentType="text/html; charset=UTF-8" %>
2.<%@ taglib prefix="s" uri="/struts-tags" %>
3.
4.<html>
5. <head>
6. <title>radio标签实例</title>
7. </head>
8. <body>
9. <s:form action="testAction" method="post">
10. <!-- 选项的值与显示值一样的单选框: -->
11. <s:radio name="bookType"
12. label="请选择图书类型"
13. labelposition="top"
14. list="{'计算机', '社会科学', '财经', '文学', '数学', '升学考试'}" />
15.
16. <!-- 选项的值与显示值不一样的单选框: -->
17. <s:radio name="bookType"
18. label="请选择图书类型"
19. labelposition="top"
20. list="#{'1':'计算机', '2':'社会科学', '3':'财经', '4':'文学', '5':'数学', '6': '升学考试'}" />
21.
22. <!-- 使用集合里放多个JavaBean实例来生成单选框: -->
23. <s:bean name="amigo.struts.tag.uiTags.BookTypeService" id="typeService"/>
24. <s:radio name="bookType"
25. label="请选择图书类型"
26. labelposition="top"
27. list="#typeService.bookTypes"
28. listKey="typeId"
29. listValue="typeName" />
30. </s:form>
31. </body>
32.</html>
<%@ 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标签(该标签可以一次创建多个复选框列表,它的属性与单选框类似)
具体实例如下:
Java代码
1.<%@ page contentType="text/html; charset=UTF-8" %>
2.<%@ taglib prefix="s" uri="/struts-tags" %>
3.
4.<html>
5. <head>
6. <title>checkboxlist标签实例</title>
7. </head>
8. <body>
9. <s:form action="testAction" method="post">
10. <!-- 选项的值与显示值一样的复选框: -->
11. <s:checkboxlist name="bookType"
12. label="请选择图书类型"
13. labelposition="top"
14. list="{'计算机', '社会科学', '财经', '文学', '数学', '升学考试'}" />
15.
16. <!-- 选项的值与显示值不一样的复选框: -->
17. <s:checkboxlist name="bookType"
18. label="请选择图书类型"
19. labelposition="top"
20. list="#{'1':'计算机', '2':'社会科学', '3':'财经', '4':'文学', '5':'数学', '6': '升学考试'}" />
21.
22. <!-- 使用集合里放多个JavaBean实例来生成复选框: -->
23. <s:bean name="amigo.struts.tag.uiTags.BookTypeService" id="typeService"/>
24. <s:checkboxlist name="bookType"
25. label="请选择图书类型"
26. labelposition="top"
27. list="#typeService.bookTypes"
28. listKey="typeId"
29. listValue="typeName" />
30. </s:form>
31. </body>
32.</html>
<%@ 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属性,当然也不会产生请求参数。)
具体代码如下:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags"%>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>combobox标签实例</title>
9.</head>
10.<body>
11. <s:form action="testAction" method="post">
12. <s:combobox
13. name="bookType"
14. label="请选择图书类型"
15. labelposition="top"
16. list="{'计算机','社会科学','财经','文学','数学','升学考试'}"/>
17. </s:form>
18.</body>
19.</html>
<%@ 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属性类似,但它是指第二个下拉列表框的。
具体实例代码如下:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>doubleselect标签实例</title>
9.</head>
10.<body>
11. <s:form action="testAction" name="form0" method="post">
12. <s:set name="bookTypes" value="#{'计算机':{'Java','C#','struts2'},
13. '文学':{'言情小说','恐怖','散文','历史小学'},
14. '升学考试':{'考研','考博','高考'}}">
15. </s:set>
16. <s:doubleselect label="请选择类别"
17. name="bookType"
18. list="#bookTypes.keySet()"
19. doubleName="subBookType"
20. doubleList="#bookTypes[top]"></s:doubleselect>
21. </s:form>
22.</body>
23.</html>
<%@ 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:指定日期选择框中哪天才是一周的第一天
实例代码如下:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>datetimepicker标签实例</title>
9.<s:head/>
10.</head>
11.<body>
12. <s:form action="test" theme="simple" method="POST">
13. 日期选择,使用默认的displayFormat(默认为yyyy-MM-dd),指定toggleType(默认为plain)和value属性(值为today,即今天):<br>
14. <s:datetimepicker name="birthDate" label="出生日期" toggleType="fade" value="today"/><hr>
15. 日期选择,指定了format属性<br>
16. <s:datetimepicker name="birthDate" label="出生日期" displayFormat="yyyy/MM/dd"/><hr>
17. 日期选择,指定了weekStartsOn属性(不指定时默认为0,即从周日开始)<br>
18. <s:datetimepicker name="birthDate" label="出生日期" weekStartsOn="1"/><hr>
19.
20. 时间选择(设置type为time)<br>
21. <s:datetimepicker name="start" label="选择出发时间" type="time" /><hr>
22.
23. </s:form>
24.</body>
25.</html>
<%@ 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: 设置第二个下拉列表框是否允许多选
具体代码如下:
Java代码
1.<%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>optiontransferselect标签实例</title>
9.<s:head/>
10.</head>
11.<body>
12. <s:form>
13. <!-- 使用简单集合对象来生成可移动的下拉列表框 -->
14. <s:optiontransferselect
15. label="请选择你喜欢的图书类型"
16. name="bookType"
17. leftTitle="--所有图书--"
18. list="{'计算机','社会科学','财经','文学','数学'}"
19. multiple="true"
20. addToLeftLabel="左移"
21. selectAllLabel="全部选择"
22. addToRightLabel="右移"
23. rightTitle="--喜欢图书--"
24. doubleList="{'散文','历史'}"
25. doubleName="loveBookType"
26. doubleMultiple="true" />
27. </s:form>
28.</body>
29.</html>
<%@ 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../>标签类似)
具体实例代码如下:
Java代码
1. <%@ page language="java" contentType="text/html; charset=UTF-8"
2. pageEncoding="UTF-8"%>
3.<%@ taglib prefix="s" uri="/struts-tags" %>
4.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5.<html>
6.<head>
7.<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8.<title>optgroup实例</title>
9.</head>
10.<body>
11. <s:form action="testAction" method="post">
12. <s:select label="选择您喜欢的图书类型"
13. name="bookType"
14. list="#{'1':'计算机','2':'社会科学','3':'财经','4':'数学'}"
15. listKey="key"
16. listValue="value">
17. <s:optgroup label="小说"
18. list="#{'5':'言情小说','6':'武打小说','7':'恐怖小说'}"
19. listKey="key"
20. listValue="value" />
21. <s:optgroup label="升学考试"
22. list="#{'8':'考研','9':'考博','10':'高考'}"
23. listKey="key"
24. listValue="value"/>
25.
26. </s:select>
27. </s:form>
28.</body>
29.</html>
<%@ 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:全选按钮的文本
具体代码如下:
Java代码
1.<%@ page contentType="text/html; charset=UTF-8" %>
2.<%@ taglib prefix="s" uri="/struts-tags" %>
3.
4.<html>
5. <head>
6. <title>updownselect标签实例</title>
7. <s:head/>
8. </head>
9. <body>
10. <s:form>
11. <!-- 使用简单集合来生成可上下移动选项的下拉选择框 -->
12. <s:updownselect name="bookType1" label="请选择图书类型"
13. labelposition="top"
14. list="{'计算机', '社会科学', '财经', '文学', '数学', '升学考试'}" />
15.
16. <!-- 使用简单Map对象来生成可上下移动选项的下拉选择框,且使用emptyOption="true"增加一个空选项-->
17. <s:updownselect name="bookType2" label="请选择图书类型"
18. labelposition="top"
19. moveUpLabel="上移"
20. moveDownLabel="下移"
21. list="#{'1':'计算机', '2':'社会科学', '3':'财经', '4':'文学', '5':'数学', '6': '升学考试'}"
22. listKey="key"
23. listValue="value"
24. emptyOption="true" />
25.
26. <!-- 使用集合里放多个JavaBean实例来生成下拉列表框: -->
27. <s:bean name="amigo.struts.tag.uiTags.BookTypeService" id="typeService"/>
28. <s:updownselect name="bookType3" label="请选择图书类型"
29. labelposition="top"
30. selectAllLabel="全选"
31. multiple="true"
32. list="#typeService.bookTypes"
33. listKey="typeId"
34. listValue="typeName" />
35. </s:form>
36. </body>
37.</html>
<%@ 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>struts2+hibernate3+spring2读书笔记8(OG ... | struts2+hibernate3+spring2读书笔记6( ... 15:15浏览 (33)评论 (0)分类: struts+hibernate+spring學習相关推荐评论发表评论
表情图标
字体颜色: 标准深红红色橙色棕色黄色绿色橄榄青色蓝色深蓝靛蓝紫色灰色白色黑色 字体大小: 标准1 (xx-small)2 (x-small)3 (small)4 (medium)5 (large)6 (x-large)7 (xx-large) 对齐: 标准居左居中居右
提示:选择您需要装饰的文字, 按上列按钮即可添加上相应的标签.您还没有登录,请登录后发表评论(快捷键 Alt+S / Ctrl+Enter)
hhr_michael
浏览: 4760 次
性别:
来自: 惠州
详细资料 留言簿
搜索本博客
最近访客 >>更多访客
qljobswpfwupengfeiwpf土豆皮mianhuatangone.博客分类
■全部博客 (45)
■java學習 (15)
■PHP學習 (2)
■javascript (12)
■vba excel (1)
■英語學習 (1)
■k3安裝與配置 (1)
■html學習 (1)
■struts+hibernate+spring學習 (11)
■数据库学习 (1)
我的留言簿 >>更多留言
■漂过
-- by xw302
其他分类
■我的收藏 (0)
■我的论坛主题贴 (0)
■我的所有论坛贴 (0)
■我的精华良好贴 (0)
最近加入圈子
存档
■2010-06 (7)
■2010-05 (6)
■2010-03 (1)
■更多存档...
最新评论
■struts2+hibernate3+sprin ...
恩,LZ,谢谢了,那我还是练习把SSh的例子改成S2sh的吧,呵呵,哎,现在s2s ...
-- by lixia0417
■struts2+hibernate3+sprin ...
哥们,你好,这本书着重s2sh的各个部分开发,而整合的例子不是很多,只有三章是整合 ...
-- by hhr_michael
■struts2+hibernate3+sprin ...
对,哥们,问一下,这本书中关于S2SH整合开发的例子多吗,就是用三个框架一起开发的 ...
-- by lixia0417
■struts2+hibernate3+sprin ...
谢谢提醒,由于只是着重校验的过程,所以这代码没有在struts.xml中配置对应的 ...
-- by hhr_michael
■struts2+hibernate3+sprin ...
楼主写得不错,受教了, 不过觉得<code> public Stri ...
-- by lixia0417
评论排行榜
■struts2+hibernate3+spring2读书笔记4(数据 ...
■js生成行,刪除行,復制行
■struts2+hibernate3+spring2读书笔记11( ...
■JSP环境配置使用fckeditor
■struts2+hibernate3+spring2读书笔记10( ...
[什么是RSS?]
.
--------------------------------------------------------------------------------
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。若作者同意转载,必须以超链接形式标明文章原始出处和作者。
? 2003-2010 JavaEye.com. All rights reserved. 上海炯耐计算机软件有限公司 [ 沪ICP备05023328号 ] .