现在的位置: 首页 > 综合 > 正文

Struts2 学习笔记14 OGNL part3

2018年04月04日 ⁄ 综合 ⁄ 共 2908字 ⁄ 字号 评论关闭

  OGNL这部分主要就是配置的方法,虽然不难但是比较繁琐,大家可以先做了解,等到需要用到的时候再查询。

接下来要说的是访问普通类的构造方法,很简单就不上代码了,访问普通类的构造方法:<s:property value="new com.tfj.struts2.ognl.User(9)"/>这样就可以可。

  要详细说的是访问集合。还是先上ognl.jsp。9-19是访问集合的ognl表达式。


ognl.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Ognl表达式学习</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>
    <ol>
    	<li>访问值栈中的action的普通属性:username=<s:property value="username"/></li>
    	<li>访问值栈中对象的普通属性:<s:property value="user.age"/></li>
    	<li>访问值栈中对象的普通属性:<s:property value="cat.friend.name"/></li>
    	<hr/>
    	<li>访问action的属性的方法:<s:property value="password.length()"/></li>
    	<li>访问静态的属性:<s:property value="@com.tfj.struts2.ognl.S@STR"/></li>
    	<li>访问静态的方法:<s:property value="@com.tfj.struts2.ognl.S@s()"/></li>
    	<li>访问Math类:<s:property value="@@max(2,3)"/>
    	<hr/>
    	<li>访问普通类的构造方法:<s:property value="new com.tfj.struts2.ognl.User(9)"/></li>
    	<hr>
    	<li>访问List:<s:property value="users"/></li>
    	<li>访问List中的某个元素:<s:property value="users[1]"/></li>
    	<li>访问List中某个元素某个属性的集合:<s:property value="users.{age}"/></li>
    	<li>访问List中某个元素某个属性集合中的特定值:<s:property value="users.{age}[0]"/>|<s:property value="users[0].age"/></li>
    	<li>访问set:<s:property value="dogs"/></li>
    	<li>访问set中某个元素:<s:property value="dogs[1]"/></li>
    	<li>访问Map:<s:property value="dogmap"/></li>
    	<li>访问Map中某个元素:<s:property value="dogmap.dog101"/></li>
    	<li>访问Map中所有key:<s:property value="dogmap.keys"/></li>
    	<li>访问Map中所有value:<s:property value="dogmap.values"/></li>
    	<li>访问容器大小:<s:property value="dogmap.size()"/>|<s:property value="dogs.size"/>
    	
    	
    	
    </ol>
    <s:debug></s:debug>
  </body>
</html>

然后我挑其中的一些需要注意的地方来讲一下,为了和上面的截图对应起来,我就用截图中的序号。

9.访问List 直接访问List的名称就可以了。

10.访问List中的某个元素,用中括号。

11.访问List中某个元素某个属性的集合,用大括号把属性名括起来。

12.访问List中某个元素某个属性集合中的特定值,有两种方式,我们习惯于用后一种。

13.访问set,访问set名。
14.访问set中某个元素,因为set是无序的,无法访问。
15.访问Map,访问Map名称。
16.访问Map中某个元素,直接访问。
17.访问Map中所有key,加keys。

18.访问Map中所有value,加values。

19.访问容器大小,有两种方式size带不带括号都行。


接下来要说的就是投影了,简单说就是过滤,选择集合中符合条件的显示出来。

<li>投影:<s:property value="users.{?#this.age==1}.{age}"/></li>
<li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>
<li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>
<li>投影:<s:property value="users.{$#this.age>1}==null"/>


20.显示users里age为1的age集合。

21.^表示第一个,表达式表示第一个age大于1,显示age的集合。

22.$表示最后一个,表达式表示最后个age大于1,显示age的集合。

23.这是个判断,很好理解。

下一部分就是中括号表达式。

<li>[]:<s:property value="[0]"/>

会得到

这个表达式会访问对应的action,[]:<s:property value="[0].username"/>就可以得到u。

还有如果第一个action里没有username,他就会找第二个action,在服务器跳转的时候,值栈里有多个action。



抱歉!评论已关闭.