struts-核心内容-10-Ognl表达式语言
来源:互联网 发布:大数据的价值体现在 编辑:程序博客网 时间:2024/05/21 11:03
Struts框架默认支持ognl表达式语言
作用-页面取值
ognl必须配合struts标签用
OgnlContext类
原理代码演示一
User.java
package com.cx.ognl;/** * Created by cxspace on 16-7-13. */public class User { private String id; private String name; private Address address = new Address("江西省","南昌市"); public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
测试
@Test public void testOgnl() throws OgnlException { //创建一个ognl对象 OgnlContext context = new OgnlContext(); User user = new User(); user.setId("1"); user.setName("cx"); //设置根元素值 context.setRoot(user);
//存放一个非根元素 //context.put("user",user); //需要用#号拿值
//Object ognl = Ognl.parseExpression("#user.name");
// 底层解析,根元素,不需要加警号 Object ognl = Ognl.parseExpression("name"); //得到value Object value = Ognl.getValue(ognl,context,context.getRoot()); System.out.println(value); //context.put("cn","China"); //String value = (String) context.get("cn"); //System.out.println(value); }
原理代码演示二
Address.java
package com.cx.ognl;/** * Created by cxspace on 16-7-13. */public class Address { private String province; private String 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; } public Address(String province, String city) { this.province = province; this.city = city; }}
@Test public void testOngl2() throws OgnlException { OgnlContext context = new OgnlContext(); User user = new User(); user.setId("122"); user.setName("cxspace"); context.setRoot(user);
//非根元素
Object ongl = Ognl.parseExpression("address.province"); Object value = Ognl.getValue(ongl,context,context.getRoot()); System.out.println(value); }
调用静态方法
public void testOgnl3() throws Exception{ OgnlContext context = new OgnlContext(); //Ognl调用类的静态方法 Object ognl = Ognl.parseExpression("@Math@floor(10.9)"); //由于math类在开发中常用上面ognl表达式也可以写为@@floor Object value = Ognl.getValue(ognl,context,context.getRoot()); System.out.println(value); }
ValueStack类--值栈
是整个Struts数据存储的核心,或者叫中转站。
每次用户访问struts的action,都会创建一个action对象、值栈对象、ActionContext对象,然后把Action对象放入值栈中,最后把值栈对象放入request中,传入jsp页面。
开发者只要通过ActionContext对象就可以访问struts的其他的关键对象。
(ActionContext是给开发者用)
获取值栈对象的两种方式
package com.cx.ognl;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.util.ValueStack;import org.apache.struts2.ServletActionContext;import javax.servlet.http.HttpServletRequest;/** * Created by cxspace on 16-7-14. */public class OgnlDemo2 extends ActionSupport { @Override public String execute() throws Exception { //获取值栈对象,方式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 return SUCCESS; }}
ValueStack对象中存储的数据结构图
值栈对象中的值
<br/>1. 取根元素的值<br/>
<s:property value="user.id"/>
<s:property value="user.name"/>
<s:property value="user.address"/>
<s:property value="user.address.city"/>
<s:property value="user.address.province"/>
<br/>2. 取非根元素的值<br/>
<s:property value="#request.cn"/>
<s:property value="#session.Session_data"/>
<s:property value="#application.Application_data"/> <br/>
<!-- 自动找request/session/application,找到后立刻返回 -->
<s:property value="#request_data"/>
<s:property value="#attr.Session_data"/>
<s:property value="#attr.Application_data"/> <br/>
<!-- 获取请求的参数数据 -->
<s:property value="#parameters.userName"/>
<!-- struts的调试标签:可以观测值栈数据 -->
<s:debug></s:debug>
OGNL表达式的几个特殊符号
# 非根元素取值、动态构建map集合
$ 配置文件取值
% 提供一个ognl表达式运行环境
- struts-核心内容-10-Ognl表达式语言
- Struts中的OGNL表达式语言
- Struts 2中的表达式语言-OGNL
- Struts 2 OGNL 表达式语言教程
- OGNL表达式语言和struts标签
- Struts拦截器、国际化、Ognl表达式语言
- Struts Ognl表达式语言几个符号 % $ #
- Struts的OGNL表达式
- struts中OGNL表达式
- struts专题 OGNL 表达式
- Struts的OGNL表达式
- struts--ognl表达式
- struts中的ognl表达式
- struts第十四天--ognl表达式,对象图导航语言
- Struts标签与OGNL表达式语言实例演练
- Struts2 入门(笔记三):Ognl表达式语言与struts标签
- Struts 简单UI标签,ognl表达式语言几个符号
- Struts中的OGNL表达式语言1/Struts标签与OGNL表达式语言实例演练2/Struts2常用标签总结3
- struts-基础内容-6-数据的自动封装和类型转换
- struts-基础内容-7-文件上传
- struts-基础内容-8-文件下载
- struts-核心内容-9-拦截器
- javaweb-c3p0连接池
- struts-核心内容-10-Ognl表达式语言
- Android系列之Service组件的应用
- struts-10-主要标签
- struts-11-表单数据校验
- struts标签解决用户输入验证的解决方案
- Hibernate学习-1-ORM 概念
- Hibernate学习-2-环境搭建
- Hibernate学习-3-API详解以及执行流程
- container_of分析