打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
OGNL 摘要
在struts2 中使用OGNL时经常会遇到“#”,"%","$",具体用法绕到头晕 ,在网上找到下面的介绍 ,故整理摘记,防止一晕到底。
“#”主要有3种用途,
1.访问OGNL上下文和Action上下文 ,相当于ActionContext.getContext();下面是几个ActionContext 中有用的属性
parameter :    #parameter.id[0]   ~~request.getParameter("id");
request     :    #request.id            ~~request.getAttribute("id");
session     :    #session.id            ~~session.getAttribute("id");
application:    #application.id       ~~application.getAttribute("id");
attr            :    #attr.id                   ~~依次按照request ->session ->application 顺序查找id属性的值。

2用于过滤和投影集合,如 books.{?#this.price <100};

3.够着Map ,如  #{‘foo’:‘ak’,'boo':'kk'};

"%"用途是在标志属性为字符串的时候用于计算OGNL表达式的值。
如 <s:url value = "%{#foobar['foo']}"/>

$主要有两种用途
1.在国际化资源文件中引用OGNL表达式。
2.在struts2配置文件中使用OGNL表达式。


找到的一段示例代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.     <title></title>  
  7.     <meta http-equiv="pragma" content="no-cache">  
  8.     <meta http-equiv="cache-control" content="no-cache">  
  9.     <meta http-equiv="expires" content="0">      
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11.     <meta http-equiv="description" content="This is my page">  
  12.   </head>  
  13.     
  14.   <body>  
  15.     ognl.jsp.....................<br>  
  16.       编号: <s:property value="id"/><br>  
  17.       地址:<s:property value="address"/><br>  
  18.   User对象:<s:property value="user"/><br>  
  19.   userName:<s:property value="user.userName"/><br>  
  20.   userPwd:<s:property value="user.userPwd"/><br>  
  21.   userList集合:<s:property value="userList"/><br>  
  22.   list size:<s:property value="userList.size"/><br>  
  23.  第一个对象:<s:property value="userList[0]"/><br>  
  24.   投影<br>   
  25.  取得List中所有的姓名:<s:property value="userList.{userName}"/><br>  
  26.   取得List中第一个对象的姓名:<s:property value="userList.{userName}[0]"/>    
  27.  输出List中姓名为tom的所有用户对象:<br>  
  28.   <s:property value="userList.{?#this.userName=='tom'}"/><br>  
  29.  输出List中姓名为tom的第一个用户对象:<br>  
  30.   <s:property value="userList.{^#this.userName=='tom'}"/><br>  
  31.  输出List中姓名为tom的最后一个用户对象:<br>  
  32.   <s:property value="userList.{$#this.userName=='tom'}"/><br>  
  33.     
  34.   OGNL操作Map集合--------------------<br>  
  35.   输出Map中的key:<s:property value="userMap.keys"/><br>  
  36.  输出Map中的value:<s:property value="userMap.values"/><br>  
  37. 输出Map中的数量:<s:property value="userMap.size"/><br>  
  38. 输出key(tom)对应的value:<s:property value="userMap['tom'].{id}[0]"/><br>---  
  39. 判断Map是否为empty:<s:property value="userMap.isEmpty"/><br>  
  40.   
  41. 动态创建List和Map ---------------------------<br>  
  42. 动态创建List:<s:property value="{'aa','bb','cc'}"/>  
  43. 动态创建Map:<s:property value="#{'aa':'aa','bb':'bb'}"/>  
  44. <br>  
  45. 获取request  session  Application中的值--------------------------<br>   
  46. 获取Session中的值:<br>  
  47. <s:property value="#session.sessionKey"/><br>  
  48. 获取reuqst中的值:<br>  
  49. <s:property value="#request.requestKey"/><br>  
  50. 获取Application中的值:<br>  
  51. <s:property value="#application.applicationKey"/>  
  52.   </body>  
  53. </html> 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Struts中OGNL与EL表达式
<s:property>的用法
Struts2 ONGL表达式
5、struts2值栈、命名参数与OGNL应用开发
OGNL表达式总结
Java程序员从笨鸟到菜鸟之(四十九)细谈struts2(十一)OGNL表达式的基本语法和...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服