Struts2学习(五)
表达式
1、表达式语言 ( Expression Language )
2、表达式的本质: 就是 按照某种规则 书写的 字符串
3、表达式的处理: 一定有一套程序 对 表达式 中的 字符串进行解析和处理,比如 Tomcat
4、常见的表达式
- JSP 容器中支持 EL 表达式 ( 因此在 JSP 页面中可以直接使用 )
- Spring 中 可以使用 Spring Expression Language ( SpEL )
- Struts2 中可以使用 对象图导航语言Object Graphic Navigation Language(OGNL)
OGNL
1、定义
- OGNL 是一种 功能强大 的 表达式语言 ( Expression Language )
- OGNL 在 非 Web 环境下 ,需要用到 :javassist-3.20.0-GA.jar,ognl-3.1.12.jar
- OGNL 目前是 Apache 基金会 下的 commons 项目 下的 一个小项目: http://commons.apache.org
2、测试案例
- OgnlContext两个方法测试案例
package ecut.OGNL.entity; import java.io.Serializable; public class Customer implements Serializable { private static final long serialVersionUID = 7499870485797565944L; private String username ; private String password ; private String confirm ; 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; } public String getConfirm() { return confirm; } public void setConfirm(String confirm) { this.confirm = confirm; } }
package ecut.OGNL; import java.util.Map; import ecut.OGNL.entity.Customer; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; public class OgnlTest1 { public static void main(String[] args) throws OgnlException { /*OGNL是通常要结合Struts 2的标志一起使用,如<s:property value="#xx" /> struts页面中不能单独使用,el可以单独使用 ${sessionScope.username} */ OgnlContext context = new OgnlContext(); //OgnlContext 实现了Map接口 System.out.println( context instanceof Map ); // Ognl.setValue( expression , root, value ); Ognl.setValue( "customer" , context , new Customer() ); // Customer c = new Customer(); context.put( "customer" , c ) ; Ognl.setValue( "customer.username" , context , "张三丰" ); // c.setUsername( "张三丰" ); // JSP 表达式 : <%= c.getUsername() %> // EL 表达式: ${ customer.username } // OGNL 表达式 : customer.username(支持OGNL的环境,才可以使用) Object value = Ognl.getValue( "customer.username" , context ) ; // Ognl.getValue(expression, root) System.out.println( value ); } }
OGNL 在 非 Web 环境下需要添加两个jar包,OGNL是通常要结合Struts 2的标志一起使用,如<s:property value="#x" /> struts页面中不能单独使用,el可以单独使用 ${sessionScope.username}。OgnlContext实现了Map接口因此可以使用相应的方法获取属性和设置属性。
- 单元测试测试案例
package ecut.OGNL; import org.junit.After; import org.junit.Before; import org.junit.Test; import ecut.OGNL.entity.Customer; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; public class OgnlTest2 { private OgnlContext root; /* @Before 每次test都执行一次,@BeforeClass仅执行一次,方法得是静态的 */ public @Before void init() throws OgnlException { root = new OgnlContext(); Ognl.setValue("customer", root, new Customer()); Ognl.setValue("customer.username", root, "张三丰"); } public @Test void testGetValue() throws OgnlException { Object value = Ognl.getValue("customer.username", root); System.out.println(value); } public @Test void testSetValue() throws OgnlException { Object value = Ognl.getValue("customer.username", root); System.out.println(value); Ognl.setValue("customer.username", root, "张君宝"); value = Ognl.getValue("customer.username", root); System.out.println(value); } public @After void destory() { } }
首先要添加Junit 的library,然后新建Junit Test Case ,注解的位置可以在方法前也可以在修饰符后面。
- 获取对象属性测试案例
package ecut.OGNL.action; import ecut.OGNL.entity.Customer; public class CustomerAction { private Customer customer ; public String execute() throws Exception { System.out.println( "execute" ); return "execute" ; } public String login() throws Exception { System.out.println( "login" ); return "login" ; } public String logout() throws Exception { System.out.println( "logout" ); return "logout" ; } public Customer getCustomer() { System.out.println( "CustomerAction # getCustomer()" ); return customer; } public void setCustomer(Customer customer) { System.out.println( "CustomerAction # setCustomer( Customer )" ); this.customer = customer; } }
package ecut.OGNL; import org.junit.After; import org.junit.Before; import org.junit.Test; import ecut.OGNL.entity.Customer; import ecut.OGNL.action.CustomerAction; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; public class OgnlTest3 { private OgnlContext root ; public @Before void init() throws OgnlException { root = new OgnlContext(); Ognl.setValue( "customerAction" , root , new CustomerAction() ); } public @Test void testGetValue() throws OgnlException { Object value = Ognl.getValue( "customerAction.customer" , root ) ; System.out.println( value ); } public @Test void testSetValue() throws OgnlException { Object value = Ognl.getValue( "customerAction.customer" , root ) ; System.out.println( value ); Ognl.setValue( "customerAction.customer" , root , new Customer() ); Ognl.setValue( "customerAction.customer.username" , root , "张翠山" ); value = Ognl.getValue( "customerAction.customer.username" , root ) ; System.out.println( value ); } public @After void destory() { } }
- 获取方法测试案例
package ecut.OGNL; import org.junit.After; import org.junit.Before; import org.junit.Test; import ecut.OGNL.action.CustomerAction; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; public class OgnlTest4 { private OgnlContext root ; public @Before void init() throws OgnlException { root = new OgnlContext(); Ognl.setValue( "customerAction" , root , new CustomerAction() ); } public @Test void testGetValue() throws OgnlException { Object value = Ognl.getValue( "customerAction.login()" , root ) ; System.out.println( value ); } public @Test void testSetValue() throws OgnlException { } public @After void destory() { } }
转载请于明显处标明出处