概述
l OGNL表達式
OGNL是Object Graphic Navigation Language(對象圖導航語言)的縮寫,它是一個開源項目。 Struts2框架使用OGNL作為默認的表達式語言。
l OGNL優勢
1、支持對象方法調用,如xxx.doSomeSpecial();
2、支持類靜態的方法調用和值訪問,表達式的格式:
@[類全名(包括包路徑)]@[方法名 | 值名],例如:
@java.lang.String@format('foo %s', 'bar')
或@tutorial.MyConstant@APP_NAME;
3、支持賦值操作和表達式串聯,如price=100, discount=0.8,
calculatePrice(),這個表達式會返回80;
4、訪問OGNL上下文(OGNL context)和ActionContext;
5、操作集合對象。
l 總結
OGNL 有一個上下文(Context)概念,說白了上下文就是一個MAP結構,它實現了 java.utils.Map 的接口。 OgnlContext對象
分析:
ü Struts框架默認就支持Ognl表達式語言。
(struts必須引用的包:ognl.jar)
ü 作用
頁面取值用。
El表達式語言,用於頁面取值,jsp頁面取值的標准。(默認直接可以使用)
(應用范圍更廣。)
Ognl表達式語言, struts標簽默認支持的表達式語言。
必須配置struts標簽用,不能離開struts標簽直接用。
package loaderman.b_ognl; public class Address { private String province; private String city; public Address() { super(); // TODO Auto-generated constructor stub } public Address(String province, String city) { super(); this.province = province; this.city = city; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
package loaderman.b_ognl; import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException; import org.junit.Test; // OgnlContext用法 public class OgnlDemo1 { /** * 1. Ognl表達式語言語言取值,取非根元素的值,必須用#號 * @throws Exception */ @Test public void testOgnl() throws Exception { // 創建一個Ognl上下文對象 OgnlContext context = new OgnlContext(); // 放入數據 User user = new User(); user.setId(100); user.setName("Jack"); // 【往非根元素放入數據, 取值的時候表達式要用"#"】 context.put("user", user); // 獲取數據(map) // 先構建一個Ognl表達式, 再解析表達式 Object ognl = Ognl.parseExpression("#user.name"); Object value = Ognl.getValue(ognl, context, context.getRoot()); System.out.println(value); } /** * 2. Ognl表達式語言語言取值,取根元素的值,不用帶#號 * @throws Exception */ @Test public void testOgn2() throws Exception { // 創建一個Ognl上下文對象 OgnlContext context = new OgnlContext(); // 放入數據 User user = new User(); user.setId(100); user.setName("Jack"); // 【往根元素放入數據】 context.setRoot(user); // 獲取數據(map) // 先構建一個Ognl表達式, 再解析表達式 Object ognl = Ognl.parseExpression("address.province"); Object value = Ognl.getValue(ognl, context, context.getRoot()); System.out.println(value); } /** * 3.Ognl對 靜態方法調用的支持 * @throws Exception */ @Test public void testOgn3() throws Exception { // 創建一個Ognl上下文對象 OgnlContext context = new OgnlContext(); // Ognl表單式語言,調用類的靜態方法 //Object ognl = Ognl.parseExpression("@Math@floor(10.9)"); // 由於Math類在開發中比較常用,所以也可以這樣寫 Object ognl = Ognl.parseExpression("@@floor(10.9)"); Object value = Ognl.getValue(ognl, context, context.getRoot()); System.out.println(value); } }
package loaderman.b_ognl; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.util.ValueStack; /** * struts的數據流轉 * */ public class OgnlDemo2 extends ActionSupport{ // 根元素值 private User user = new User(100,"Jacks"); public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { ActionContext ac = ActionContext.getContext(); // 映射數據 ac.getContextMap().put("request_data", "request_data"); // 數據存儲request // Map<String,Object> map = (Map<String, Object>) ac.get("request"); // map.put("request_data", "request_data"); // map.put("cn", "China"); ac.getSession().put("Session_data", "Session_data"); ac.getApplication().put("Application_data", "Application_data"); // 二、值棧對象的存儲數據的原理 ValueStack vs = ac.getValueStack(); /***************操作根元素的幾種方法*****************/ // 設置數據: 入棧 //vs.push(new User(1002,"Tom")); // 棧頂 //vs.pop(); // 移除棧頂元素 // 如何存儲? map結構存儲 //vs.set("user1", new User(1,"Jacky1")); //vs.set("user2", new User(2,"Jacky2")); return super.execute(); } // 一、獲取值棧對象的2種方式 private void getVs() { // 獲取值棧對象,方式1: HttpServletRequest request = ServletActionContext.getRequest(); ValueStack vs1 = (ValueStack) request.getAttribute("struts.valueStack"); // 獲取值棧對象,方式2: ActionContext ac = ActionContext.getContext(); ValueStack vs2 = ac.getValueStack(); System.out.println(vs1 == vs2);//true } }
package loaderman.b_ognl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** * struts的數據流轉 * */ public class OgnlDemo3 extends ActionSupport{ @Override public String execute() throws Exception { // 測試迭代標簽 List<User> list = new ArrayList<User>(); Map<Integer,User> map = new HashMap<Integer, User>(); // 初始化 for (int i=1; i<11; i++) { User user = new User(i,"Jack" + i); list.add(user); map.put(user.getId(), user); } // 保存 ActionContext.getContext().getContextMap().put("list", list); ActionContext.getContext().getContextMap().put("map", map); return super.execute(); } }
package loaderman.b_ognl; public class User { private int id; private String name; private Address address = new Address("廣東省","廣州"); public User() { super(); } public User(int id, String name) { super(); this.id = id; this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="ogn_" extends="struts-default"> <action name="ognl" class="loaderman.b_ognl.OgnlDemo3"> <result name="success">/ognl.jsp</result> </action> </package> </struts>