OGNL 表达式学习
(文章源自:http://www.iteye.com/topic/646851)
运行结果
[img]http://dl.iteye.com/upload/attachment/504552/b6fde85e-bb29-3531-806c-663d14de27a3.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/504556/64264f4e-bfa7-3c53-b2f6-0580607500e4.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/504554/bb189c5b-2359-337f-8ef2-3c2624c9ee84.jpg[/img]
(文章源自:http://www.iteye.com/topic/646851)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.rao.struts2.bean.Sex"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'OGNL1.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setAttribute("req", "request scope");
request.getSession().setAttribute("sess", "session scope");
request.getSession().getServletContext().setAttribute("app",
"aplication scope");
%>
1.通过ognl表达式获取 属性范围中的值
<br>
<s:property value="#request.req" />
<br />
<s:property value="#session.sess" />
<br />
<s:property value="#application.app" />
<br />
<hr>
2.通过<SPAN style="BACKGROUND-COLOR: #fafafa">ognl表达式创建list 集合 ,并且遍历出集合中的值
<br>
<s:set name="list" value="{'eeeee','ddddd','ccccc','bbbbb','aaaaa'}"></s:set>
<s:iterator value="#list" var="o">
<!-- ${o }<br/> -->
<s:property />
<br />
</s:iterator>
<br />
<hr>
3.通过ognl表达式创建Map 集合 ,并且遍历出集合中的值
<br>
<s:set name="map"
value="#{'1':'eeeee','2':'ddddd','3':'ccccc','4':'bbbbb','5':'aaaaa'}"></s:set>
<s:iterator value="#map" var="o">
<!-- ${o.key }->${o.value }<br/> -->
<!-- <s:property value="#o.key"/>-><s:property value="#o.value"/><br/> -->
<s:property value="key" />-><s:property value="value" />
<br />
</s:iterator>
<br />
<hr>
4.通过ognl表达式 进行逻辑判断
<br>
<s:if test="'aa' in {'aaa','bbb'}">
aa 在 集合{'aaa','bbb'}中;
</s:if>
<s:else>
aa 不在 集合{'aaa','bbb'}中;
</s:else>
<br />
<s:if test="#request.req not in #list">
不 在 集合list中;
</s:if>
<s:else>
在 集合list中;
</s:else>
<br />
<hr>
5.通过ognl表达式 的投影功能进行数据筛选
<br>
<s:set name="list1" value="{1,2,3,4,5}"></s:set>
<s:iterator value="#list1.{?#this>2}" var="o">
<!-- #list.{?#this>2}:在list1集合迭代的时候,从中筛选出当前迭代对象>2的集合进行显示 -->
${o }<br />
</s:iterator>
<br />
<hr>
5.通过ognl表达式 访问某个类的静态方法和值
<br>
<s:property value="@java.lang.Math@floor(32.56)" />
<s:property value="@com.rao.struts2.action.OGNL1Action@aa" />
<br />
<br />
<hr>
6.ognl表达式 迭代标签 详细
<br>
<s:set name="list2"
value="{'aa','bb','cc','dd','ee','ff','gg','hh','ii','jj'}"></s:set>
<table border="1">
<tr>
<td>
索引
</td>
<td>
值
</td>
<td>
奇?
</td>
<td>
偶?
</td>
<td>
首?
</td>
<td>
尾?
</td>
<td>
当前迭代数量
</td>
</tr>
<s:iterator value="#list2" var="o" status="s">
<tr bgcolor="<s:if test="#s.even">pink</s:if>">
<td>
<s:property value="#s.getIndex()" />
</td>
<td>
<s:property />
</td>
<td>
<s:if test="#s.odd">Y</s:if>
<s:else>N</s:else>
</td>
<td>
<s:if test="#s.even">Y</s:if>
<s:else>N</s:else>
</td>
<td>
<s:if test="#s.first">Y</s:if>
<s:else>N</s:else>
</td>
<td>
<s:if test="#s.isLast()">Y</s:if>
<s:else>N</s:else>
</td>
<td>
<s:property value="#s.getCount()"/>
</td>
</tr>
</s:iterator>
</table>
<br>
<hr>
7.ognl表达式: if/else if/else 详细<br>
<% request.setAttribute("aa",0); %>
<s:if test="#request.aa>=0 && #request.aa<=4">
在0-4之间;
</s:if>
<s:elseif test="#request.aa>=4 && #request.aa<=8">
在4-8之间;
</s:elseif>
<s:else>
大于8;
</s:else>
<br>
<hr>
8.ognl表达式: url 详细<br>
<% request.setAttribute("aa","sss"); %>
<s:url action="testAction" namespace="/aa/bb">
<s:param name="aa" value="#request.aa"></s:param>
<s:param name="id">100</s:param>
</s:url>
<br/>
<s:set name="myurl" value="'http://www.baidu.com'"></s:set>
value以字符处理: <s:url value="#myurl"></s:url><br>
value明确指定以ognl表达式处理: <s:url value="%{#myurl}"></s:url>
<br>
<hr>
9.ognl表达式: checkboxlist 详细<br>
1> .list 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
name:checkboxlist的名字<br>
list:checkboxlist要显示的列表<br>
value:checkboxlist默认被选中的选项,checked=checked<br>
<s:checkboxlist name="checkbox1" list="{'上网','看书','爬山','游泳','唱歌'}" value="{'上网','看书'}" ></s:checkboxlist>
<br>
以上生成代码:<br>
<xmp>
<input type="checkbox" name="checkbox1" value="上网" id="checkbox1-1" checked="checked"/>
<label for="checkbox1-1" class="checkboxLabel">上网</label>
<input type="checkbox" name="checkbox1" value="看书" id="checkbox1-2" checked="checked"/>
<label for="checkbox1-2" class="checkboxLabel">看书</label>
<input type="checkbox" name="checkbox1" value="爬山" id="checkbox1-3"/>
<label for="checkbox1-3" class="checkboxLabel">爬山</label>
<input type="checkbox" name="checkbox1" value="游泳" id="checkbox1-4"/>
<label for="checkbox1-4" class="checkboxLabel">游泳</label>
<input type="checkbox" name="checkbox1" value="唱歌" id="checkbox1-5"/>
<label for="checkbox1-5" class="checkboxLabel">唱歌</label>"
</xmp>
2> .Map 生成;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
name:checkboxlist的名字<br>
list:checkboxlist要显示的列表<br>
listKey:checkbox 的value的值<br>
listValue:checkbox 的lablel(显示的值)<br>
value:checkboxlist默认被选中的选项,checked=checked<br>
<s:checkboxlist name="checkbox2" list="#{1:'上网',2:'看书',3:'爬山',4:'游泳',5:'唱歌'}" listKey="key" listValue="value" value="{1,2,5}" ></s:checkboxlist>
<br>
以上生成代码:<br>
<xmp>
<input type="checkbox" name="checkbox2" value="1" id="checkbox2-1" checked="checked"/>
<label for="checkbox2-1" class="checkboxLabel">上网</label>
<input type="checkbox" name="checkbox2" value="2" id="checkbox2-2" checked="checked"/>
<label for="checkbox2-2" class="checkboxLabel">看书</label>
<input type="checkbox" name="checkbox2" value="3" id="checkbox2-3"/>
<label for="checkbox2-3" class="checkboxLabel">爬山</label>
<input type="checkbox" name="checkbox2" value="4" id="checkbox2-4"/>
<label for="checkbox2-4" class="checkboxLabel">游泳</label>
<input type="checkbox" name="checkbox2" value="5" id="checkbox2-5" checked="checked"/>
<label for="checkbox2-5" class="checkboxLabel">唱歌</label>
</xmp>
<hr>
10.ognl表达式: s:radio 详细<br>
<%
Sex sex1 = new Sex(1,"男");
Sex sex2 = new Sex(2,"女");
List<Sex> list = new ArrayList<Sex>();
list.add(sex1);
list.add(sex2);
request.setAttribute("sexs",list);
%>
这个与checkboxlist差不多;<br>
1>.如果集合为javabean:<s:radio name="sex" list="#request.sexs" listKey="id" listValue="name"></s:radio><br>
2>.如果集合为list:<s:radio name="sexList" list="{'男','女'}"></s:radio><br>
3>.如果集合为map:<s:radio name="sexMap" list="#{1:'男',2:'女'}" listKey="key" listValue="value"></s:radio><br>
<hr>
11.ognl表达式: s:select 详细<br>
这个与s:checkboxlist差不多;<br>
1>.如果集合为javabean:<s:select name="sex" list="#request.sexs" listKey="id" listValue="name"></s:select><br>
2>.如果集合为list:<s:select name="sexList" list="{'男','女'}"></s:select><br>
3>.如果集合为map:<s:select name="sexMap" list="#{1:'男',2:'女'}" listKey="key" listValue="value"></s:select><br>
到此主要的ognl</SPAN>标签已经介绍完毕...由于表单标签相对简单不介绍了....
</body>
</html>
运行结果
[img]http://dl.iteye.com/upload/attachment/504552/b6fde85e-bb29-3531-806c-663d14de27a3.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/504556/64264f4e-bfa7-3c53-b2f6-0580607500e4.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/504554/bb189c5b-2359-337f-8ef2-3c2624c9ee84.jpg[/img]