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

struts2学习(8)struts标签1(数据标签、控制标签)

时间:2017-07-01 20:52:43      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:lis   tab   cape   bug   return   分享   sort   star   space   

一、struts2标签简介;                      

技术分享

struts标签很多,功能强大,这是优点;
但是缺点的话,性能方面可能会,各方面速度啊啥的会降低;有人比较测试,struts性能比jstl低很多;
 
二、struts2数据标签:                            
技术分享
技术分享
com.cy.model.Student.java:
技术分享
package com.cy.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;
    }
}
View Code

dataTag.jsp:

技术分享
<body>
<h>数据标签</h>
<hr/>
<a href="data/property.jsp" target="_blank">property标签</a><br/>
<a href="data/set.jsp" target="_blank">set标签</a><br/>
<a href="data/bean.jsp" target="_blank">bean标签</a><br/>
<a href="data/date.jsp" target="_blank">date标签</a><br/>
<a href="data/debug.jsp" target="_blank">debug标签</a><br/>
<a href="data/url_a.jsp" target="_blank">url_a标签</a><br/>
<a href="data/include.jsp" target="_blank">include标签</a><br/>
</body>
View Code

property.jsp:

技术分享
<%@ 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>
View Code

set.jsp:

技术分享
<%@ 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/><!-- action范围相当于值栈里面 -->
<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>
View Code

bean.jsp:

技术分享
<%@ 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.cy.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>
View Code

data.jsp:

技术分享
<%@ 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/>
<!-- 这种格式化时间的jstl也是有的 -->
当前日期:<s:date name="#request.date" format="yyyy-MM-dd"/><br>
当前日期:<s:date name="#request.date" format="yyyy/MM/dd"/>
</body>
</html>
View Code

debug.jsp:

技术分享
<%@ 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>
<!-- 相当于值栈debug 可以看到valueStack中的值
     可以看到attr,request,session,application等...中的值
 -->
<s:debug></s:debug>
</body>
</html>
View Code

url_a.jsp:

技术分享
<%@ 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相当于定义一个url变量 -->
<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>
View Code

include.jsp:和head.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">
<%@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>
<!-- 相当于jsp:include -->
<s:include value="head.html"></s:include>
</body>
</html>
View Code
技术分享
<body>
头部
</body>
View Code

测试结果:

技术分享

property标签结果:      

技术分享

set标签结果:          

技术分享

bean标签结果:                    

技术分享

date标签结果:                      

技术分享

debug标签结果:                      

技术分享

url_a标签结果:                        

技术分享

include标签结果:                      

技术分享

 

 

 三、struts2控制标签:                                              

技术分享

merge标签和append标签是有区别的;

可以看结果显示出来的区别;
append是将list1和list2合并到一起;
merge是组合,有点像取list1中的一个元素,再取list2中的一个元素,....最后将这两个list中的元素合并到一个list里面;
 
subset:count="2" start="2" 索引从2开始,取两条数据;
 

 技术分享

com.cy.comparator.MyComparator.java:

技术分享
package com.cy.comparator;

import java.util.Comparator;

import com.cy.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;
    }

}
View Code

controlTag.jsp:

技术分享
<body>
<h>控制标签</h>
<hr/>
<a href="control/ifelse.jsp" target="_blank">ifelse标签</a><br/>
<a href="control/iterator.jsp" target="_blank">iterator标签</a><br/>
<a href="control/append.jsp" target="_blank">append标签</a><br/>
<a href="control/generator.jsp" target="_blank">generator标签</a><br/>
<a href="control/merge.jsp" target="_blank">merge标签</a><br/>
<a href="control/sort.jsp" target="_blank">sort标签</a><br/>
<a href="control/subset.jsp" target="_blank">subset标签</a><br/>
</body>
View Code

技术分享

ifelse.jsp:                          

技术分享
<%@ 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>
View Code

技术分享

iterator.jsp:            

技术分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cy.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>
View Code

技术分享

append.jsp:                      

技术分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cy.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>();
    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);
%>
</head>
<body>
<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>
</body>
</html>
View Code

技术分享

generator.jsp:                      

技术分享
<%@ 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>
View Code

技术分享

merge.jsp:                    

技术分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cy.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>();
    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);
%>
</head>
<body>
<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>
</body>
</html>
View Code

技术分享

sort.jsp:                        

技术分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cy.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.cy.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>
View Code

技术分享

subset.jsp:                      

技术分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cy.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>
View Code

技术分享

 

 

--------------

struts2学习(8)struts标签1(数据标签、控制标签)

标签:lis   tab   cape   bug   return   分享   sort   star   space   

原文地址:http://www.cnblogs.com/tenWood/p/7103350.html

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