---
title: "尚学堂struts2 ognl讲解"
author: "deejay"
date: "2011-01-09T17:28:02Z"
tags: []
categories: ["企业架构"]
slug: "struts2-ognl"
draft: false
lastmod: "2013-07-16T11:16:41Z"
id: 866394
url: "https://deejay.iteye.com/blog/866394"
---

01. &lt;?xml version="1.0" encoding="GB18030" ?&gt;
02. &lt;%@ page language="java" contentType="text/html; charset=GB18030"
03. pageEncoding="GB18030"%&gt;
04. &lt;%@ taglib uri="/struts-tags" prefix="s" %&gt;
05. &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
06. &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
07. &lt;head&gt;
08. &lt;meta http-equiv="Content-Type" content="text/html; charset=GB18030" /&gt;
09. &lt;title&gt;OGNL表达式语言学习&lt;/title&gt;
10. &lt;/head&gt;
11. &lt;body&gt;
12. &lt;ol&gt;
13. &lt;li&gt;访问值栈中的action的普通属性: username = &lt;s:property value="username"/&gt; &lt;/li&gt;
14. &lt;li&gt;访问值栈中对象的普通属性(get set方法)：&lt;s:property value="user.age"/&gt; | &lt;s:property value="user\['age']"/&gt; | &lt;s:property value="user\[\\"age\\"]"/&gt; | wrong: &lt;%--&lt;s:property value="user\[age]"/&gt;--%&gt;&lt;/li&gt;
15. &lt;li&gt;访问值栈中对象的普通属性(get set方法): &lt;s:property value="cat.friend.name"/&gt;&lt;/li&gt;
16. &lt;li&gt;访问值栈中对象的普通方法：&lt;s:property value="password.length()"/&gt;&lt;/li&gt;
17. &lt;li&gt;访问值栈中对象的普通方法：&lt;s:property value="cat.miaomiao()" /&gt;&lt;/li&gt;
18. &lt;li&gt;访问值栈中action的普通方法：&lt;s:property value="m()" /&gt;&lt;/li&gt;
19. &lt;hr /&gt;
20. &lt;li&gt;访问静态方法：&lt;s:property value="@com.bjsxt.struts2.ognl.S@s()"/&gt;&lt;/li&gt;
21. &lt;li&gt;访问静态属性：&lt;s:property value="@com.bjsxt.struts2.ognl.S@STR"/&gt;&lt;/li&gt;
22. &lt;li&gt;访问Math类的静态方法：&lt;s:property value="@@max(2,3)" /&gt;&lt;/li&gt;
23. &lt;hr /&gt;
24. &lt;li&gt;访问普通类的构造方法：&lt;s:property value="new com.bjsxt.struts2.ognl.User(8)"/&gt;&lt;/li&gt;
25. &lt;hr /&gt;
26. &lt;li&gt;访问List:&lt;s:property value="users"/&gt;&lt;/li&gt;
27. &lt;li&gt;访问List中某个元素:&lt;s:property value="users\[1]"/&gt;&lt;/li&gt;
28. &lt;li&gt;访问List中元素某个属性的集合:&lt;s:property value="users.{age}"/&gt;&lt;/li&gt;
29. &lt;li&gt;访问List中元素某个属性的集合中的特定值:&lt;s:property value="users.{age}\[0]"/&gt; | &lt;s:property value="users\[0].age"/&gt;&lt;/li&gt;
30. &lt;li&gt;访问Set:&lt;s:property value="dogs"/&gt;&lt;/li&gt;
31. &lt;li&gt;访问Set中某个元素:&lt;s:property value="dogs\[1]"/&gt;&lt;/li&gt;&lt;!--set是没有顺序的，所以取下标值是没有意义的--&gt;
32. &lt;li&gt;访问Map:&lt;s:property value="dogMap"/&gt;&lt;/li&gt;
33. &lt;li&gt;访问Map中某个元素:&lt;s:property value="dogMap.dog101"/&gt; | &lt;s:property value="dogMap\['dog101']"/&gt; | &lt;s:property value="dogMap\[\\"dog101\\"]"/&gt;&lt;/li&gt;&lt;!--取出key为dog101的值--&gt;
34. &lt;li&gt;访问Map中所有的key:&lt;s:property value="dogMap.keys"/&gt;&lt;/li&gt;
35. &lt;li&gt;访问Map中所有的value:&lt;s:property value="dogMap.values"/&gt;&lt;/li&gt;
36. &lt;li&gt;访问容器的大小：&lt;s:property value="dogMap.size()"/&gt; | &lt;s:property value="users.size"/&gt; &lt;/li&gt;&lt;!--不加ku9ohao也可以访问--&gt;
37. &lt;hr /&gt;
38. &lt;li&gt;投影(过滤)：&lt;s:property value="users.{?#this.age==1}\[0]"/&gt;&lt;/li&gt;&lt;!--格式固定，**this**指循环过程中的每一个对象。取出users里age等于1的集合里面的第一个元素--&gt;
39. &lt;li&gt;投影：&lt;s:property value="users.{^#this.age&gt;1}.{age}"/&gt;&lt;/li&gt;&lt;!--^指第一个元素--&gt;
40. &lt;li&gt;投影：&lt;s:property value="users.{$#this.age&gt;1}.{age}"/&gt;&lt;/li&gt;&lt;!--$指结尾的元素--&gt;
41. &lt;li&gt;投影：&lt;s:property value="users.{$#this.age&gt;1}.{age} == null"/&gt;&lt;/li&gt;&lt;!--查看age&gt;1的user的age是否为空--&gt;
42. &lt;hr /&gt;
43. &lt;li&gt;\[]:&lt;s:property value="\[0].username"/&gt;&lt;/li&gt;
44. &lt;!--指value stack栈顶的那个Action，因为action在栈顶。0值value stack从上往下数的第几0个元素 --&gt;
45. &lt;!--\[0]代表action的位置，而不是action那个对象，\[0]代表从0往下数到栈底的对象的集合。--&gt;
46. &lt;!--\[0].username，如果有多个action，在第一个action里找不到\[0].username，会往下找--&gt;
47. &lt;/ol&gt;
48. &lt;s:debug&gt;&lt;/s:debug&gt;
49. &lt;s:select list="{'aa','bb','cc'}" theme="simple" headerKey="00" headerValue="00"&gt;&lt;/s:select&gt;
50. &lt;s:select theme="simple" list="#{1:'★',2:'★★',3:'★★★',4:'★★★★',5:'★★★★★'}" name="ponderance" id="ponderance" value="ponderance" headerKey="0" headerValue="请选择"/&gt;

[http://localhost:8080/demo/index.jsp?flag=kkkk](http://localhost:8080/demo/index.jsp?flag=kkkk)     
&lt;s:property value="#parameters.flag" /&gt;     
 

这里有三种方法  
&lt;s:property value="#parameters.mySessionPropKey"/&gt; or     
&lt;s:property value="#parameters\['mySessionPropKey']"/&gt; or     
&lt;s:property value="#parameters\['myRequestPropKey']"/&gt;     
 

从上面的答案我们可以推测到request，session ,application中的属性值也可以用同样的方法获取  
如：   
  request.----&gt;    
&lt;s:property value="#request.mySessionPropKey"/&gt; or     
&lt;s:property value="#request\['mySessionPropKey']"/&gt; or     
&lt;s:property value="#request\['myRequestPropKey']"/&gt;    
session----&gt;    
&lt;s:property value="#session.mySessionPropKey"/&gt; or     
&lt;s:property value="#request\['mySessionPropKey']"/&gt; or     
&lt;s:property value="#request\['myRequestPropKey']"/&gt;    
application---&gt;    
&lt;s:property value="#application.mySessionPropKey"/&gt; or     
&lt;s:property value="#application\['mySessionPropKey']"/&gt; or     
&lt;s:property value="#application\['myRequestPropKey']"/&gt;     
   
参数资料：[http://struts.apache.org/2.1.6/docs/ognl.html](http://struts.apache.org/2.1.6/docs/ognl.html)

 

 

1. &lt;/body&gt;
2. &lt;/html&gt;