我是靠谱客的博主 舒心花生,最近开发中收集的这篇文章主要介绍OGNL表达式介绍代码示例:,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

介绍

OGNL:对象视图导航语言(Object Graphic Navigation Language)Struts框架使用OGNL作为默认的表达式语言

 ${user.addr.name} 这种写法就叫对象视图导航

OGNL不仅可以视图导航 支持比EL表达式更加丰富的功能

详情请看代码

代码示例:

bean类

package cn.bean;
public class User {
private String name;
private Integer age;
public User() {
super();
// TODO Auto-generated constructor stub
}
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 + "]";
}
}

demo 示例

package cn.ognl;
import cn.bean.User;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
public class Demo {
@Test
//准备工作+基本语法
public void fun1() throws OgnlException {
//准备OGNLContext
//准备root
User rootUser = new User("小明",18);
//准备Context
HashMap<String, User> context = new HashMap<>();
context.put("user1",new User("小小",18));
context.put("user2",new User("大大",22));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
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);
}
@Test
//取出context属性值
public void fun2() throws OgnlException {
//准备OGNLContext
//准备root
User rootUser = new User("小小",18);
//准备Context
HashMap<String, User> context = new HashMap<>();
context.put("user1",new User("大大",18));
context.put("user2",new User("哈哈",22));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
Integer age = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot());
Integer age2 = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
System.out.println(name);
System.out.println(name2);
System.out.println(age);
System.out.println(age2);
}
@Test
//为属性赋值
public void fun3() throws OgnlException {
//准备OGNLContext
//准备root
User rootUser = new User("嘻嘻",18);
//准备Context
HashMap<String, User> context = new HashMap<>();
context.put("user1",new User("哈哈",18));
context.put("user2",new User("hiahia",22));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
Ognl.getValue("name='Jerry'", oc, oc.getRoot());
String name = (String) Ognl.getValue("name", oc, oc.getRoot());
System.out.println(name);
String name2 = (String) Ognl.getValue("#user1.name='马云',#user1.name", oc, oc.getRoot());
System.out.println(name2);
}
@Test
//调用方法
public void fun4() throws OgnlException {
//准备OGNLContext
//准备root
User rootUser = new User("二蛋",18);
//准备Context
HashMap<String, User> context = new HashMap<>();
context.put("user1",new User("大蛋",18));
context.put("user2",new User("小蛋",22));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
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);
}
@Test
//调用静态方法
public void fun5() throws OgnlException {
//准备OGNLContext
//准备root
User rootUser = new User("马云",18);
//准备Context
HashMap<String, User> context = new HashMap<>();
context.put("user1",new User("java",18));
context.put("user2",new User("PHP",22));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser);
oc.setValues(context);
//书写OGNL
String name = (String) Ognl.getValue("@cn.ognl.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);
}
@Test
//基本语法演示
//ognl创建对象-list|map
public void fun6() throws OgnlException {
//准备OGNLContext
//准备root
User rootUser = new User("tom", 18);
//准备Context
HashMap<String, User> 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);
}
}

静态 类

public class HahaUtils {
//回应方法
public static Object echo(Object o){
return o;
}
}

 

最后

以上就是舒心花生为你收集整理的OGNL表达式介绍代码示例:的全部内容,希望文章能够帮你解决OGNL表达式介绍代码示例:所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部