您的位置:首页 > 编程语言 > Java开发

Struts2自学入门(六)——OGNL数据标签和控制标签

2016-09-21 22:39 459 查看
一、数据标签

1.property标签

<%
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/>


当值不存在时 将使用default的值,escapeHtml可以将带有HTML标签的字符串转化为HTML输出

2.set标签

<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/>


    3.Bean标签

<s:bean name="com.java1234.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"/>

4.date标签

<%
request.setAttribute("date",new Date());
%>

<body>
${date }<br/>
当前日期:<s:date name="#request.date" format="yyyy-MM-dd"/>
</body>
5.debug标签
debug标签主要用于辅助测试,它在页面上生成一个超链接,通过该链接可以查看ValueStack和Stack Context
中的所有值信息。使用debug标签只有一个id属性,这个属性仅仅是该元素一个引用id。 在页面上增加<s:debug/>标签,通过debug标签,可以看的系统中ValueStack离得全部信息,并可以看到Stack
Context中的属性。

<s:debug></s:debug>
6.url_a标签

<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>


7.include标签

<s:include value="head.html"></s:include>


二、控制标签


1、ifelse标签——条件判断

<s:if test="#request.age<20">
年龄小于20岁
</s:if>
<s:elseif test="#request.age==20">
年龄等于20岁
</s:elseif>
<s:else>
年龄大于20岁
</s:else>


2、iterator标签——遍历标签

status属性:可选属性,该属性在迭代时会产生一个IteratorStatus对象,该对象可以判断当前元素的位置,包含了以下属性方法:
int getCount(); 迭代元素个数
int getIndex(); 迭代元素当前索引
boolean getFirst(); 是否为第一个
boolean getEven(); 是否为偶
boolean getLast(); 是否最后一个
bolean getOdd(); 是否为奇 (ps:#status.odd)

<%
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>


3、append标签——叠加

<%
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&
4000
gt;
<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>
4、generator标签——分隔标签

<s:generator separator="," val="'张三,李四,王五'" var="nameList"></s:generator>

<s:iterator value="#nameList">
<s:property/>
</s:iterator>


5、Merge标签——组合标签(混合叠加)

 
<%
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>

6、Sort标签——排序标签

<%
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.java1234.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>

自定义排序规则 myComparator

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;
}

}


7、subset标签——截取标签

<%
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JAVA Struts2 Web后台 ognl