(五)Struts2 标签

所有的学习我们必须先搭建好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>

结果:

时间: 2024-10-16 06:55:31

(五)Struts2 标签的相关文章

JavaWeb框架_Struts2_(五)-----&gt;Struts2的标签库

1. Struts2的标签库 标签库概述 数据标签 流程控制标签 表单标签 非表单标签 2. 具体内容 2.1 Struts2标签库概述 Struts2的标签库可以分为以下3类:用户界面标签.非用户界面标签.AJAX标签; 2.1.1 Struts2标签库的分类和使用 1.  从上图中可以看出,标签的分类如下: (1) 用户界面标签(UI标签) UI标签主要用来生成HTML元素的标签,又分为表单标签和非表单标签.表单标签主要用于生成HTML页面的form元素,以及普通表单元素的标签.非表单元素标

[Java]Struts2标签

http://www.open-open.com/lib/view/open1331393920452.html Struts2常用标签总结 一 介绍 1.Struts2的作用    Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的代码复用.Struts2允许在页面中使用自定义组件,这完全能满足项目中页面显示复杂,多变的需求.     Struts2的标签库有一个巨大的改进之处,struts2标签库的标签不依

【Struts2标签的使用和具体事项总结】

Struts2常用标签总结 一 介绍 1.Struts2的作用     Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的代码复用.Struts2允许在页面中使用自定义组件,这完全能满足项目中页面显示复杂,多变的需求.     Struts2的标签库有一个巨大的改进之处,struts2标签库的标签不依赖于任何表现层技术,也就是说strtus2提供了大部分标签,可以在各种表现技术中使用.包括最常用的jsp页面,也

struts2 标签介绍

计应134班(实验班) 陈兴 Struts2常用标签总结一 介绍1.Struts2的作用 Struts2标签库提供了主题.模板支持,极大地简化了视图页面的编写,而且,struts2的主题.模板都提供了很好的扩展性.实现了更好的代码复用.Struts2允许在页面中使用自定义组件,这完全能满足项目中页面显示复杂,多变的需求. Struts2的标签库有一个巨大的改进之处,struts2标签库的标签不依赖于任何表现层技术,也就是说strtus2提供了大部分标签,可以在各种表现技术中使用.包括最常用的js

18.如何自学Struts2之Struts2标签和集成初略总结篇

18.如何自学Struts2之Struts2标签和集成初略总结篇[视频] 之前写了一篇"打算做一个视频教程探讨如何自学计算机相关的技术",优酷上传不了,只好传到百度云上: http://pan.baidu.com/s/1kTDsa95 有问题可以直接回复这篇文章.

Struts2标签小结

前边总结Struts2的时候将标签这一部分给丢了,当时因为开发的时候很少使用,这里简单总结一下.先看一下Struts2标签的分类吧.然后分类总结一下即可. 由于控制标签用的比较多,所以放在了前边.好首先看一下控制标签吧. 一,控制标签,JSTL大家应该都使用过,其实Struts2的控制标签和那个特别像的,主要是对EL表达式满足不了的进行补充使用.这里简单看一下吧: 1, if--elseif--else,用来控制选择输出的标签: <html xmlns="http://www.w3.org

JAVAEE——struts2_04:自定义拦截器、struts2标签、登陆功能和校验登陆拦截器的实现

一.自定义拦截器 1.架构 2.拦截器创建 //拦截器:第一种创建方式 //拦截器生命周期:随项目的启动而创建,随项目关闭而销毁 public class MyInterceptor implements Interceptor{} //创建方式2: 继承AbstractInterceptor -> struts2的体贴 //帮我们空实现了init 和 destory方法. 我们如果不需要实现这两个方法,就可以只实现intercept方法 public class MyInterceptor2

用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。

最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量.<s:iterator>标签有一个value属性,用来存放在Action类的方法中存数据的list集合,还有一个id,好像是说指定集合的索引的意思,就是给list集合遍历出来的每个对象加上一个数字标签,反正我是这么理解的,没用过.还有一个很重要,就是var变量,我在s:iterator按ctr

Struts2(十)OGNL标签二与Struts2标签

一.Struts2标签的优势 标签库简化了用户对标签的使用 结合OGNL使用,对于集合.对象的访问功能非常强大 提供可扩展的主题.模板支持.极大简化了视图页面的编写 不依赖任何表现层技术 Struts2标签的分类: 通用标签(Generir Tags) 数据标签(Data Tags) 控制标签(Control Tags) UI标签(UI Tags) Ajax标签(Ajax Tags) 二.数据标签 web.xml <filter> <filter-name>struts2</