java integer ognl_学习OGNL语法

python nltk自然处理nlp实践教程书

99元

包邮

(需用券)

去购买 >

489590043fa7c5f40763877ba54c1101.png

原文链接:http://www.yiidian.com/struts2/struts2-ognl.html

1 OGNL简介

OGNL 的全称是对象图导航语言( Object-Graph Navigation Language),它是一种功能强大的开源表达式语言,使用这种表达式语言,可以通过某种表达式语法,存储Java对象的任意属性,调用Java 对象的方法,同时能够自动实现必要的类型转换。如果把表达式看作是一种带有语义的字符串,那么 OGNL 无疑成为了这个语义字符串于Java对象之间的沟通桥梁。

2 OGNL数据结构

dbea43579dd36c673c4987439700e62c.png

OGNL表达式,操作的是一个叫OgnlContext的对象。该对象包含两个属性:

root:根对象,可以存入任何对象作为Root对象

context:上下文对象,这是一个Map结构,可以往该Map存入任意key-value键值对

以上OgnlContext对象的两部分属性,我们都可以通过OGNL表达式进行数据存取。

3 OGNL表达式语法

Ognl表达式基本规则:

取root对象的值,只需要直接通过根对象属性即可

取context对象的值,必须通过指定的上下文容器中的#key.属性去取

3.1 环境准备

首先导入ognl包到项目

7a76a53c3e097b8028236c3c20771d0b.png

设计一个User类,用于存储数据

package com.yiidian.ognl;

/**

* @author 一点教程(yiidian.com)

*/

public class User {

private String name;

private Integer 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;

}

public User() {

super();

}

public User(String name, Integer age) {

super();

this.name = name;

this.age = age;

}

}

3.2 基本取值

/**

* 基本取值

*/

@Test

// 取出root中的属性值

public void test1() throws Exception {

// 准备ONGLContext

// 准备Root

User rootUser = new User("tom", 18);

// 准备Context

Map context = new HashMap();

context.put("user1", new User("jack", 18));

context.put("user2", new User("rose", 22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

// ====使用OGNL表达式取出属性====

// 取出root中user对象的name属性

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);

// ---------------------------------------------------

// 取出context中键为user1对象的name属性

String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());

String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());

Integer age1 = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());

System.out.println(name1);

System.out.println(name2);

System.out.println(age1);

}

结果为:

2f08e2f3c7640dc0bdf60e4358120089.png

3.3 赋值

/**

* 赋值

*/

@Test

public void test2() throws Exception {

// 准备ONGLContext

// 准备Root

User rootUser = new User("tom", 18);

// 准备Context

Map context = new HashMap();

context.put("user1", new User("jack", 18));

context.put("user2", new User("rose", 22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

// 编写OGNL表达式

// 将root中的user对象的name属性赋值

Ognl.getValue("name='jerry'", oc, oc.getRoot());

String name = (String) Ognl.getValue("name", oc, oc.getRoot());

String name2 = (String) Ognl.getValue("#user1.name='张三',#user1.name",oc, oc.getRoot());

System.out.println(name);

System.out.println(name2);

}

结果为:

9414ba85335eec4e9c669b619bb48e96.png

3.4 调用方法

/**

* 调用方法

*/

@Test

public void test3() throws Exception {

// 准备ONGLContext

// 准备Root

User rootUser = new User("tom", 18);

// 准备Context

Map context = new HashMap();

context.put("user1", new User("jack", 18));

context.put("user2", new User("rose", 22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

// 编写OGNL表达式

// 调用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);

}

结果为:

0d5d49c5a1d9e4aea8c5d9ecec94b0d5.png

3.5 调用静态方法

1)设计一个OgnlUtil类,里面有个sayHello的静态方法

package com.yiidian.ognl;

/**

* @author 一点教程(yiidian.com)

*/

public class OgnlUtil {

public static String sayHello(String name){

return "你好,"+name;

}

}

使用Ognl表达式调用sayHello静态方法

/**

* 调用静态方法

*/

@Test

public void test4() throws Exception {

// 准备ONGLContext

// 准备Root

User rootUser = new User("tom", 18);

// 准备Context

Map context = new HashMap();

context.put("user1", new User("jack", 18));

context.put("user2", new User("rose", 22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

// 编写OGNL表达式

String name = (String) Ognl.getValue("@com.yiidian.ognl.OgnlUtil@sayHello('张三')", oc,oc.getRoot());

Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());

System.out.println(name);

System.out.println(pi);

}

eedc727d39f1ba87706cc0fe7d412617.png

3.6 创建对象(List、Map)

/**

* 创建对象(List、Map)

*/

@Test

public void test5() throws Exception {

// 准备ONGLContext

// 准备Root

User rootUser = new User("tom", 18);

// 准备Context

Map context = new HashMap();

context.put("user1", new User("jack", 18));

context.put("user2", new User("rose", 22));

OgnlContext oc = new OgnlContext();

oc.setRoot(rootUser);

oc.setValues(context);

// 编写OGNL表达式

// 创建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);

}

结果为:

af95eaec90cd457cc976e17aeaac2589.png

源码下载:https://pan.baidu.com/s/1lD59FmfQLsG1h1Yvx55WUg

欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。

如果您对我的系列教程感兴趣,也可以关注我的网站:yiidian.com

java 11官方入门(第8版)教材

79.84元

包邮

(需用券)

去购买 >

f0f3f55624fb396b1764d42d6df88864.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值