struts2之OGNL
来源:互联网 发布:上海淘宝模特兼职招聘 编辑:程序博客网 时间:2024/06/05 10:07
OGNL是Object-Graph Navigation Language的缩写,它是一种功能强大的表达式语言(Expression Language,简称为EL),通过它简单一致的表达式语法,可以存取对象的任意属性,调用对象的方法,遍历整个对象的结构图,实现字段类型转化等功能。它使用相同的表达式去存取对象的属性。
下面讲述OGNL在struts2中的运用:
首先给出两个实体类:
(1)Emp.java
package com.study.struts2.yy;public class Emp {private int age = 8;public Emp(){}public Emp(int age){this.age = age;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "I am 8 years old";}public String sayHello(){return "hello";}}
(2)Department.java
package com.study.struts2.yy;public class Department {private Emp emp;public Department() {}public Emp getEmp() {return emp;}public void setEmp(Emp emp) {this.emp = emp;}@Overridepublic String toString() {return emp.getAge() + "";}}(3)配置文件
<struts> <constant name="struts.devMode" value="true" /><package name="ognl" extends="struts-default" namespace="/actions"><action name="ognl" class="com.study.struts2.yy.OgnlAction"><result name="success">/ognl.jsp</result></action></package></struts>
(4)OgnlAction.java
package com.study.struts2.yy;import com.opensymphony.xwork2.ActionSupport;public class OgnlAction extends ActionSupport {private String username;private String password;//添加对象Emp emp;//添加另一个变量Department department;public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department = department;}public Emp getEmp() {return emp;}public void setEmp(Emp emp) {this.emp = emp;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String execute() throws Exception {return SUCCESS;}}
(5)index.jsp
<a href="<%=basePath %>/actions/ognl?username=yy&password=pp"> ognl</a>
在ognl.index中做如下实验:
a、访问值栈中的action的普通属性:
<s:property value="username"/>b、访问值栈中对象的普通属性(get set方法)
<s:property value="emp.age"/> | <s:property value="emp['age']"/> | <s:property value="emp[\"age\"]"/>
注意:当前Emp类在Action中没有new,所以需要在地址栏传递emp.age=12等
c、访问值栈中对象的普通属性(get set方法)
<s:property value="department.emp.age"/>方法同b,需要传递department.emp.age的值
d、访问值栈中属性的普通方法
<s:property value="password.length()"/>e、访问值栈中对象的普通方法:
首先在Action中添加一个方法,如下:
public String test(){return "test";}进行访问:
<s:property value="test()" />f、访问静态方法
首先在Emp.java中添加一静态方法:
public static String getEmpAge(){return "8";}进行访问:
<s:property value="@com.study.struts2.yy.Emp.@getEmpAge()"/>但是,此时访问不到,需要在struts.xml中添加如下:
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>g、访问静态属性
首先在Emp.java中添加一静态属性:
public static String str ="aaa";
进行访问:
<s:property value="@com.study.struts2.yy.Emp@str"/>
0 0
- struts2之OGNL
- Struts2之Ognl用法
- 八、Struts2之OGNL
- struts2之ognl表达式
- Struts2之ognl
- Struts2之OGNL
- struts2.x之OGNL
- struts2之OGNL
- Struts2之Ognl详解
- struts2之OGNL表达式
- Struts2之OGNL
- struts2之ognl表达式
- struts2之OGNL表达式
- 传智播客--struts2.x之OGNL
- struts2总结之 OGNL表达式
- Struts2之OGNL使用小结
- 【struts2学习之OGNL详解】
- Struts2复习之OGNL(1)
- cocos2d-x 2.2.2 => Android
- 解决android模拟器 Failed to create Context 0x3005 Could not initialize OpenglES emulation...
- c中memcpy和strcpy区别
- UVA 138 - Street Numbers
- OpenGL: 3D 轮廓字体
- struts2之OGNL
- 九度OJ 1120 全排列
- 排序算法的比较
- ospf 序列号
- JavaScript权威指南 正则表达式的模式匹配 读书笔记
- sql注入绕开过滤的常用方法
- poj2709 Painter
- Android高手进阶教程(三)之----Android 中自定义View的应用.
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch