OGNL表达式

 

 1  2     OGNL是从ActionContext中获取数据的。
 3     
 4     ActionContext的结构:
 5         ValueStack:
 6             List:动作类放在此处。取存放在ValueStack中的root的对象的属性,直接写即可
 7             
 8             访问以下内容中的对象要使用#+(范围)session, 还有 列出的所有的#加上key属性取出值
 9         application:ServletContext中的那个Map
10         session:HttpSession中的那个Map
11         request:ServletRequest中的那个Map
12         parameters:请求参数的那个Map。(如同EL表达式的paramValues内置对象)
13         attr:相当于PageContext的findAttribute方法。${username}
14     小技巧:在页面中使用<s:debug/>查看上下文中的对象
15         <s:property value="[0].userName"/>    省略[0].就是取第一个Action实例动作的userName
16         <s:property value="#session.user"/>
17     EL表达式会先在四大域顺序找值,如果找不到去ValueStack中找值,struts2实现了这一内容
    ${requestScope.userName} el表达式在request中搜索,找不到去value Stack中寻找搜索
    <c:property value="#request.userName" />
request中搜索,找不到去value Stack中寻找搜索
 

 set标签下有 action page request scope application域

取出action域内的值 直接 #key名

 

in和not in是关键词,

1     <s:if test="'aaa' not in {'aa','bb'}">
2         不在
3     </s:if>

 ognl表达式的投影功能

?:获得所有符合逻辑的元素。

^获得符合逻辑的第一个元素

$获得符合逻辑的最后一个元素

例如<s:iterator value="books.{?#this.price>35}">

  <s:property value="title"/><s:property value="price"/><br/>

</s:iterator>

 

s:set标签

scope 表示的是作用范围. 里面可以取5个值.可以看官方文档给的解释

The scopes available are as follows :-

  • application - the value will be set in application scope according to servlet spec. using the name as its key
  • session - the value will be set in session scope according to servlet spec. using the name as key
  • request - the value will be set in request scope according to servlet spec. using the name as key
  • page - the value will be set in page scope according to servlet sepc. using the name as key
  • action - the value will be set in the request scope and Struts' action context using the name as key

我们可以通过<s:debug>标签来查看对应的值插入到了哪个位置...

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib uri="/struts-tags" prefix="s"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 5 <html>
 6   <head>
 7     <title>利用OGNL表达式创建集合</title>
 8     
 9     <meta http-equiv="pragma" content="no-cache">
10     <meta http-equiv="cache-control" content="no-cache">
11     <meta http-equiv="expires" content="0">    
12     <!--
13     <link rel="stylesheet" type="text/css" href="styles.css">
14     -->
15 
16   </head>
17   
18   <body>
19     <s:set var="list1" value='{"a","b","c"}'></s:set><br/><!-- 默认放到了ActionContext上下文件中,scope="action" -->
20     <s:set var="list2" value="{'aa','bb','cc'}" scope="session"/>
21     <s:iterator value="#session.list2" var="l">
22         <s:property value="#l"/><br/>
23     </s:iterator>
24     <hr/>
25     <s:property value="#list1[1]"/>
26     
27     <hr/>
28     <s:set var="map1" value="#{'a':'valuea','b':'valueb'}" scope="session"/>
29     <!-- 打印valueb -->
30     <s:iterator value="#session.map1" var="me">
31         <s:property value="#me.key"/>=<s:property value="#me.value"/><br/>
32     </s:iterator><!-- 作用如同forEach  -->
33     
34     <hr/>
35     <c:forEach items="${sessionScope.map1}" var="me">
36         ${me.key }=${me.value }<br/>
37     </c:forEach>
38     <hr/>
39     
40     <s:if test="'aaa' not in {'aa','bb'}">
41         不在
42     </s:if>
43     
44     <s:debug></s:debug>
45    
46   </body>
47 </html>
View Code

 

 

 

 

posted on 2014-06-04 17:03  wf110  阅读(223)  评论(0编辑  收藏  举报