1、OGNL
(Object Graph Navigation Language),简称OGNL,是应用于Java中的一个开源的表达式语言,它被集成在Struts2等框架中,作用是对数据进行访问,它拥有类型转换、访问对象方法、操作集合对象等功能。
Struts2框架使用OGNL作为默认的表达式语言
OGNL是一种比EL强大很多倍的语言
xwork提供 OGNL表达式
ognl-3.0.5.jar
示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28// 访问对象的方法 @Test public void test1() throws OgnlException{ OgnlContext context = new OgnlContext(); // 获取对象的方法 Object obj = Ognl.getValue("'helloworld'.length()", context, context.getRoot()); System.out.println(obj); } // 获取OGNL上下文件的对象 @Test public void test2() throws OgnlException{ OgnlContext context = new OgnlContext(); context.put("name", "Tom"); // 获取对象的方法 Object obj = Ognl.getValue("#name", context, context.getRoot()); System.out.println(obj); } // 从root栈获取值 @Test public void test3() throws OgnlException{ OgnlContext context = new OgnlContext(); Customer c = new Customer(); c.setCustName("haha"); context.setRoot(c); String name = (String) Ognl.getValue("custName", context, context.getRoot()); System.out.println(name); }
在Struts2框架中使用OGNL表达式,主要是在JSP页面中获取值栈中的值。
需要先引入Struts2的标签库
1
2<%@ taglib prefix="s" uri="/struts-tags" %>
使用Struts2提供的标签库中的标签
1
2<s:property value="OGNL表达式"/>
2、什么是值栈
- 值栈就相当于Struts2框架的数据的中转站,向值栈存入一些数据。从值栈中获取到数据。
- ValueStack 是 struts2 提供一个接口,实现类 OgnlValueStack ---- 值栈对象 (OGNL是从值栈中获取数据的 )
在action里面把数据放到值栈里面,在页面中获取到值栈数据
servlet和action区别
(1)Servlet:默认在第一次访问时候创建,创建一次,单实例对象
(2)Action:访问时候创建,每次访问action时候,都会创建action对象,创建多次,多实例对象
值栈存储位置
(1)每次访问action时候,都会创建action对象,
(2)在每个action对象里面都会有一个值栈对象(只有一个)
3、获取值栈对象
1) 使用ActionContext类里面的方法得到值栈对象
1
2
3
4
5
6
7
8
9
10
11
12public String execute() throws Exception { //获取context ActionContext context = ActionContext.getContext(); //用context调用getValueStack()方法得到context对象 ValueStack valueStack1 = context.getValueStack(); ValueStack valueStack2 = context.getValueStack(); //判断两个对象是否是同一个 System.out.println(valueStack1==valueStack2); return NONE; } }
控制台输出true,证明是同一个对象
2)、通过request获取值栈对象
4、值栈内部结构
root – Struts把动作和相关对象压入 ObjectStack 中–List
context – Struts把各种各样的映射关系(一些 Map 类型的对象) 压入 ContextMap 中
struts2里面标签 s:debug,使用这个标签可以查看值栈结构和存储值
访问action,执行action的方法有返回值,配置返回值到jsp页面中,在jsp页面中使用这个标签
浏览器访问action,点击超链接看到如下结构:
5、向值栈放数据
1)获取值栈对象调用值栈对象里面的set方法
1
2
3
4
5
6
7public String execute() throws Exception { ActionContext context = ActionContext.getContext(); ValueStack stack = context.getValueStack(); stack.set("userName", "Tom"); return SUCCESS; }
通过debug观察值栈上方添加了一个HashMap,就是刚才添加的数据
2)获取值栈对象,调用值栈的push方法
1
2
3
4
5
6
7
8public String execute() throws Exception { ActionContext context = ActionContext.getContext(); ValueStack stack = context.getValueStack(); stack.set("userName", "Tom"); stack.push("abcdefg"); return SUCCESS; }
能看见刚才添加的字符串
3)在action定义变量,生成变量的get方法
1
2
3
4
5private String str ="Hello World"; public String getStr() { return str; }
在action添加属性的这种方法,向值栈中添加数据不会生成一个内存对象放在栈顶,而是直接放在action对象中
4)、向值栈放对象
声明变量
在execute()方法中设置对象的属性值
1
2
3
4
5
6
7
8
9
10
11
12
13
14//定义变量 private User user = new User(); //user的get()方法 public User getUser() { return user; } @Override public String execute() throws Exception { user.setName("Tom"); user.setAdress("China"); user.setPassword("1323243"); return SUCCESS; }
5)向值栈放list集合
1
2
3
4
5
6
7
8
9
10
11
12
13private List<User> list = new ArrayList(); public List<User> getList() { return list; } @Override public String execute() throws Exception { User user1 = new User("王某", "USA", "1213"); User user2 = new User("李某", "US", "134433"); list.add(user1); list.add(user2); return SUCCESS; }
6、从值栈中获取数据
1) 演示,获取字符串
使用struts2的标签+ognl表达式获取值栈数据
1
2<s:property value=”ognl表达式”/>
在action中声明字符串变量,并定义get方法,在execute()方法内设置字符串的值
1
2
3
4
5
6
7
8
9
10private String userName; public String getUserName() { return userName; } @Override public String execute() throws Exception { userName = "jack"; return SUCCESS; }
jsp页面代码如下:
浏览器访问action,获得了userName的内容:
2)获取对象
action类代码
1
2
3
4
5
6
7
8
9
10private User user; public User getUser() { return user; } @Override public String execute() throws Exception { user = new User("Tom", "China", "121323"); return SUCCESS; }
jsp页面代码
1
2
3
4<s:property value="user.name"/> <s:property value="user.password"/> <s:property value="user.adress"/>
浏览器访问action显示结果:
3)获取list集合
①、第一种方式:
浏览器:
如果集合内数据过多,或者不清楚集合内有几个元素,这种方法,就不那么好用
2、第二种方式:<S:iterator>标签
1
2
3
4
5
6
7
8
9
10<%-- <s:iterator/>标签遍历list集合 --%> <s:iterator value="list"> <!-- 遍历list集合得到每个user对象 --> <s:property value="name"/> <s:property value="password"/> <s:property value="adress"/> <br> <br> </s:iterator>
注意:html注释无法注释标签,注释标签,使用jsp注释,否则会报错很难找到原因,例:
1
2
3
4
5不正确: <!-- <s:iterator/>标签遍历集合 --> 正确: <%-- <s:iterator/>标签遍历list集合 --%>
3、第三种方式
1
2
3
4
5
6
7
8
9
10
11
12
13<%-- <s:iterator/>标签遍历list集合 --%> <s:iterator value="list" var="user"> <!-- 遍历值栈list集合,得到每个对象 机制:把每次遍历出来的user对象放到context里面 获取context里面数据的特点:写ognl表达式 使用特殊符号# --> <s:property value="#user.name"/> <s:property value="#user.password"/> <s:property value="#user.adress"/> <br> </s:iterator>
最后
以上就是敏感胡萝卜最近收集整理的关于Struts2---------3(OGNL表达式语言和struts值栈)的全部内容,更多相关Struts2---------3(OGNL表达式语言和struts值栈)内容请搜索靠谱客的其他文章。
发表评论 取消回复