码迷,mamicode.com
首页 > 其他好文 > 详细

(五)Struts2 标签

时间:2016-05-08 23:57:49      阅读:449      评论:0      收藏:0      [点我收藏+]

标签:

所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)

第一节:Struts2 标签简介

Struts2 自己封装了一套标签,比JSTL 强大,而且与Struts2 中的其他功能无缝结合。

当然Strust2 标签的内容很多,随着版本的升级,标签和属性越来越多。我们要掌握好核心标签及了解其他标签。

根据功能可以分为:数据标签,控制标签,界面标签,其他标签

第二节:Struts2 数据标签

Property 标签:输出OGNL 表达式的值;
Set 标签:设置变量;
Bean 标签:定义javaBean 对象;
Date 标签:日期标签;
Debug 标签:调试标签;
Url&a 标签:超链接标签;
Include 标签:动态包含标签;

 

 struts.xml
1
<?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 9 <package name="manage" namespace="/" extends="struts-default"> 10 11 </package> 12 13 </struts>

 

 Student.java
1
package com.wishwzp.model; 2 3 public class Student { 4 5 private int id; 6 private String name; 7 private int age; 8 9 public Student() { 10 super(); 11 // TODO Auto-generated constructor stub 12 } 13 14 public Student(int id, String name, int age) { 15 super(); 16 this.id = id; 17 this.name = name; 18 this.age = age; 19 } 20 21 public int getId() { 22 return id; 23 } 24 public void setId(int id) { 25 this.id = id; 26 } 27 public String getName() { 28 return name; 29 } 30 public void setName(String name) { 31 this.name = name; 32 } 33 public int getAge() { 34 return age; 35 } 36 public void setAge(int age) { 37 this.age = age; 38 } 39 40 }

 

 dataTag.jsp
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>Insert title here</title> 8 </head> 9 <body> 10 <h>数据标签</h> 11 <hr/> 12 <a href="data/property.jsp" target="_blank">property标签</a><br/> 13 <a href="data/set.jsp" target="_blank">set标签</a><br/> 14 <a href="data/bean.jsp" target="_blank">bean标签</a><br/> 15 <a href="data/date.jsp" target="_blank">date标签</a><br/> 16 <a href="data/debug.jsp" target="_blank">debug标签</a><br/> 17 <a href="data/url_a.jsp" target="_blank">url_a标签</a><br/> 18 <a href="data/include.jsp" target="_blank">include标签</a><br/> 19 </body> 20 </html>

 

页面结果:

 

技术分享

上面的是一个主的页面,会跳转到以下几个页面的。(一下页面都会在WebContent下的data文件夹中)

Property 标签:输出OGNL 表达式的值;

技术分享
 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 <%
10     request.setAttribute("name","<font color=red>张三</font>");
11 %>
12 </head>
13 <body>
14 <s:property value="#request.name" /><br/>
15 <s:property value="#request.name2" default="某某人"/><br/>
16 <s:property value="#request.name" default="某某人" escapeHtml="false"/><br/>
17 </body>
18 </html>
property.jsp

 

结果:


技术分享

Set 标签:设置变量;

技术分享
 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:set var="i" value="1"></s:set>
12 <s:property value="#i" /><br/>
13 <s:set var="a"  value="‘action范围的值‘" scope="action"></s:set>
14 <s:set var="p"  value="‘page范围的值‘" scope="page"></s:set>
15 <s:set var="r"  value="‘request范围的值‘" scope="request"></s:set>
16 <s:set var="s"  value="‘session范围的值‘" scope="session"></s:set>
17 <s:set var="app"  value="‘application范围的值‘" scope="application"></s:set>
18 <s:property value="#a" /><br/>
19 <s:property value="#attr.p"/><br/>
20 <s:property value="#request.r"/><br/>
21 <s:property value="#session.s"/><br/>
22 <s:property value="#application.app"/><br/>
23 </body>
24 </html>
set.jsp

 

结果:

 

技术分享

Bean 标签:定义javaBean 对象;

技术分享
 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:bean name="com.wishwzp.model.Student" var="student">
12     <s:param name="name" value="‘张三‘"></s:param>
13     <s:param name="age" value="10"></s:param>
14 </s:bean> 
15 <s:property value="#student.name"/>
16 <s:property value="#student.age"/>
17 </body>
18 </html>
bean.jsp

 

结果:

 

技术分享

Date 标签:日期标签;

技术分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="java.util.*" %>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <%@taglib prefix="s" uri="/struts-tags" %>
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 <%
11     request.setAttribute("date",new Date());
12 %>
13 </head>
14 <body>
15 ${date }<br/>
16 当前日期:<s:date name="#request.date" format="yyyy-MM-dd"/>
17 </body>
18 </html>
date.jsp

 

结果:

 

技术分享

Debug 标签:调试标签;

技术分享
 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:debug></s:debug>
12 </body>
13 </html>
debug.jsp

 

结果:

 

技术分享

 

技术分享

Url&a 标签:超链接标签;

技术分享
 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:url action="hello" namespace="/foreground" id="h">
12     <s:param name="name" value="‘struts2‘"></s:param>
13 </s:url>
14 <s:a href="%{h}">超链接</s:a>
15 
16 <s:a action="hello" namespace="/foreground">
17     <s:param name="name" value="‘struts2‘"></s:param>
18     超链接2
19 </s:a>
20 </body>
21 </html>
url_a.jsp

 

结果:

 

技术分享

Include 标签:动态包含标签;

技术分享
 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:include value="head.html"></s:include>
12 </body>
13 </html>
include.jsp

 

技术分享
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 头部
 9 </body>
10 </html>
head.html

 

结果:

 

技术分享

第三节:Struts2 控制标签

Ifelse 标签:条件判断标签;
Iterator 标签:遍历标签;
Append 标签:叠加标签;
Generator 标签:分隔标签;
Merge 标签:组合标签;
Sort 标签:排序标签;
Subset 标签:截取标签;

 

MyComparator.java
 1 package com.wishwzp.comparator;
 2 
 3 import java.util.Comparator;
 4 
 5 import com.wishwzp.model.Student;
 6 
 7 public class MyComparator implements Comparator<Student>{
 8 
 9     public int compare(Student s1, Student s2) {
10         if(s1.getAge()>s2.getAge()){
11             return 1;
12         }else if(s1.getAge()<s2.getAge()){
13             return -1;
14         }
15         return 0;
16     }
17 
18 }

 

 

controlTag.jsp

 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>Insert title here</title>
 8 </head>
 9 <body>
10 <h>控制标签</h>
11 <hr/>
12 <a href="control/ifelse.jsp" target="_blank">ifelse标签</a><br/>
13 <a href="control/iterator.jsp" target="_blank">iterator标签</a><br/>
14 <a href="control/append.jsp" target="_blank">append标签</a><br/>
15 <a href="control/generator.jsp" target="_blank">generator标签</a><br/>
16 <a href="control/merge.jsp" target="_blank">merge标签</a><br/>
17 <a href="control/sort.jsp" target="_blank">sort标签</a><br/>
18 <a href="control/subset.jsp" target="_blank">subset标签</a><br/>
19 </body>
20 </html>

 

上面的是一个主的页面,会跳转到以下几个页面的。(一下页面都会在WebContent下的control文件夹中)

结果:

 

技术分享

Ifelse 标签:条件判断标签;

ifelse.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 <%
10     int age=11;
11     request.setAttribute("age",age);
12 %>
13 </head>
14 <body>
15 <s:if test="#request.age<20">
16     年龄小于20岁
17 </s:if>
18 <s:elseif test="#request.age==20">
19     年龄等于20岁
20 </s:elseif>
21 <s:else>
22     年龄大于20岁
23 </s:else>
24 </body>
25 </html>

 

结果:

 

技术分享

 

Iterator 标签:遍历标签;

iterator.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="com.wishwzp.model.Student" %>
 4 <%@ page import="java.util.*" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <%@taglib prefix="s" uri="/struts-tags" %>
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 <%
12     List<Student> studentList=new ArrayList<Student>();
13     studentList.add(new Student(1,"张三",10));
14     studentList.add(new Student(3,"李四",20));
15     studentList.add(new Student(5,"王五",30));
16     request.setAttribute("studentList",studentList);
17 %>
18 </head>
19 <body>
20 <table>
21     <tr>
22         <th>序号</th>
23         <th>编号</th>
24         <th>姓名</th>
25         <th>年龄</th>
26     </tr>
27     <s:iterator value="#request.studentList" status="status">
28     <tr>
29         <td><s:property value="#status.index+1"/></td>
30         <td><s:property value="id"/></td>
31         <td><s:property value="name"/></td>
32         <td><s:property value="age"/></td>
33     </tr>
34     </s:iterator>
35 </table>
36 </body>
37 </html>

 

结果:

 

技术分享

Append 标签:叠加标签;

append.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="com.wishwzp.model.Student" %>
 4 <%@ page import="java.util.*" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <%@taglib prefix="s" uri="/struts-tags" %>
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 <%
12     List<Student> studentList1=new ArrayList<Student>();
13     List<Student> studentList2=new ArrayList<Student>();
14     studentList1.add(new Student(1,"张三",10));
15     studentList1.add(new Student(3,"李四",20));
16     studentList2.add(new Student(5,"王五",30));
17     studentList2.add(new Student(7,"赵六",40));
18     request.setAttribute("studentList1",studentList1);
19     request.setAttribute("studentList2",studentList2);
20 %>
21 </head>
22 <body>
23 <!-- 把studentList1和studentList2叠加到studentList3中 -->
24 <s:append var="studentList3">
25     <s:param value="#request.studentList1"></s:param>
26     <s:param value="#request.studentList2"></s:param>
27 </s:append>
28 <table>
29     <tr>
30         <th>序号</th>
31         <th>编号</th>
32         <th>姓名</th>
33         <th>年龄</th>
34     </tr>
35     <s:iterator value="studentList3" status="status">
36     <tr>
37         <td><s:property value="#status.index+1"/></td>
38         <td><s:property value="id"/></td>
39         <td><s:property value="name"/></td>
40         <td><s:property value="age"/></td>
41     </tr>
42     </s:iterator>
43 </table>
44 </body>
45 </html>

 

结果:

 

技术分享

Generator 标签:分隔标签;

generator.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:generator separator="," val="‘张三,李四,王五‘" var="nameList"></s:generator>
12 
13 <s:iterator value="#nameList">
14     <s:property/>
15 </s:iterator>
16 </table>
17 </body>
18 </html>

 

结果:

 

技术分享

Merge 标签:组合标签;

merge.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="com.wishwzp.model.Student" %>
 4 <%@ page import="java.util.*" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <%@taglib prefix="s" uri="/struts-tags" %>
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 <%
12     List<Student> studentList1=new ArrayList<Student>();
13     List<Student> studentList2=new ArrayList<Student>();
14     studentList1.add(new Student(1,"张三",10));
15     studentList1.add(new Student(3,"李四",20));
16     studentList2.add(new Student(5,"王五",30));
17     studentList2.add(new Student(7,"赵六",40));
18     request.setAttribute("studentList1",studentList1);
19     request.setAttribute("studentList2",studentList2);
20 %>
21 </head>
22 <body>
23 <s:merge var="studentList3">
24     <s:param value="#request.studentList1"></s:param>
25     <s:param value="#request.studentList2"></s:param>
26 </s:merge>
27 <table>
28     <tr>
29         <th>序号</th>
30         <th>编号</th>
31         <th>姓名</th>
32         <th>年龄</th>
33     </tr>
34     <s:iterator value="studentList3" status="status">
35     <tr>
36         <td><s:property value="#status.index+1"/></td>
37         <td><s:property value="id"/></td>
38         <td><s:property value="name"/></td>
39         <td><s:property value="age"/></td>
40     </tr>
41     </s:iterator>
42 </table>
43 </body>
44 </html>

 

结果:

 

技术分享

Sort 标签:排序标签;

sort.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="com.wishwzp.model.Student" %>
 4 <%@ page import="java.util.*" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <%@taglib prefix="s" uri="/struts-tags" %>
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 <%
12     List<Student> studentList1=new ArrayList<Student>();
13     studentList1.add(new Student(1,"张三",20));
14     studentList1.add(new Student(3,"李四",10));
15     studentList1.add(new Student(5,"王五",40));
16     studentList1.add(new Student(7,"赵六",30));
17     request.setAttribute("studentList1",studentList1);
18 %>
19 </head>
20 <body>
21 <s:bean id="myComparator" name="com.wishwzp.comparator.MyComparator"></s:bean>
22 
23 
24 
25 <table>
26     <tr>
27         <th>序号</th>
28         <th>编号</th>
29         <th>姓名</th>
30         <th>年龄</th>
31     </tr>
32     <s:sort comparator="#myComparator" source="#request.studentList1" >
33     <s:iterator status="status">
34     <tr>
35         <td><s:property value="#status.index+1"/></td>
36         <td><s:property value="id"/></td>
37         <td><s:property value="name"/></td>
38         <td><s:property value="age"/></td>
39     </tr>
40     </s:iterator>
41     </s:sort>
42 </table>
43 </body>
44 </html>

 

结果:

 

技术分享

Subset 标签:截取标签;

subset.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page import="com.wishwzp.model.Student" %>
 4 <%@ page import="java.util.*" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <%@taglib prefix="s" uri="/struts-tags" %>
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>Insert title here</title>
11 <%
12     List<Student> studentList1=new ArrayList<Student>();
13     studentList1.add(new Student(1,"张三",20));
14     studentList1.add(new Student(3,"李四",10));
15     studentList1.add(new Student(5,"王五",40));
16     studentList1.add(new Student(7,"赵六",30));
17     request.setAttribute("studentList1",studentList1);
18 %>
19 </head>
20 <body>
21 
22 <table>
23     <tr>
24         <th>序号</th>
25         <th>编号</th>
26         <th>姓名</th>
27         <th>年龄</th>
28     </tr>
29     <s:subset source="#request.studentList1" count="2" start="2">
30     <s:iterator status="status">
31     <tr>
32         <td><s:property value="#status.index+1"/></td>
33         <td><s:property value="id"/></td>
34         <td><s:property value="name"/></td>
35         <td><s:property value="age"/></td>
36     </tr>
37     </s:iterator>
38     </s:subset>
39 </table>
40 </body>
41 </html>

 

结果:

 

技术分享

第四节:Strut2 界面标签

Form 标签:表单提交标签;

Text 标签:文本标签;

Radios 标签:单选标签;

Checkboxlist 标签:复选框标签;

Select 标签:下拉框标签;

 struts.xml
1
<?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 9 <package name="manage" namespace="/" extends="struts-default"> 10 11 </package> 12 13 </struts>

 

 uiTag.jsp
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>Insert title here</title> 8 </head> 9 <body> 10 <h>界面标签</h> 11 <hr/> 12 <a href="ui/form.jsp" target="_blank">form标签</a><br/> 13 <a href="ui/text.jsp" target="_blank">文本标签</a><br/> 14 <a href="ui/radio.jsp" target="_blank">单选标签</a><br/> 15 <a href="ui/checkbox.jsp" target="_blank">复选框标签</a><br/> 16 <a href="ui/select.jsp" target="_blank">下拉框标签</a><br/> 17 </body> 18 </html>

 

上面的是一个主的页面,会跳转到以下几个页面的。(一下页面都会在WebContent下的ui文件夹中)

结果:

技术分享

Form 标签:表单提交标签;

form.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:form action="hello" method="post" namespace="/foreground">
12 </s:form>
13 </body>
14 </html>

 

 

Text 标签:文本标签;

text.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 用户名:<s:textfield name="userName"></s:textfield><br/>
12 密码:<s:password name="password"></s:password><br/>
13 备注:<s:textarea name="desc"></s:textarea><br/>
14 </body>
15 </html>

 

结果:

技术分享

Radios 标签:单选标签;

radios.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 性别:<s:radio list="#{0: ‘男 ‘, 1:‘女 ‘}" name="sex" value="0" /> 
12 </body>
13 </html>

 

结果:

技术分享

Checkboxlist 标签:复选框标签;

checkbox.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 爱好:<s:checkboxlist list="#{0: ‘游泳‘, 1:‘唱歌 ‘,2:‘跳舞‘}" name="hobby" value="1" /> 
12 </body>
13 </html>

 

结果:

技术分享

Select 标签:下拉框标签;

select.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 爱好:<s:select list="#{0: ‘游泳‘, 1:‘唱歌 ‘,2:‘跳舞‘}" name="hobby" value="1" multiple="true"/> 
12 </body>
13 </html>

 

结果:

技术分享

第五节:其他标签

Updownselect 标签;

Optiontransferselect 标签;

otherTag.jsp

 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>Insert title here</title>
 8 </head>
 9 <body>
10 <h>其他标签</h>
11 <hr/>
12 <a href="other/updownselect.jsp" target="_blank">updownselect标签</a><br/>
13 <a href="other/optiontransferselect.jsp" target="_blank">optiontransferselect标签</a><br/>
14 </body>
15 </html>

 

上面的是一个主的页面,会跳转到以下几个页面的。(一下页面都会在WebContent下的ui文件夹中)

结果:

技术分享

Updownselect 标签;

updownselect.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:updownselect 
12     list="#{0:‘游泳‘, 1:‘唱歌‘, 2:‘跳舞‘}"
13     name="hobby" 
14     headerKey="-1"
15     headerValue="请选择" 
16     emptyOption="true"
17     allowMoveUp="true" 
18     allowMoveDown="true" 
19     allowSelectAll="true"
20     moveUpLabel="向上" 
21     moveDownLabel="向下"
22     selectAllLabel="全选" /> 
23 </body>
24 </html>

 

结果:

技术分享

Optiontransferselect 标签;

optiontransferselect.jsp

 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 <%@taglib prefix="s" uri="/struts-tags" %>
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <s:optiontransferselect label="选择你喜欢图书"  
12               name="cnbook" leftTitle="中文图书"  list="{‘struts2权威指南‘,‘轻量级javaeye 企业应用空实战‘,‘ajax讲义‘}"
13               doubleName="enBook"  rightTitle="外文图书" doubleList="{‘JavaScrip:The definitive Guide‘,‘export one-to-one‘}"  multiple="true" 
14               addToLeftLabel="向左移动" addToRightLabel="向右移动" addAllToRightLabel="全部右移" addAllToLeftLabel="全部左移"
15                allowSelectAll="true" headerKey="cnKey" headerValue="选择图书" emptyOption="true"   doubleHeaderKey="enKey" 
16                doubleHeaderValue="选择外文图书" doubleMultiple="true" doubleEmptyOption="true"  leftDownLabel="向下移动" 
17        rightDownLabel="向下移动" 
18        leftUpLabel="向上移动" 
19        rightUpLabel="向上移动" >
20    </s:optiontransferselect>
21 </body>
22 </html>

 

结果:

技术分享

 

(五)Struts2 标签

标签:

原文地址:http://www.cnblogs.com/wishwzp/p/5472013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!