struts2学习之struts标签(九)

struts2标签
Struts2自己封装了一套标签,比jstl强大,而且与Struts2中的其他功能无缝结合,但相比于jstl标签,速度稍微慢点
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>
  • 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>
  • Bean标签:定义javaBean对象
<%@ 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.newbeedaly.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>
  • 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>
  • Debug标签:调试标签(显示作用域的值)
<%@ 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:debug></s:debug>
</body>
</html>
  • 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>
  • 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>

head.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
头部
</body>
</html>

2.控制标签

  • 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>
  • Iterator标签:遍历标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.newbeedaly.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标签:叠加标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.newbeedaly.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标签:分隔标签
<%@ 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标签:组合标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.newbeedaly.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.newbeedaly.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.newbeedaly.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:截取标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.newbeedaly.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>

3.界面标签

  1. Form标签:表单提交标签
<%@ 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:form action="hello" method="post" namespace="/foreground">
</s:form>
</body>
</html>
  1. Text标签:文本标签
<%@ 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:textfield name="userName"></s:textfield><br/>
密码:<s:password name="password"></s:password><br/>
备注:<s:textarea name="desc"></s:textarea><br/>
</body>
</html>
  1. Radios标签:单选标签
<%@ 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:radio list="#{0: '男 ', 1:'女 '}" name="sex" value="0" /> 
</body>
</html>
  1. Checkboxlist标签:复选框标签
<%@ 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:checkboxlist list="#{0: '游泳', 1:'唱歌 ',2:'跳舞'}" name="hobby" value="1" /> 
</body>
</html>
  1. select标签:下拉框标签
<%@ 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:select list="#{0: '游泳', 1:'唱歌 ',2:'跳舞'}" name="hobby" value="1" multiple="true"/> 
</body>
</html>

4.其他标签

  • Updownselect标签:选择框上下移动标签
<%@ 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:updownselect 
    list="#{0:'游泳', 1:'唱歌', 2:'跳舞'}"
    name="hobby" 
    headerKey="-1"
    headerValue="请选择" 
    emptyOption="true"
    allowMoveUp="true" 
    allowMoveDown="true" 
    allowSelectAll="true"
    moveUpLabel="向上" 
    moveDownLabel="向下"
    selectAllLabel="全选" /> 
</body>
</html>
  • Optiontransferselect标签:操作选择框标签
<%@ 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: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>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值