J2EE系列之Struts2学习笔记(十六)--Struts2标签(数据标签、控制标签)

来源:互联网 发布:网络答题成绩单系统 编辑:程序博客网 时间:2024/05/24 07:35

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

一、数据标签,struts2的数据标签主要分为一下几种

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


下面分别介绍一下各个标签的用法:

1.property标签在前面使用OGNL获取对象值的时候已经使用过了,这里简单给出使用代码:

<%@ 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"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><%request.setAttribute("name","<font color=red>张三</font>");%></head><body><s:property value="#request.name" /><br/><s:property value="#request.name2" default="某某人"/><br/><s:property value="#request.name" default="某某人" escapeHtml="false"/><br/></body></html>

这里在request中保存了一个name对象,name对象的值是一个html表达式。第二种方式中设置了default属性,这个属性的意思是当取不到name2的值的时候,给name2设置默认值;第三个取法中设置了escapeHtml属性,这个属性的意思是“取消Html效果”,这里设置为false就是保留Html效果。程序运行结果为:


2.set标签,用来给变量赋值

<%@ 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"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><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/></body></html>

这里分别给变量i、a、p、r、s、app进行了赋值,scope属性指定了变量的作用范围。接下来使用property标签读取这些值。


3.Bean标签,这个标签能够定义javabean对象

3.1新建一个类Student,放在com.test.model包中:

package com.test.model;public class Student {private int id;private String name;private int age;public Student() {super();// TODO Auto-generated constructor stub}public Student(int id, String name, int age) {super();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;}}

bean标签使用方法为:

<%@ 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"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><s:bean name="com.test.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"/></body></html>

4.date标签,用来显示日期信息

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page import="java.util.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><%request.setAttribute("date",new Date());%></head><body>${date }<br/>当前日期:<s:date name="#request.date" format="yyyy-MM-dd"/></body></html>
这里把一个日期信息保存在了request中,并分别使用el表达式和date标签来显示这个日期。程序运行效果为:


5.Url&a 标签:超链接标签

<%@ 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"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><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></body></html>
这里使用了两种标签,第一种是使用了url和a组合完成超链接的功能,第二种是单独使用a标签完成超链接功能。


6.Include 标签:动态包含标签,能够在页面中动态的包含其它的页面。

<%@ 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"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><s:include value="head.html"></s:include></body></html>

二、控制标签,struts2的控制标签主要有一下几种


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

1.ifelse标签,条件判断标签:

<%@ 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"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><%int age=11;request.setAttribute("age",age);%></head><body><s:if test="#request.age<20">年龄小于20岁</s:if><s:elseif test="#request.age==20">年龄等于20岁</s:elseif><s:else>年龄大于20岁</s:else></body></html>

这里定义了一个age对象,使用ifelse标签对age的大小进行判断并输出其值。

2.Iterator 标签:遍历标签;

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page import="com.test.model.Student" %><%@ page import="java.util.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><%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);%></head><body><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></body></html>

这里定义了一个List类型的studentList对象,使用iterator标签来遍历输出其中的元素。

3.Generator 标签:分隔标签,作用与Java中的split函数差不多

<%@ 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"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><s:generator separator="," val="'张三,李四,王五'" var="nameList"></s:generator><s:iterator value="#nameList"><s:property/></s:iterator></body></html>

这里使用generator标签把以逗号分隔的字符串变量,并使用iterator标签把这些字符串输出。


4.Sort 标签:排序标签;

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page import="com.test.model.Student" %><%@ page import="java.util.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><%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);%></head><body><s:bean id="myComparator" name="com.test.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></body></html>

这里定义了一个List集合对象,使用sort标签对List中的对象按照年龄排序排序输出。这里需要使用的一个比较器,我们定义一个比较器如下:

package com.test.comparator;import java.util.Comparator;import com.test.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;}}

程序运行结果为:



5.Subset 标签:截取标签

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ page import="com.test.model.Student" %><%@ page import="java.util.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@taglib prefix="s" uri="/struts-tags" %><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><%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);%></head><body><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></body></html>

这里使用subset标签截取studentList1中从索引2开始的2个元素值输出

0 0
原创粉丝点击