ongl表达式的应用
来源:互联网 发布:淘宝商家信用卡费率 编辑:程序博客网 时间:2024/05/17 07:39
OGNL能够访问系统中OgnlContext中的对象, OgnlContext对象是OGNL表达式的下上文对象,即所有通过OGNL表达式取出的数据都是从上下文对象取出来的, OGNL的上下文环境是一个Map结构,称之为OgnlContext。
OgnlContext中包括以下几个内置对象
ValueStack:
存放在OgnlContext的第一位,是根对象,访问这里面的内容有点特殊,不用指定范围, Struts2框架总是把Action实例放在栈顶。因为Action在值栈中,而值栈又是OGNL中的根,所以引用Action的属性可以省略“#”标记,这也是为什么我们在结果页面中可以直接访问Action的属性的原因。
request:
模拟了HttpServletRequest对象,取这里面的值就相当于调用了request.getAttribute(“key”),访问这里面的属性形式: #request['user']或#request.user
session:
模拟了HttpServletSession对象,取这里面的值就相当于调用了session.getAttribute(“key”),
访问这里面的属性形式: #session['user']或#session.user
application:
模拟了HttpServletApplication对象,取这里面的值就相当于调用了servletContext.getAttribute(“key”),访问这里面的属性形式: #application['user']或#application.user
attr:
如果PageContext可用,则访问PageContext,否则依次搜索request、session和application对象。访问形式: #attr['user']或#attr.user
parameters:
用于访问请求参数,相当于调用了HttpServletRequest对象的getParameter()方法。也相当于JSTL中的 ${ param.id },访问形式: :#parameters['id']或#parameters.id
记住一点,不要和EL表达式混用,如果你用了EL表达式,就全用EL表达式,如果用了OGNL,就全用OGNL.
按道理来说,EL表达式只能访问request/session/page/application中的数据,但是我们现在又说了,Action中的属性值都是放在ValueStack中的,那为什么我们在先前的示例中,EL表达式能够访问ValueStack中的数据呢?
原因是:Struts2对request对象进行装饰,所用的类是org.apache.struts2.dispatcher.StrutsRequestWrapper
查看该类源码,发现它重写了getAttribute()方法:
通过查看源代码我们找到了原因:
会先前reuqest中查找属性值:
ActionContext ctx = ActionContext.getContext();
Object attribute = super.getAttribute(s);
如果没有找到,再从 ValueStack中查找
if (attribute == null) {
….
attribute = stack.findValue(s); //从ValueStack查找
….
}
这里就说明了为什么Struts2中EL表达式能够取出Action中的值了,但是一定要记住Action中的值是存储在ValueStack中的
首先我们来看看怎么用OGNL表达式取值
取ValueStack中的值
Action中:
public String method1(){
setMessage("取ValueStack中的值");
return "method1";
}
JSP页面:
<s:property value="message"/>
XML配置:
<action name="method1" class="chapter10.action.Chapter10Action" method="method1">
<result name="method1">/WEB-INF/JspPage/chapter10/success.jsp</result>
</action>
取request其它对象中的值
Action:
public String method1(){
request.setAttribute("message", "request中的值");
session.put("message", "session中的值");
request.getSession().getServletContext().setAttribute("message", "ServletContext中的值");
return "method1";
}
JSP页面:
<s:property value="#request.message"/> | <s:property value="#request['message']"/><br />
<s:property value="#session.message"/> | <s:property value="#session['message']"/><br />
<s:property value="#application.message"/> | <s:property value="#application['message']"/><br />
<s:property value="#attr.message"/>
可以看到attr对象是用来取值用的,它取出的值是顺着pagerequestsessionapplication
取parameters中的值
Action
public String method1() {
setMessage("parameters");
return "method1";
}
XML:
<action name="method1" class="chapter10.action.Chapter10Action" method="method1">
<result name="method1" type="redirect">/success.jsp?message=${message}</result>
</action>
JSP:
<s:property value="#parameters.message"/> | <s:property value="#parameters['message']"/><br />
注意type="redirect"时,JSP页面不能放在web-inf下
访问Action的中的方法
在Action中增加一个方法
public String test() {
return "Action Method";
}
JSP页面:
<s:property value="test()" /><br />
XML:
<action name="method1" class="chapter10.action.Chapter10Action" method="method1">
<result name="method1">/WEB-INF/JspPage/chapter10/success.jsp</result>
</action>
访问Action中属性的方法
增加一个User Model
public class User {
private String userName;
public User() {
super();
}
public User(String userName) {
super();
this.userName = userName;
}
@Override
public String toString() {
return "姓名:" + getUserName();
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
修改Action,增加一个构造:
public Chapter10Action() {
user = new User("wdpc");
}
public String method1() {
return "method1";
}
XML
<action name="method1" class="chapter10.action.Chapter10Action" method="method1">
<result name="method1">/WEB-INF/JspPage/chapter10/success.jsp</result>
</action>
JSP
<s:property value="user.toString()" /><br />
访问属性的内置方法
Action:
public String method1() {
setMessage("调用字符串的length方法");
return "method1";
}
JSP:
<s:property value="message" />的长度是:<s:property value="message.length()" />
OGNL表达式访问静态属性和方法
首先加入一个开关
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
修改Model,添加一个静态方法,一个静态常量
public static final String CLASSNAME= "wdpc0801";
public static String staticMethod() {
return "Static Method";
}
JSP:
访问静态方法:
<s:property value="@chapter10.model.User@staticMethod()" />
<br />
访问静态属性或常量:
<s:property value="@chapter10.model.User@CLASSNAME" />
<br />
访问静态方法(默认为Math类):@@floor(1.32342):
<s:pr
- ongl表达式的应用
- ONGL表达式的用法
- Struts2的ONGL表达式
- Struts2.1 中 ONGL 表达式的应用总结
- Struts2.1 中 ONGL 表达式的应用总结
- Struts2.1 中 ONGL 表达式的应用总结
- ONGL表达式
- ongl表达式
- ONGL表达式的用法(精)
- Ongl表达式使用
- Strut2之ongl表达式
- struts2标签和ongl表达式结合表示&&和||的写法
- ONGL表达式在jsp中获取各种数据的方法
- EL表达式 与ONGL表达式
- struts2-ONGL原理和表达式
- 【项目学习】ONGL表达式学习
- struts2-ONGL原理和表达式
- 处理ongl表达式传值
- linux下用vim写基本的c程序
- HDOJ 题目4334 Trouble(hash)
- android eclipse的xml字体大小
- 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-13-卷积层
- Android基础入门教程——8.3.5 Paint API之—— Xfermode与PorterDuff详解(二)
- ongl表达式的应用
- Spark源码—Job Runtime
- unzip解压中文名称文件
- 什么是指针?如何理解指针?指针又是如何定义与使用的?
- [BZOJ1040][ZJOI2008]骑士
- loadrunner脚本录制为空的解决方法
- RTEMS-BBB(九):UART扩展:编程
- iOS多线程1 Operation
- Handler任务模型之MessageQueue