`

struts2标签

阅读更多
1. struts2标签
   
Struts2自己封装了一套标签,比JSTL强大,而且与Struts2中的其他功能无缝结合。
当然Strust2标签的内容很多,随着版本的升级,标签和属性越来越多。我们要掌握好核心标签及了解其他标签;
根据功能可以分为:数据标签,控制标签,界面标签,其他标签;


新建项目HeadFirstStruts2Chap05

MyComparator.java

package com.andrew.comparator;
import java.util.Comparator;
import com.andrew.model.Student;
public class MyComparator implements Comparator<Student> {
    public int compare(Student s1, Student s2) {
        if (s1.getAge() > s2.getAge()) {
            return 1;
        } else if (s1.getAge() < s2.getAge()) {
            return -1;
        }
        return 0;
    }
}

Student.java

package com.andrew.model;
public class Student {
    private int id;
    private String name;
    private int age;
    public Student() {}
    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

struts.xml

<package name="manage" namespace="/" extends="struts-default"></package>

jsp

<%@taglib prefix="s" uri="/struts-tags" %>


2. Struts2数据标签

1) property标签:输出OGNL表达式的值

<% request.setAttribute("name","<font color=red>张三</font>"); %>
<s:property value="#request.name" /><br/>
<s:property value="#request.name2" default="某某人"/><br/>
<s:property value="#request.name" default="某某人" escapeHtml="false"/><br/>

http://localhost:8080/HeadFirstStruts2Chap05/data/property.jsp
<font color=red>张三</font>
某某人
张三


2) set标签:设置变量

<s:set var="i" value="1"></s:set>
<s:property value="#i" /><br/>
<s:set var="a"  value="'action范围的值'" scope="action"></s:set>
<s:set var="p"  value="'page范围的值'" scope="page"></s:set>
<s:set var="r"  value="'request范围的值'" scope="request"></s:set>
<s:set var="s"  value="'session范围的值'" scope="session"></s:set>
<s:set var="app"  value="'application范围的值'" scope="application"></s:set>
<s:property value="#a" /><br/>
<s:property value="#attr.p"/><br/>
<s:property value="#request.r"/><br/>
<s:property value="#session.s"/><br/>
<s:property value="#application.app"/><br/>

http://localhost:8080/HeadFirstStruts2Chap05/data/set.jsp
1
action范围的值
page范围的值
request范围的值
session范围的值
application范围的值


3) bean标签:定义javaBean对象

<s:bean name="com.andrew.model.Student" var="student">
    <s:param name="name" value="'张三'"></s:param>
    <s:param name="age" value="10"></s:param>
</s:bean> 
<s:property value="#student.name"/>
<s:property value="#student.age"/>

http://localhost:8080/HeadFirstStruts2Chap05/data/bean.jsp
张三 10


4) date标签:日期标签

<%    request.setAttribute("date",new Date()); %>
${date }<br/>
当前日期:<s:date name="#request.date" format="yyyy-MM-dd"/>

http://localhost:8080/HeadFirstStruts2Chap05/data/date.jsp
Sat Jan 05 15:44:04 CST 2018
当前日期:2018-01-05


5) debug标签:调试标签

<s:debug></s:debug>


6) url&a标签:超链接标签

<s:url action="hello" namespace="/foreground" id="h">
    <s:param name="name" value="'struts2'"></s:param>
</s:url>
<s:a href="%{h}">超链接</s:a>
<s:a action="hello" namespace="/foreground">
    <s:param name="name" value="'struts2'"></s:param>
    超链接2
</s:a>

http://localhost:8080/HeadFirstStruts2Chap05/foreground/hello.action?name=struts2
http://localhost:8080/HeadFirstStruts2Chap05/foreground/hello.action?name=struts2


7) include标签:动态包含标签

<s:include value="head.html"></s:include>

head.html

头部

http://localhost:8080/HeadFirstStruts2Chap05/data/include.jsp
头部


3.  Struts2控制标签

1) ifelse标签:条件判断标签

<%    int age=11;
    request.setAttribute("age",age); %>
<s:if test="#request.age<20">年龄小于20岁</s:if>
<s:elseif test="#request.age==20">年龄等于20岁</s:elseif>
<s:else>年龄大于20岁</s:else>

年龄小于20岁 


2) iterator标签:遍历标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List<Student> studentList=new ArrayList<Student>();
    studentList.add(new Student(1,"张三",10));
    studentList.add(new Student(3,"李四",20));
    studentList.add(new Student(5,"王五",30));
    request.setAttribute("studentList",studentList);
%>
<table>
    <tr><th>序号</th><th>编号</th><th>姓名</th><th>年龄</th></tr>
    <s:iterator value="#request.studentList" status="status">
    <tr>
        <td><s:property value="#status.index+1"/></td>
        <td><s:property value="id"/></td>
        <td><s:property value="name"/></td>
        <td><s:property value="age"/></td>
    </tr>
    </s:iterator>
</table>

http://localhost:8080/HeadFirstStruts2Chap05/control/iterator.jsp
序号 编号 姓名 年龄
1 1 张三 10 
2 3 李四 20 
3 5 王五 30


3) append标签:叠加标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List<Student> studentList1=new ArrayList<Student>();
    List<Student> studentList2=new ArrayList<Student>();
    studentList1.add(new Student(1,"张三",10));
    studentList1.add(new Student(3,"李四",20));
    studentList2.add(new Student(5,"王五",30));
    studentList2.add(new Student(7,"赵六",40));
    request.setAttribute("studentList1",studentList1);
    request.setAttribute("studentList2",studentList2);
%>
<s:append var="studentList3">
    <s:param value="#request.studentList1"></s:param>
    <s:param value="#request.studentList2"></s:param>
</s:append>
<table>
    <tr><th>序号</th><th>编号</th><th>姓名</th><th>年龄</th></tr>
    <s:iterator value="studentList3" status="status">
    <tr>
        <td><s:property value="#status.index+1"/></td>
        <td><s:property value="id"/></td>
        <td><s:property value="name"/></td>
        <td><s:property value="age"/></td>
    </tr>
    </s:iterator>
</table>

http://localhost:8080/HeadFirstStruts2Chap05/control/append.jsp
序号 编号 姓名 年龄
1 1 张三 10 
2 3 李四 20 
3 5 王五 30 
4 7 赵六 40


4) generator标签:分隔标签

<s:generator separator="," val="'张三,李四,王五'" var="nameList"></s:generator>
<s:iterator value="#nameList">
    <s:property/>
</s:iterator>

http://localhost:8080/HeadFirstStruts2Chap05/control/generator.jsp
张三 李四 王五


5) merge标签:组合标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List<Student> studentList1=new ArrayList<Student>();
    List<Student> studentList2=new ArrayList<Student>();
    studentList1.add(new Student(1,"张三",10));
    studentList1.add(new Student(3,"李四",20));
    studentList2.add(new Student(5,"王五",30));
    studentList2.add(new Student(7,"赵六",40));
    request.setAttribute("studentList1",studentList1);
    request.setAttribute("studentList2",studentList2);
%>
<s:merge var="studentList3">
    <s:param value="#request.studentList1"></s:param>
    <s:param value="#request.studentList2"></s:param>
</s:merge>
<table>
    <tr><th>序号</th><th>编号</th><th>姓名</th><th>年龄</th></tr>
    <s:iterator value="studentList3" status="status">
    <tr>
        <td><s:property value="#status.index+1"/></td>
        <td><s:property value="id"/></td>
        <td><s:property value="name"/></td>
        <td><s:property value="age"/></td>
    </tr>
    </s:iterator>
</table>

http://localhost:8080/HeadFirstStruts2Chap05/control/merge.jsp
序号 编号 姓名 年龄
1 1 张三 10 
2 5 王五 30 
3 3 李四 20 
4 7 赵六 40 


6) sort标签:排序标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List<Student> studentList1=new ArrayList<Student>();
    studentList1.add(new Student(1,"张三",20));
    studentList1.add(new Student(3,"李四",10));
    studentList1.add(new Student(5,"王五",40));
    studentList1.add(new Student(7,"赵六",30));
    request.setAttribute("studentList1",studentList1);
%>
<s:bean id="myComparator" name="com.andrew.comparator.MyComparator"></s:bean>
<table>
    <tr><th>序号</th><th>编号</th><th>姓名</th><th>年龄</th></tr>
    <s:sort comparator="#myComparator" source="#request.studentList1" >
    <s:iterator status="status">
    <tr>
        <td><s:property value="#status.index+1"/></td>
        <td><s:property value="id"/></td>
        <td><s:property value="name"/></td>
        <td><s:property value="age"/></td>
    </tr>
    </s:iterator>
    </s:sort>
</table>

http://localhost:8080/HeadFirstStruts2Chap05/control/sort.jsp
序号 编号 姓名 年龄
1 3 李四 10 
2 1 张三 20 
3 7 赵六 30 
4 5 王五 40


7) subset标签:截取标签

<%@ page import="com.andrew.model.Student" %>
<%@ page import="java.util.*" %>
<%
    List<Student> studentList1=new ArrayList<Student>();
    studentList1.add(new Student(1,"张三",20));
    studentList1.add(new Student(3,"李四",10));
    studentList1.add(new Student(5,"王五",40));
    studentList1.add(new Student(7,"赵六",30));
    request.setAttribute("studentList1",studentList1);
%>
<table>
    <tr><th>序号</th><th>编号</th><th>姓名</th><th>年龄</th></tr>
    <s:subset source="#request.studentList1" count="2" start="2">
    <s:iterator status="status">
    <tr>
        <td><s:property value="#status.index+1"/></td>
        <td><s:property value="id"/></td>
        <td><s:property value="name"/></td>
        <td><s:property value="age"/></td>
    </tr>
    </s:iterator>
    </s:subset>
</table>

http://localhost:8080/HeadFirstStruts2Chap05/control/subset.jsp
序号 编号 姓名 年龄
1 5 王五 40 
2 7 赵六 30 


4. Strut2界面标签

1) form标签:表单提交标签

<s:form action="hello" method="post" namespace="/foreground"></s:form>

http://localhost:8080/HeadFirstStruts2Chap05/ui/form.jsp


2) text标签:文本标签

用户名:<s:textfield name="userName"></s:textfield><br/>
密码:<s:password name="password"></s:password><br/>
备注:<s:textarea name="desc"></s:textarea><br/>

http://localhost:8080/HeadFirstStruts2Chap05/ui/text.jsp


3) radios标签:单选标签

性别:<s:radio list="#{0: '男 ', 1:'女 '}" name="sex" value="0" />

http://localhost:8080/HeadFirstStruts2Chap05/ui/radio.jsp


4) checkboxlist标签:复选框标签

爱好:<s:checkboxlist list="#{0: '游泳', 1:'唱歌 ',2:'跳舞'}" name="hobby" value="1" />

http://localhost:8080/HeadFirstStruts2Chap05/ui/checkbox.jsp


5) select标签:下拉框标签

爱好:<s:select list="#{0: '游泳', 1:'唱歌 ',2:'跳舞'}" name="hobby" value="1" multiple="true"/>

http://localhost:8080/HeadFirstStruts2Chap05/ui/select.jsp



5. 其他标签

1) updownselect标签

<s:updownselect 
    list="#{0:'游泳', 1:'唱歌', 2:'跳舞'}"
    name="hobby" 
    headerKey="-1"
    headerValue="请选择" 
    emptyOption="true"
    allowMoveUp="true" 
    allowMoveDown="true" 
    allowSelectAll="true"
    moveUpLabel="向上" 
    moveDownLabel="向下"
    selectAllLabel="全选" /> 

http://localhost:8080/HeadFirstStruts2Chap05/ui/updownselect.jsp


2) optiontransferselect标签

<s:optiontransferselect label="选择你喜欢图书"  
              name="cnbook" leftTitle="中文图书"  list="{'struts2权威指南','轻量级javaeye 企业应用空实战','ajax讲义'}"
              doubleName="enBook"  rightTitle="外文图书" doubleList="{'JavaScrip:The definitive Guide','export one-to-one'}"  multiple="true" 
              addToLeftLabel="向左移动" addToRightLabel="向右移动" addAllToRightLabel="全部右移" addAllToLeftLabel="全部左移"
               allowSelectAll="true" headerKey="cnKey" headerValue="选择图书" emptyOption="true"   doubleHeaderKey="enKey" 
               doubleHeaderValue="选择外文图书" doubleMultiple="true" doubleEmptyOption="true"  leftDownLabel="向下移动" 
       rightDownLabel="向下移动" 
       leftUpLabel="向上移动" 
       rightUpLabel="向上移动" >
   </s:optiontransferselect>

http://localhost:8080/HeadFirstStruts2Chap05/ui/optiontransferselect.jsp
分享到:
评论

相关推荐

    struts2标签使用例子

    在Struts2框架中,标签库是其一大特色,它提供了丰富的自定义标签,使得开发者能够更加便捷地创建动态页面。这些标签极大地简化了JSP页面的编写,提高了代码的可读性和可维护性。 1. **Struts2核心标签库**: - `s...

    struts2标签库struts2标签库struts2标签库

    "Struts2标签库详解" Struts2标签库是Struts2框架中的一个重要组件,它提供了一系列的标签,帮助开发者快速构建Web应用程序。在Struts2标签库中,标签可以分为UI标签和非UI标签两大类。UI标签用于生成HTML控件,如...

    struts2标签库例子

    Struts2 标签库详解 Struts2 提供了一个强大的标签库,用于简化 Web 应用程序的开发过程。这些标签可以分为两类:通用标签和 UI 标签。下面,我们将详细介绍 Struts2 标签库的使用。 4.1 通用标签 通用标签用来...

    Struts2标签集锦(附Struts2标签详解word文档)

    Struts2标签是Java开发中一个非常重要的工具,它极大地简化了MVC框架下的视图层构建,使得开发者能够更加高效地实现动态网页。在Struts2框架中,标签库是一系列预定义的JSP标签,它们提供了丰富的功能,如数据绑定、...

    使用Struts 2标签的注意事项

    Struts 2 标签的使用注意事项 Struts 2 是一个基于MVC架构的Java Web应用程序框架,它提供了一系列的标签来简化Web开发。然而,在使用Struts 2标签时,需要注意一些重要的事项。 首先,在使用Struts 2标签之前,...

    Struts2标签使用及API文档

    在Struts2框架中,标签库是其重要组成部分,它提供了丰富的标签来帮助开发者更方便地构建用户界面。下面我们将深入探讨Struts2标签的使用及其API文档。 1. **Struts2标签的分类与功能** - **控制标签**:如`s:if`,...

    struts2标签使用方法

    在Struts2中,标签库是其核心特性之一,它提供了一系列预定义的JSP标签,用于简化视图层的编码,提高代码的可读性和可维护性。下面我们将详细探讨Struts2标签的使用方法以及EL(Expression Language)表达式。 1. *...

    struts2标签库

    ### Struts2 标签库详解 Struts2框架作为Struts1的升级版,在功能性和易用性方面有了显著的提升。对于那些已经熟悉Struts1的人来说,学习Struts2将是一个相对平滑的过程。Struts2标签库是其核心组件之一,它提供了...

    struts2标签下的用户注册

    2. **Struts2标签**:Struts2提供了一系列自定义标签来简化视图层的开发。例如,我们可以使用`&lt;s:textfield&gt;`标签来创建输入框,`&lt;s:password&gt;`标签用于密码输入,`&lt;s:submit&gt;`标签则用于提交表单。 ```jsp ...

    Struts2标签中文文档(html和chm格式)

    在Struts2框架中,标签库是其核心特性之一,它提供了丰富的UI组件和控制逻辑,使得开发者可以更加便捷地进行页面渲染和交互处理。本文将深入探讨Struts2标签的中文文档,以及如何利用这些文档进行有效开发。 首先,...

    struts2 标签库使用文档

    Struts2标签库的组成 Struts2框架的标签库可以分为以下三类: 用户界面标签(UI标签):主要用来生成HTML元素的标签。 表单标签:主要用于生成HTML页面的FORM元素,以及普通表单元素的标签。 非表单标签:主要用于生成...

    struts2标签+ tiles lib

    本文将深入探讨Struts2标签和Tiles库的使用,以及它们在实际开发中的应用。 1. **Struts2标签** Struts2提供了丰富的标签库,这些标签使得开发者能够在JSP页面中方便地实现业务逻辑和控制流程。例如: - `s:...

    Struts2之struts2标签库了解和使用案例struts025

    首先,Struts2标签库分为两大类:核心标签库和展示标签库。核心标签库提供了一组基础功能,如表单处理、控制流程、消息显示等,而展示标签库则包含更具体的UI元素,如数据展示、分页等。了解这些标签库的使用,可以...

    struts2 标签 tags API 参考文档

    参考文档是开发者理解和使用Struts2标签的关键资源,它详细阐述了每个标签的功能、属性、用法及示例,有助于快速学习和掌握Struts2的视图层开发。 通过深入学习和实践Struts2的标签API,开发者可以有效地提升Web...

    Struts 2标签的应用

    利用Struts 2标签创建ex3.jsp页面,此页面提交给tag.action,在structs.xml中配置这个action的转发视图为result.jsp在result.jsp页面中显示ex3.jsp页面控件的值。(源代码和实验报告)

    struts2标签库的使用pdf+Word格式

    Struts2标签库是Java Web开发中非常重要的一个部分,它是Apache Struts框架的一部分,用于简化JSP页面的编写,提高代码的可读性和可维护性。本资源包含了一个PDF和Word格式的文档,旨在详细介绍如何使用Struts2的...

    struts2标签详解与实例

    Struts2标签详解与实例 在Java Web开发中,Struts2框架因其强大的MVC(模型-视图-控制器)架构而备受青睐。Struts2提供了丰富的标签库,简化了视图层的开发,使开发者可以更加专注于业务逻辑。本文将深入探讨Struts...

    struts2 标签

    struts2 标签 Taglib (Struts2标签库)

Global site tag (gtag.js) - Google Analytics