第 24讲 struts2标签-控制标签

Struts2 控制标签接上一节
Ifelse 标签:条件判断标签;在request设置值,
<%@ 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=2;
    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>
Iterator 标签:遍历标签;
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cruise.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>

Append 标签:叠加标签;将两个List放在一起,形成一个List,前提是两个List集合的对象有相同的属性,合并之后才能在一起遍历
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cruise.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>
Generator 标签:分隔标签;字符串的分割,和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>
</table>
</body>
</html>
Merge 标签:组合标签;将两个List集合混合,顺序发生了变化。
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cruise.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>
Sort 标签:排序标签;需要引入一个比较器,
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cruise.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.cruise.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>

Subset 标签:截取标签,类似与substring()方法,获取List集合中的数据,
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.cruise.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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值