ongl表达式的应用

OGNL表达相当于JSP+Servlet模式中JSTL的地位,但是它的功能要比JSTL强大,在学习OGNL表达式时,你需要忘掉JSTL.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值