1、set标签用于分配 值在指定范围内的变量,就是在前端创建一个参数并且用已有参数或者值域的数据为其赋值
属性及用途
- name:被创建的属性的键
- value:被该键说应用的对象
- scope:目标变量的作用范围(application/session/request/page/default)
2、push标签用来将值推到堆栈顶部,以便更加容易地访问
属性及用途
- value:将要被压入栈的值
3、if/elseif/else标签,跟java里面的if/elseif/else判断一样
属性及用途
- test:判断条件
4、iterator标签用来遍历值域中的数据
属性及用途
- value:将要被遍历的可遍历对象
- status:属性值直接填写”status”,在标签范围内可以通过${status.status属性值}来获取对应数据
- var:用来应用可变你对象当前元素的变量status属性值:
index:各次遍历的下标值,从零开始
count:当前遍历的下标值
first:当前元素是不是可遍历对象的第一个元素(true/false)
last:当前元素是不是可遍历对象的最后一个元素(true/false)
even:判断count属性的值是不是偶数(true/false)
odd:判断count属性的值是不是奇数(true/false)
modulus:这个值需要输一个int类型的输入值,返回值是count属性值除以输入值的余数
5、sort标签用于对可遍历对象的元素进行排序
属性及用途
- comparator:排序过程中使用的比较器
- source:将要排序的可遍历对象
- var:用来引用排序新产生的可遍历对象
6、struts.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.action.extension" value="action,do,"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="TestLabels" class="com.demo.sshtest.TestLabels" method="doLabels">
<result>/result.jsp</result>
</action>
<action name="TestLabels-Iterator" class="com.demo.sshtest.TestIterator" method="testActionIter">
<result>/result_iter.jsp</result>
</action>
</package>
</struts>
其中第一个action,set标签、push标签、if/elseif/else标签测试用,第二个action是测试iterator标签、sort标签用
7、set标签、push标签、if/elseif/else标签触发struts2.xml用的action(TestLabels .java)
package com.demo.sshtest;
public class TestLabels {
public int pid = 0;
public String pName = "";
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public String doLabels(){
return "success";
}
}
8、push标签需要用到的类(TestPush.jsp)
package com.demo.sshtest;
public class TestPush {
public int pushid = 0;
public String pushName = "";
public int getPushid() {
return pushid;
}
public void setPushid(int pushid) {
this.pushid = pushid;
}
public String getPushName() {
return pushName;
}
public void setPushName(String pushName) {
this.pushName = pushName;
}
}
9、测试iterator标签、sort标签时候要用到的action(TestIterator.java)
package com.demo.sshtest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class TestIterator {
public int IteratorId = 0;
public String IteratorName = "";
public int getIteratorId() {
return IteratorId;
}
public void setIteratorId(int iteratorId) {
IteratorId = iteratorId;
}
public String getIteratorName() {
return IteratorName;
}
public void setIteratorName(String iteratorName) {
IteratorName = iteratorName;
}
public TestIterator(){
}
public TestIterator(int IteratorId,String IteratorName){
super();
this.IteratorId = IteratorId;
this.IteratorName = IteratorName;
}
public List<TestIterator> testIterator = new ArrayList<TestIterator>();
public List<TestIterator> getTestIterator() {
return testIterator;
}
public void setTestIterator(List<TestIterator> testIterator) {
this.testIterator = testIterator;
}
public String testActionIter(){
testIterator.add(new TestIterator(1,"aaa"));
testIterator.add(new TestIterator(5,"eee"));
testIterator.add(new TestIterator(3,"ccc"));
testIterator.add(new TestIterator(4,"ddd"));
testIterator.add(new TestIterator(2,"bbb"));
return "success";
}
}
这里的testActionIter()是用来把模拟从数据库或其他途径获取数据,然后把数据放到List里面,等待iterator标签调用的时候测试遍历用,所以testIterator参数要写getter setter方法
10、测试sort标签需要用的类(TestComparator.java)
package com.demo.sshtest;
import java.util.Comparator;
public class TestComparator implements Comparator<TestIterator>{
public int compare(TestIterator o1, TestIterator o2) {
String o1Id = String.valueOf(o1.getIteratorId());
String o2Id = String.valueOf(o2.getIteratorId());
return o1Id.compareTo(o2Id);
}
}
这个类只是用来对比传入参数达到对比效果,继承Comparator接口
11、调用action页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.net.*,java.io.*,java.text.*, java.util.*" %>
<!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>
<form method="post" id="ActionForm" name="ActionForm" action="TestLabels.action">
pid:<input type="text" id="pid" name="pid" value="1">
pName:<input type="text" id="pName" name="pName" value="pName11111">
<input type="submit" value="submit">
</form>
</body>
</html>
测试set标签、push标签、if/elseif/else标签时,form的action地址是TestLabels.action,测试iterator标签、sort标签时,form的action地址是TestLabels-Iterator.action
12、set标签、push标签、if/elseif/else标签的结果显示页面(result.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.demo.sshtest.*" %>
<%@ page language="java" import="java.net.*,java.io.*,java.text.*, java.util.*" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<% request.setCharacterEncoding("UTF-8"); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
pid: <s:property value="pid"/>
<br><br>
pName: <s:property value="pName"/>
<br><br>
-------------------------s:set-------------------------
<br><br>
set标签用于分配值在指定范围内的变量
<br><br>
<s:set name="pNameChange1" value="pName" scope="request"></s:set>
pNameChange1:${requestScope.pNameChange1 }(scope="request")
<br><br>
<s:set name="pNameChange2" value="pName" scope="page"></s:set>
pNameChange2:${pageScope.pNameChange2 }(scope="page")
<br><br>
<s:set name="pNameChange3" value="pName" scope="application"></s:set>
pNameChange3:${applicationScope.pNameChange3 }(scope="application")
<br><br>
<s:set name="pNameChange4" value="pName" scope="session"></s:set>
pNameChange4:${sessionScope.pNameChange4 }(scope="session")
<br><br>
<s:set name="pNameChange5" value="pName" scope="default"></s:set>
pNameChange5:${pNameChange5 }(scope="default")
<br><br>
<s:set name="pNameChange6" value="pName" scope=""></s:set>
pNameChange6:${pNameChange6 }(scope="")
<br><br>
<s:set name="pNameChange7" value="pName"></s:set>
pNameChange7:${pNameChange7 }(没有 scope)
<br><br>
<s:set name="pNameChange8" value="'pName3423423423'"></s:set>
pNameChange8:${pNameChange8 }(自定义新参数名的值)
<br><br>
-------------------------s:set-------------------------
<br><br><br><br>
-------------------------s:push-------------------------
<br><br>
push标签被用来将值推到堆栈的顶部,以便它可以容易地访问或参考。
<br>
标签使用完之后,值会被弹出栈,就是只在标签内有效
<br><br>
<%
TestPush testPush = new TestPush();
testPush.setPushid(123);
testPush.setPushName("test_push_123");
request.setAttribute("testpush", testPush);
%>
<s:push value="#request.testpush">
pushName:${pushName}
</s:push>
<br><br>
-------------------------s:push-------------------------
<br><br><br><br>
-------------------------s:if s:else s:else if-------------------------
<br><br>
可以直接用值栈里面的值
<br><br>
<s:if test="pid>6">
pid大于6
</s:if>
<s:elseif test="#request.pid>4">
pid大于4小于6
</s:elseif>
<s:else>
pid小于4
</s:else>
<br><br>
-------------------------s:if s:else s:else if-------------------------
<br><br><br><br>
<br><br>
<s:debug></s:debug>
<br><br>
</body>
</html>
标签使用的一些简单注释都在对应位置了,注意的是
- push标签的数据,只有在标签范围内才有效
- if/elseif/else标签可以直接用值栈里面的值
13、iterator标签、sort标签的结果显示页面(result_iter.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="com.demo.sshtest.*" %>
<%@ page language="java" import="java.net.*,java.io.*,java.text.*, java.util.*" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<% request.setCharacterEncoding("UTF-8"); %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
-------------------------s:iterator-------------------------
<br><br>
一般情况下action传递结果到前台页面的时候,数据会存到域对象里面(request/session/application),这个时候用iterator就能按顺序遍历出来
<br>
标签使用完之后,值会被弹出栈,就是只在标签内有效
<br>
<%
//测试数据
//testIterator放到域对象,模拟遍历域对象数据
List<TestIterator> testIterator = new ArrayList<TestIterator>();
testIterator.add(new TestIterator(1,"AAAAAAAA"));
testIterator.add(new TestIterator(5,"EEEEEEEE"));
testIterator.add(new TestIterator(3,"CCCCCCCC"));
testIterator.add(new TestIterator(4,"DDDDDDDD"));
testIterator.add(new TestIterator(2,"BBBBBBBB"));
request.setAttribute("testIterator", testIterator);
%>
<s:iterator value="#request.testIterator">
${IteratorId} >>> ${IteratorName} <br>
</s:iterator>
<br><br>
<%
//从值栈中的testIterator,获取并且遍历域对象数据
%>
<s:iterator value="testIterator" status="status">
index:${status.index},count:${status.count } >>> ${IteratorId} - ${IteratorName} <br>
</s:iterator>
<br>
-------------------------s:iterator-------------------------
<br><br><br><br>
-------------------------s:sort-------------------------
<br><br>
<%
TestComparator comparator = new TestComparator();
request.setAttribute("comparator", comparator);
%>
<s:sort comparator="#request.comparator" source="testIterator" var="testIterator2"></s:sort>
<s:iterator value="#attr.testIterator2">
${IteratorId} >>> ${IteratorName} <br>
</s:iterator>
<br>
-------------------------s:sort-------------------------
<br><br>
<br><br>
<s:debug></s:debug>
<br><br>
</body>
</html>
14、项目目录
15、set标签、push标签、if/elseif/else标签测试结果
16、iterator标签、sort标签测试结果
17、demo:http://download.csdn.net/download/qq_22778717/10247688