[TOC]
# 簡介
OGNL:對象視圖導航語言. `${user.addr.name}` 這種寫法就叫對象視圖導航.
OGNL不僅僅可以視圖導航.支持比EL表達式更加豐富的功能.
它是一種功能強大的表達式語言,通過它簡單一致的表達式語法,可以存取對象的任意屬性,調用對象的方法,遍歷整個對象的結構圖,實現字段類型轉化等功能。它使用相同的表達式去存取對象的屬性
Strtsu2框架內置了OGNL
OGNL本身也是一個項目,它是可以單獨使用。
OGNL作用:
支持對象的操作,調用對象的方法
支持靜態成員訪問
支持賦值操作與表達串聯
訪問OGNL上下文,訪問ActionContext
操作集合對象
搭建環境:單獨使用OGNL來完成示例

OGNL三要素: 表達式 OgnlContext 上下文 Root 根

# 作用

# 基本演示
準備測試類,User類
~~~
package bean;
public class User {
private String name;
private Integer age;
public User() {
super();
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
~~~
## 取出root中的屬性值
~~~
@Test
public void fun1() throws OgnlException {
// 準備ONGLContext
// 準備Root
User rootUser = new User("tom", 18);
// 準備Context
Map<String, User> context = new HashMap<String, User>();
context.put("user1", new User("jack", 18));
context.put("user2", new User("rose", 22));
// 它就是個java.util.map
OgnlContext oc = new OgnlContext();
// 將rootUser作為root部分
oc.setRoot(rootUser);
// 將context這個Map作為Context部分
oc.setValues(context);
// 書寫OGNL
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
System.out.println(name);
System.out.println(age);
}
~~~
## 為屬性賦值
~~~
// 準備ONGLContext
// 準備Root
User rootUser = new User("tom", 18);
// 準備Context
Map<String, User> context = new HashMap<String, User>();
context.put("user1", new User("jack", 18));
context.put("user2", new User("rose", 22));
// 它就是個java.util.map
OgnlContext oc = new OgnlContext();
// 將rootUser作為root部分
oc.setRoot(rootUser);
// 將context這個Map作為Context部分
oc.setValues(context);
// 將root中的user對象的name屬性賦值
Ognl.getValue("name='jerry'", oc, oc.getRoot());
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
String name1 = (String) Ognl.getValue("#user1.name='jdxia',#user1.name", oc, oc.getRoot());
System.out.println(name);
System.out.println(name1);
~~~
## 調用方法
~~~
// 準備ONGLContext
// 準備Root
User rootUser = new User("tom", 18);
// 準備Context
Map<String, User> context = new HashMap<String, User>();
context.put("user1", new User("jack", 18));
context.put("user2", new User("rose", 22));
// 它就是個java.util.map
OgnlContext oc = new OgnlContext();
// 將rootUser作為root部分
oc.setRoot(rootUser);
// 將context這個Map作為Context部分
oc.setValues(context);
// 調用root中user對象的setName方法
Ognl.getValue("setName('lilei')", oc, oc.getRoot());
String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());
System.out.println(name);
System.out.println(name2);
~~~

## 調用靜態方法
準備測試類HahaUtils
~~~
package bean;
public class HahaUtils {
// 回音方法
public static Object echo(Object o) {
return o;
}
}
~~~
~~~
// 準備ONGLContext
// 準備Root
User rootUser = new User("tom", 18);
// 準備Context
Map<String, User> context = new HashMap<String, User>();
context.put("user1", new User("jack", 18));
context.put("user2", new User("rose", 22));
// 它就是個java.util.map
OgnlContext oc = new OgnlContext();
// 將rootUser作為root部分
oc.setRoot(rootUser);
// 將context這個Map作為Context部分
oc.setValues(context);
String name = (String) Ognl.getValue("@bean.HahaUtils@echo('hello')", oc, oc.getRoot());
//Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
//內置
Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
System.out.println(name);
System.out.println(pi);
~~~
## 創建對象(List,Map)
~~~
// 準備ONGLContext
// 準備Root
User rootUser = new User("tom", 18);
// 準備Context
Map<String, User> context = new HashMap<String, User>();
context.put("user1", new User("jack", 18));
context.put("user2", new User("rose", 22));
// 它就是個java.util.map
OgnlContext oc = new OgnlContext();
// 將rootUser作為root部分
oc.setRoot(rootUser);
// 將context這個Map作為Context部分
oc.setValues(context);
// 創建list對象
Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
String name = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());
System.out.println(size);
System.out.println(name);
System.out.println(name2);
// 創建map對象
Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
String name3 = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
System.out.println(size2);
System.out.println(name3);
System.out.println(age);
~~~

## 訪問Ognl上下文

# struts2和Ognl結合
## 結合原理






## Strtus2框架中如何使用ognl表達式
在struts2框架中我們使用ognl表達式的作用是從valueStack中獲取數據。
我們在struts2框架中可以使用ognl+valueStack達到在頁面(jsp)上來獲取相關的數據。
要想在jsp頁面上使用ognl表達式,就需要結合struts2框架的標簽
`<s:property value=”表達式”>`來使用

默認情況下,棧中放置當前訪問的Action對象



- 基礎
- 編譯和安裝
- scanner類(鍵盤錄入)
- Random類(隨機數)
- 數組
- 方法
- 類
- ArrayList集合
- char與int
- eclipse
- IDEA
- 變量與常量
- 常用API
- String,StringBuffer,StringBuilder
- 正則,Date,DateFormat,Calendar
- 包裝類,System,Math,Arrays,BigInteger,BigDecimal
- 集合,迭代器,增強for,泛型
- List,set,判斷集合唯一
- map,Entry,HashMap,Collections
- 異常
- IO
- File
- 遞歸
- 字節流
- 字符流
- IO流分類
- 轉換流
- 緩沖流
- 流的操作規律
- properties
- 序列化流與反序列化流
- 打印流
- commons-IO
- IO流總結
- 多線程
- 線程池
- 線程安全
- 線程同步
- 死鎖
- lock接口
- ThreadLoad
- 等待喚醒機制
- 線程狀態
- jdbc
- DBUtils
- 連接池DBCP
- c3p0連接池
- 網絡編程
- 多線程socket上傳圖片
- 反射
- xml
- 設計模式
- 裝飾器模式
- web service
- tomcat
- Servlet
- response
- request
- session和cookie
- JSP
- EL
- JSTL
- 事務
- 監聽器Listener
- 過濾器Filter
- json
- linux安裝軟件
- 反射詳解
- 類加載器和注解
- 動態代理
- jedis
- Hibernate
- 簡介
- 創建映射文件
- Hibernate核心配置文件
- 事務和增刪改查
- HibernateUtils
- 持久化對象的三種狀態
- 檢索方式
- query
- Criteria
- SQLQuery
- 持久化類
- 主鍵生成策略
- 緩存
- 事務管理
- 關系映射
- 注解
- 優化
- struts2
- 搭建
- 配置詳解
- Action
- 結果跳轉方式
- 訪問ServletAPI方式
- 如何獲得參數
- OGNL表達式
- valueStack 值棧
- Interceptor攔截器
- spring
- 導包
- IOC和DI
- Bean獲取與實例化
- Bean屬性注入
- spring注解
- 注解分層
- junit整合
- aop
- 動態代理實現
- cglib代理實現
- aop名詞
- spring的aop
- aop-xml詳解
- aop-注解詳解
- 代理方式選擇
- jdbcTemplate
- spring事務管理
- 回滾注意
- 事務傳播屬性
- MyBatis
- MyBatis簡介
- 入門程序
- 與jdbc hibernate不同
- 原始Dao開發
- Mapper動態代理方式
- SqlMapConfig.xml配置文件
- 輸入參數pojo包裝類
- resultMap
- 動態sql
- 一對一關聯
- 一對多
- 整合spring
- 逆向工程
- maven
- maven簡介
- 倉庫
- maven目錄結構
- maven常用命令
- 生命周期
- eclipse中maven插件
- 入門程序
- 整合struct
- 依賴范圍
- 添加插件
- idea配置
- jar包沖突
- 分模塊開發
- 構建可執行的jar包(包含依賴jar包)
- springMVC
- 處理流程
- java面試
- java版本升級
- java1-8版本變更
- java9新特性
- 鎖
- java資料
- idea
- jdk版本切換
- log4j
- 入門實例
- 基本使用方法
- Web中使用Log4j
- spring中使用log4j
- java代碼優化