一:Struts OGNL基础
2016-11-01 11:35
225 查看
1.OGNL是一种强大的表达式语言(EL),可以通过简单的表达式来访问Java对象中的属性。
2.OGNL上下文对象很多,但会选择一个作为根对象------值栈(Value Stack)
Struts 中的OGNL Context 是 Action Context 。在开发过程中action包里的数据储存在值栈中。
见图:
![](https://img-blog.csdn.net/20161101102407003?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
3.对象访问:
a.根对象:对于根对象的访问是从栈顶向下搜索。访问格式:action1.name,action2.name 或者 [0].name,[1].name 。仅仅只有 name 访问的是action1中的name
![](https://img-blog.csdn.net/20161101102814674?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
b.parameters 对象:用来访问 HTTP 请求的参数的Map,例如#parameters.name 或者 #parameters["name"] ,相当于调用 request.getParameter("name")。
c.request 对象:用来访问 HttpServletRequest 属性的Map,例如 #request.userName 或者 request["userName"] ,相当于调用request.getAttribute("userName")。
d.session对象:用来访问 httpSession,例如 #session.userName 或者 #session["userName"] ,相当于调用session.getAttribute("userName")。
e.application对象:用于访问servletContext 例如 #application.userName 或者 #application["userName"] ,相当于调用servlet 的 getAttribute("userName")。
f.attr对象:包含前面四个作用域的所有属性。(也就是说前面四个对象的属性都可以用attr访问)
4.OGNL 的集合操作
a.创建一个List对象:{} ,如{e1,e2,e3}。
b.创建一个Map对象:#{} ,如#{key:value1,key2:value2}。
c.判断某个元素是否在指定的集合对象中:in,not in
<s:if test=" ' foo' in {'foo','bar'} ">
......
</s:if>
或
<s:if test=" ' foo' not in {'foo','bar'} ">
......
</s:if>
d.使用某个规则获得集合对象的子集:
?:获得所有符合逻辑的元素。
^ :获得符合逻辑的第一个元素。
$:获得符合逻辑的最后一个元素。
例如:
#man.{?# this.salary > 2000} // 返回所有工资大于2000的人的列表
#man.{^# this.salary > 2000} // 返回第一个工资大于2000的人的列表
#man.{$# this.salary > 2000} // 返回最后一个工资大于2000的人的列表
5.OGNL中三个重要的符号
a.#符号
— 访问非根对象属性。#相当于ActionContext. getContext();#session.msg表达式相当于ActionContext.getContext().getSession(). getAttribute("msg") 。
— 用于过滤和投影(projecting)集合,如persons.{?#this.age>25},persons.{?#this.name=='pla1'}.{age}[0]。
— 用来构造Map,例如示例中的#{'foo1':'bar1', 'foo2':'bar2'}。
b.%符号
— 将字符串强制转换为ognl表达式:<s:url value="%{#foobar['foo1']}" />
— 将属性强制转换为字符串:<s:property value="%{username}"/>
c.$符号
— 获得变量的值,国际化中使用。
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age 必须在${min} 到 ${max} 之间</message>
</field-validator>
</field>
— 在Struts 2配置文件中,引用OGNL表达式。
<action name="AddPhoto" class="addPhoto">
<interceptor-ref name="fileUploadStack" />
<result type="redirect">ListPhotos.action?albumId=${albumId}</result>
</action>
2.OGNL上下文对象很多,但会选择一个作为根对象------值栈(Value Stack)
Struts 中的OGNL Context 是 Action Context 。在开发过程中action包里的数据储存在值栈中。
见图:
3.对象访问:
a.根对象:对于根对象的访问是从栈顶向下搜索。访问格式:action1.name,action2.name 或者 [0].name,[1].name 。仅仅只有 name 访问的是action1中的name
b.parameters 对象:用来访问 HTTP 请求的参数的Map,例如#parameters.name 或者 #parameters["name"] ,相当于调用 request.getParameter("name")。
c.request 对象:用来访问 HttpServletRequest 属性的Map,例如 #request.userName 或者 request["userName"] ,相当于调用request.getAttribute("userName")。
d.session对象:用来访问 httpSession,例如 #session.userName 或者 #session["userName"] ,相当于调用session.getAttribute("userName")。
e.application对象:用于访问servletContext 例如 #application.userName 或者 #application["userName"] ,相当于调用servlet 的 getAttribute("userName")。
f.attr对象:包含前面四个作用域的所有属性。(也就是说前面四个对象的属性都可以用attr访问)
4.OGNL 的集合操作
a.创建一个List对象:{} ,如{e1,e2,e3}。
b.创建一个Map对象:#{} ,如#{key:value1,key2:value2}。
c.判断某个元素是否在指定的集合对象中:in,not in
<s:if test=" ' foo' in {'foo','bar'} ">
......
</s:if>
或
<s:if test=" ' foo' not in {'foo','bar'} ">
......
</s:if>
d.使用某个规则获得集合对象的子集:
?:获得所有符合逻辑的元素。
^ :获得符合逻辑的第一个元素。
$:获得符合逻辑的最后一个元素。
例如:
#man.{?# this.salary > 2000} // 返回所有工资大于2000的人的列表
#man.{^# this.salary > 2000} // 返回第一个工资大于2000的人的列表
#man.{$# this.salary > 2000} // 返回最后一个工资大于2000的人的列表
5.OGNL中三个重要的符号
a.#符号
— 访问非根对象属性。#相当于ActionContext. getContext();#session.msg表达式相当于ActionContext.getContext().getSession(). getAttribute("msg") 。
— 用于过滤和投影(projecting)集合,如persons.{?#this.age>25},persons.{?#this.name=='pla1'}.{age}[0]。
— 用来构造Map,例如示例中的#{'foo1':'bar1', 'foo2':'bar2'}。
b.%符号
— 将字符串强制转换为ognl表达式:<s:url value="%{#foobar['foo1']}" />
— 将属性强制转换为字符串:<s:property value="%{username}"/>
c.$符号
— 获得变量的值,国际化中使用。
<field name="age">
<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age 必须在${min} 到 ${max} 之间</message>
</field-validator>
</field>
— 在Struts 2配置文件中,引用OGNL表达式。
<action name="AddPhoto" class="addPhoto">
<interceptor-ref name="fileUploadStack" />
<result type="redirect">ListPhotos.action?albumId=${albumId}</result>
</action>
相关文章推荐
- struts2值栈&struts ognl表达式---【小白系列】0基础到熟练应用struts2框架(四)
- struts-hibernate-spring基础配制
- Struts基础篇之WEB学习(一)
- Struts常见错误及原因分析-Java基础-Java-编程开发
- Struts 2中的OGNL
- 一、Struts 2 基础---2.web.xml的配置
- 在struts2.1.2中不能使用ognl访问静态方法的原因
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的表达式语言-OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- 从struts 1.x升级到 struts 2(三) OGNL
- Struts Spring Hibernate快速入门-Java基础-Java-编程开发
- OGNL,简单的struts 2专用表达式,你表达了吗?(10)
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL
- Struts 2中的OGNL(Struts 2系列之十一)
- 一、Struts 2 基础---1.概述