Struts2之OGNL表示式與值棧
1、概述:OGNL的全稱是Object Graph Navigation Language(物件圖導航語言),它是一種強大的表示式語言,Struts框架使用OGNL作為預設的表示式語言。
2、OGNL 有一個上下文(Context)概念,它是一個map結構,因為它實現了java.utils.Map 的介面。OgnlContext(ognl上下文)=根物件(1個)+非根物件(n個),非根物件要通過"#key"訪問,根物件可以省略"#key"。可通過以下程式碼進行理解:
OnglExpression工具類:
/** * 用於OGNL表達計算的一個工具類 */ public class OnglExpression { private OnglExpression() { } /** * 根據OGNL表示式進行取值操作 * @param expression:ognl表示式 * @param ctx:ognl上下文 * @param rootObject:ognl根物件 * @return */ public static Object getValue(String expression, OgnlContext ctx, Object rootObject) { try { return Ognl.getValue(expression, ctx, rootObject); } catch (OgnlException e) { throw new RuntimeException(e); } } /** * 根據OGNL表示式進行賦值操作 * @param expression:ognl表示式 * @param ctx:ognl上下文 * @param rootObject:ognl根物件 * @param value:值物件 */ public static void setValue(String expression, OgnlContext ctx, Object rootObject, Object value) { try { Ognl.setValue(expression, ctx, rootObject, value); } catch (OgnlException e) { throw new RuntimeException(e); } } }
測試類:
public class Demo1 { /** * @param args * @throws OgnlException */ public static void main(String[] args) { Employee e = new Employee(); e.setName("小李"); Manager m = new Manager(); m.setName("張經理"); // 建立OGNL上下文,而OGNL上下文實際上就是一個Map物件 OgnlContext ctx = new OgnlContext(); // 將員工和經理放到OGNL上下文當中去 ctx.put("employee", e); ctx.put("manager", m); ctx.setRoot(e);// 設定OGNL上下文的根物件 /** ********************** 取值操作 *************************** */ // 表示式name將執行e.getName(),因為e物件是根物件(請注意根物件和非根物件表示式的區別) //通過ognl表示式在上下文中獲取根節點的name屬性值 String employeeName = (String) OnglExpression.getValue("name", ctx, e);//小李 System.out.println(employeeName); // 表示式#manager.name將執行m.getName(),注意:如果訪問的不是根物件那麼必須在前面加上一個名稱空間,例如:#manager.name //通過ognl表示式在上下文中獲取非根物件的屬性值(#非根物件在容器中的key.屬性) String managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);//張經理 System.out.println(managerName); // 當然根物件也可以使用#employee.name表示式進行訪問 employeeName = (String) OnglExpression.getValue("#employee.name", ctx, e);//小李 System.out.println(employeeName); /** ********************** 賦值操作 *************************** */ OnglExpression.setValue("name", ctx, e, "小明"); employeeName = (String) OnglExpression.getValue("name", ctx, e);//小明 System.out.println(employeeName); OnglExpression.setValue("#manager.name", ctx, e, "孫經理"); managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);//孫經理 System.out.println(managerName); OnglExpression.setValue("#employee.name", ctx, e, "小芳"); employeeName = (String) OnglExpression.getValue("name", ctx, e);//小芳 System.out.println(employeeName); } }
列印結果:
總結:1、一個上下文中只有一個根物件
2、取根物件的值,只需要直接通過根物件屬性即可
3、非根物件取值必須通過指定的上下文容器中的#key.屬性去取
3、ognl是一種思想,而Struts沿用了這種思想,所以Struts中也有上下文,即actionContext,actionContext一次請求只建立一次。ValueStack(值棧)是actionContext的根物件,值棧是先進後出的資料結構,放到值棧中的物件都可視為根物件。
public class OgnlAction {
public String execute() {
main1(null);
System.out.println("--------------");
main2(null);
return "success";
}
/**
* 值棧的使用
*/
public static void main1(String[] args) {
// 棧:表示一個先進後出的資料結構
ActionContext ac = ActionContext.getContext();
ValueStack vs = ac.getValueStack();
// push方法把項壓入棧頂
vs.push(new Employee("zs", 22));//Employee實體類的兩個屬性為name和salary
vs.push(new Employee("ls", 22));
vs.push(new Employee("ww", 22));
// pop方法移除棧頂物件並作為此函式的值返回該物件
Employee e = (Employee) vs.pop();
System.out.println(e.getName());//ww
e = (Employee) vs.pop();
System.out.println(e.getName());//ls
e = (Employee) vs.pop();
System.out.println(e.getName());//zs
}
/**
* 此例用於模擬struts2的值棧計算過程
* @param args
*/
public static void main2(String[] args) {
ActionContext ac = ActionContext.getContext();
ValueStack vs = ac.getValueStack();
vs.push(new Employee("張僱員", 2000));//Employee實體類的兩個屬性為name和salary
vs.push(new Student("小明同學", "s001"));//Student實體類的兩個屬性為name和uid
System.out.println(vs.findValue("name"));
System.out.println(vs.findValue("salary"));
}
}
列印結果:
值棧中值的順序圖如下:
總結:1、值棧取值從上往下,取到為止,如果已經拿到,不再往下找。
2、valuestack又相當於一個容器,容器裡面存放的物件,包含的所有屬性,都可以算valuestack的屬性
4、Struts2中給引數賦值:a、在Action中定義屬性,並提供get/set方法
b、實現ModelDriven介面,返回實體,不能為null,不需要提供get/set方法
當ModelDriven返回實體的屬性和Action中屬性重名時,ModelDriven中優先順序更高,有如下程式碼:
CaseAction類程式碼:
public class CaseAction implements ModelDriven<User> {
private User user = new User();//User實體類的屬性為uname和uid
private String uname;
@Override
public User getModel() {
return user;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String execute() {
System.out.println("uname:"+uname);
System.out.println(user);
return "success";
}
}
HTML程式碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>jsp與子控制器相互傳值的問題</h1>
<a href="${pageContext.request.contextPath }/sy/caseAction.action?uname=xx">測試</a>
</body>
</html>
列印結果:
結果顯示action裡定義的uname屬性為null,而實體類裡的uname屬性獲取到了傳過來的值,這是因為在向ValueStack壓棧時,是先壓action,再壓modeldriven的,而值棧取值時又是從上往下取的。
值棧中值的順序圖如下: