Struts2学习6—OGNL (1)
2013-11-05 16:18
302 查看
http://struts.apache.org/release/2.2.x/docs/ognl.html
OGNL is the Object Graph Navigation Language.
在ActionContext中,OGNL的根对象为值栈,运行的时候,action的实例将放到值栈里。
如果访问值栈内的对象,可直接引用。如果是非值栈内的对象,应该加#。
Actioncontext也可从静态方法中得到
如果表达式不支持动态内容,可以用以下方式
集合的处理
用list的代码:{e1,e2,e3}
用Map的代码:#{key1:value1,key2:value2}
可用in 和not in
子集运算可用
? 匹配所有满足条件的
^ 第一个
$ 最后一个
支持Lamda表达式,Lamda表达式用【】
Fibonacci: if n==0 return 0; elseif n==1 return 1; else return fib(n-2)+fib(n-1);
fib(0) = 0
fib(1) = 1
fib(11) = 89
OGNL在tag中的书写方式
1)tag的规则:大意思是,字符串解析成"%{ ... }" ,非字符串直接为表达式值,如果非字符串用了"%{ ... }" ,符号会忽略。
All String attribute types are parsed for the "%{ ... }" notation.
All non-String attribute types are not parsed, but evaluated directly as an expression
The exception to rule #2 is that if the non-String attribute uses the escape notion "%{}", the notation is ignored as redundant, and the content evaluated.
http://struts.apache.org/release/2.3.x/docs/tag-syntax.html
2)Value的值是对象
OGNL is the Object Graph Navigation Language.
在ActionContext中,OGNL的根对象为值栈,运行的时候,action的实例将放到值栈里。
| |--application | |--session context map---| |--value stack(root) | |--request | |--parameters | |--attr (searches page, request, session, then application scopes) |
如果访问值栈内的对象,可直接引用。如果是非值栈内的对象,应该加#。
<s:property value="postalCode"/>
<s:property value="#session.mySessionPropKey"/> or <s:property value="#session['mySessionPropKey']"/> or <s:property value="#request['myRequestPropKey']"/>
Actioncontext也可从静态方法中得到
ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);
如果表达式不支持动态内容,可以用以下方式
<c:set var="foo" value="bar" scope="request"/> <s:textfield name="username" label="%{#request.foo}" />
集合的处理
用list的代码:{e1,e2,e3}
<s:select label="label" name="name" list="{'name1','name2','name3'}" value="%{'name2'}" />
用Map的代码:#{key1:value1,key2:value2}
<s:select label="label" name="name" list="#{'foo':'foovalue', 'bar':'barvalue'}" />
可用in 和not in
<s:if test="'foo' in {'foo','bar'}"> muhahaha </s:if> <s:else> boo </s:else> <s:if test="'foo' not in {'foo','bar'}"> muhahaha </s:if> <s:else> boo </s:else>
子集运算可用
? 匹配所有满足条件的
^ 第一个
$ 最后一个
person.relatives.{? #this.gender == 'male'}
支持Lamda表达式,Lamda表达式用【】
Fibonacci: if n==0 return 0; elseif n==1 return 1; else return fib(n-2)+fib(n-1);
fib(0) = 0
fib(1) = 1
fib(11) = 89
<s:property value="#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)" />
OGNL在tag中的书写方式
1)tag的规则:大意思是,字符串解析成"%{ ... }" ,非字符串直接为表达式值,如果非字符串用了"%{ ... }" ,符号会忽略。
All String attribute types are parsed for the "%{ ... }" notation.
All non-String attribute types are not parsed, but evaluated directly as an expression
The exception to rule #2 is that if the non-String attribute uses the escape notion "%{}", the notation is ignored as redundant, and the content evaluated.
http://struts.apache.org/release/2.3.x/docs/tag-syntax.html
2)Value的值是对象
<
s:textfield
key
=
"state.label"
name
=
"state"
value
=
"ca"
/>错
<s:textfield key="state.label" name="state" value="%{'ca'}" />
<p>Username: ${user.username}</p> | EL表达式,非OGNL |
<s:textfield name="username"/> | Value Stack中的值 |
<s:url id="es" action="Hello"> <s:param name="request_locale"> es </s:param> </s:url> <s:a href="%{es}">Espanol</s:a> | |
<s:property value="#session.user.username" /> | Seesion中的值 |
<s:select label="FooBar" name="foo" list="#{'username':'trillian', 'username':'zaphod'}" /> | Map对象,非VauleStack中值 |
<s:iterator value="cart.items"> ... <s:textfield label="'Cart item No.' + #rowstatus.index + ' note'" name="'cart.items[' + #rowstatus.index + '].note'" value="note" /> </s:iterator> | <s:iterator value="cart.items"> ... <s:textfield label="Cart item No. %{#rowstatus.index} note" name="cart.items[%{#rowstatus.index}].note" value="%{note}" /> </s:iterator> 左右两边是相等的。右边更好 |
相关文章推荐
- Struts2 OGNL 学习
- ognl和struts2标签_个人学习日志
- Struts2深入学习:OGNL表达式原理
- Struts2深入学习----OGNL表达式原理
- Struts2基础学习(七)—值栈和OGNL
- Struts2 OGNL学习知识
- Struts2学习(七)【OGNL基本语法】
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2深入学习:OGNL表达式原理
- Struts2系统学习(16)OGNL表达式及基本原理分析
- Struts2学习总结 ---- OGNL 表达式语言
- Struts2学习笔记四 OGNL
- Struts2深入学习----OGNL表达式原理
- Struts2学习(八)【OGNL表达式与Struts框架结合之原理】
- 黑马程序员---struts2学习笔记之八(ognl原理及使用)
- Struts2中OGNL,valueStack,stackContext的学习
- Struts2深入学习----OGNL表达式原理(转)
- Struts2学习之3(文件的上传下载,OGNL表达式,常用标签,表单重复提交)
- [struts2] ognl, valueStack, stackContext 的学习
- Struts2学习记录-Value Stack(值栈)和OGNL表达式