序:在之前这篇《Struts2数据传输的背后机制:ValueStack(值栈)(转载)》博文中指出ognl对于struts2的使用在于
- OGNL是Struts2中使用的一种表达式语言,它可以用于JSP的标签库中,以便能够方便的访问各种对象的属性;
- .它用于界面将参数传递到Action(并进行类型转换)中;
- 它还可以用于struts2的配置文件中!所以,非常有必要理解OGNL的基本机制。
因此本文的重点在于ognl标签是如何在jsp,struts2配置文件中进行使用的,ognl表达式是如何使用的?作为%,$,#三个表达式使用符号,我们分别来介绍这三种的应用
场景。
符号:#
有#和无#的使用,直接贴源码:
Action源码:
public class OgnlAction {
private String actionStr;
private OgnlBean ob;
private List ognlList;
private Map ognlMap;
public String execute() {
/*---------------------Action属性-------------------------*/
this.setActionStr("ac");
OgnlBean ob = new OgnlBean();
ob.setBeanStr("ob");
this.setOb(ob);
ognlList=(List) new ArrayList<Object>();
ognlList.add("1");
ognlList.add("2");
this.setOgnlList(ognlList);
ognlMap=new HashMap<String, Object>();
ognlMap.put("1", 1);
ognlMap.put("2", 2);
this.setOgnlMap(ognlMap);
/*---------------------ActionContext.getContext()属性-------------------------*/
HttpServletRequest hreq=ServletActionContext.getRequest();
HttpSession session=hreq.getSession();
ServletContext aplication=ServletActionContext.getServletContext();
hreq.setAttribute("actionStr", actionStr);
session.setAttribute("ob", ob);
aplication.setAttribute("ognlList", ognlList);
return "success";
}
public String getActionStr() {
return actionStr;
}
....get,set省略
}
JSP源码:
<body>
<table border="1">
<tr >
<td colspan="2" align="center">Action属性获取</td>
</tr>
<tr>
<td>普通属性</td>
<td><s:property value="actionStr"/></td>
</tr>
<tr>
<td>Bean属性</td>
<td><s:property value="ob.beanStr"/></td>
</tr>
<tr>
<td>List属性</td>
<td>
<s:property value="ognlList[0]"/> :
<s:property value="ognlList.size"/>
</td>
</tr>
<tr>
<td>Map属性</td>
<td>
<s:property value="ognlMap.keys.toArray()[1]"/>:
<s:property value="ognlMap.values.toArray()[1]"/>
</td>
</tr>
<tr>
<td>
Action方法(普通public)
</td>
<td>
<s:property value="getActionStr()"/>
</td>
</tr>
<tr>
<td colspan="2">其他诸如静态方法的调用,选择查询等不常用,有需要百度。</td>
</tr>
<tr>
<td colspan="2" align="center">ActionContext属性获取 #</td>
</tr>
<tr>
<td>request属性</td>
<td>
<s:property value="#request.actionStr"/>
</td>
</tr>
<tr>
<td>session属性</td>
<td>
<s:property value="#session.ob.beanStr"/>
</td>
</tr>
<tr>
<td>ServletContext属性</td>
<td>
<s:property value="#application.ognlList[0]"/>
</td>
</tr>
</table>
</body>
HTML结果如图所示:
ps:
1.值栈(ValueStack) :
可以在值栈中放入、删除、查询对象。访问值栈中的对象不用“#”。Struts2总是把当前Action实例放置在栈顶。所以在OGNL中引用Action中的属性也可以省略“#”。
2.调用ActionContext的put(key,value)放入的数据,需要使用#访问。
3.#符号的用途一般有三种。
— 访问非根对象属性,例如#session.msg表达式,由于Struts 2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀。实际上,#相当于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'}。
注:ps部分都摘自网络。
符号:$
— 在国际化资源文件中,引用OGNL表达式,例如国际化资源文件中的代码:reg.agerange=国际化资源信息:年龄必须在${min}同${max}之间。
— 在Struts 2框架的配置文件中引用OGNL表达式,例如:
符号:%
%符号的用途是在标志的属性为字符串类型时,计算OGNL表达式的值
分享到:
相关推荐
struts2标签使用例子 struts2标签使用例子 struts2标签使用例子struts2标签使用例子
有对struts2标签和OGNL的总结。
struts2中siterator 标签的使用详解 及 OGNL用法 struts2中siterator 标签的使用详解 及 OGNL用法
Struts2 OGNL---标签详解析 都有实例 适合初学者
java的Struts中的OGNL标签的使用,详细介绍,比较实用。
struts2的ognl
Struts2的标签库及OGNL表达式语言
struts2标签和OGNL表达式struts2标签和OGNL表达式
struts2中ognl表达式的用法,详细介绍了ognl表达式与struts2基本标签的结合。
2、支持类静态的方法调用和值访问,表达式的格式为@[类全名(包括包路)]@[方法名 | 值名],例如: @java.lang.String@format('foo %s', 'bar')或@tutorial.MyConstant@APP_NAME; 3、支持赋值操作和表达式串联,...
struts2介绍之使用连接标签 struts2入门 struts2进阶学习一 struts2进阶学习二 struts2进阶学习三 struts2进阶学习四 struts2进阶学习一 struts2进阶学习一 struts2进阶学习一 struts2进阶学习一
有关于Struts2的国际化消息处理说明文档
博文参考:http://blog.csdn.net/u011638419/article/details/42041991
struts2标签和OGNL表达式.pdf
第四章:struts2中的OGNL.ppt,详细讲解了struts2的标签使用
最近的项目开发中使用了struts2的框架,但是好多人都不清楚struts2的标签的具体的用法,要想完全掌握struts2标签的用法,那么你必须先精通了struts2中OGNL的用法。struts2中很多东西其实理解就行,但是惟独OGNL虽然...
NULL 博文链接:https://johnson2132.iteye.com/blog/720769
struts标签和ognl语法,比较全的标签说明
OGNL几个常见标签的使用及含义asdfasdfsfasdfasdfasdfsf
Struts 2权威指南——第1章 Struts 2概述.doc Struts 2权威指南——第2章 Struts 2下的HelloWorld.doc Struts 2权威指南——第3章 Struts 2基础.doc 01 为Struts 2.0做好准备 ...Struts2介绍之使用链接标签.doc