struts2:OGNL表达式基本语法

1. 需要用到的含有静态方法的类

package com.staticmethod;

public class StaticMethod {

	public static String selectName(String name){
		return name;
	}
	
}

2. 测试类 

package com.ognl;

import java.util.HashMap;
import java.util.Map;
import com.entity.Users;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

//展示OGNL语法
public class OgnlDemo {

	public static void main(String[] args) throws OgnlException {
		//fun1();
		//fun2();
		//fun3();
		//fun4();
		//fun5();
		fun6();
	}

	//基本语法演示
	public static void fun1() throws OgnlException {
		//准备Root
		Users users = new Users("jerry",18);
		//准备Context
		Map<String, Users> context = new HashMap<String, Users>();
		context.put("users1", new Users("tom",20));
		context.put("users2", new Users("lucy",22));
		OgnlContext oc = new OgnlContext();
		//将rootUser作为root部分
		oc.setRoot(users);
		//将context这个Map作为Context部分
		oc.setValues(context);
		//书写OGNL
		Ognl.getValue("", oc, oc.getRoot());
	}
	
	//取出属性值
	public static void fun2() throws OgnlException {
		Users users = new Users("jerry",18);
		Map<String, Users> context = new HashMap<String, Users>();
		context.put("users1", new Users("tom",20));
		context.put("users2", new Users("lucy",22));
		OgnlContext oc = new OgnlContext();
		oc.setRoot(users);
		oc.setValues(context);
		//取出root中的属性值
		String name1 = (String) Ognl.getValue("name", oc, oc.getRoot());
		Integer age1 = (Integer) Ognl.getValue("age", oc, oc.getRoot());
		System.out.println(name1+":"+age1);
		//取出context中的属性值
		String name2 = (String) Ognl.getValue("#users1.name", oc, oc.getRoot());
		Integer age2 = (Integer) Ognl.getValue("#users1.age", oc, oc.getRoot());
		System.out.println(name2+":"+age2);
	}
	
	//为属性值赋值
	public static void fun3() throws OgnlException {
		Users users = new Users("jerry",18);
		Map<String, Users> context = new HashMap<String, Users>();
		context.put("users1", new Users("tom",20));
		context.put("users2", new Users("lucy",22));
		OgnlContext oc = new OgnlContext();
		oc.setRoot(users);
		oc.setValues(context);
		//给root中的users对象的name属性赋值
		Ognl.getValue("name='jerry2'", oc, oc.getRoot());
		String name1 = (String) Ognl.getValue("name", oc, oc.getRoot());
		System.out.println(name1);
		//给context中的users对象的name属性赋值
		String name2 = (String) Ognl.getValue("#users1.name='tom2',#users1.name", oc, oc.getRoot());
		System.out.println(name2);
	}
	
	//调用方法
	public static void fun4() throws OgnlException {
		Users users = new Users("jerry",18);
		Map<String, Users> context = new HashMap<String, Users>();
		context.put("users1", new Users("tom",20));
		context.put("users2", new Users("lucy",22));
		OgnlContext oc = new OgnlContext();
		oc.setRoot(users);
		oc.setValues(context);
		//调用root中user对象的setName方法
		Ognl.getValue("setName('jerry2')", oc, oc.getRoot());
		String name1 = (String) Ognl.getValue("getName()", oc, oc.getRoot());
		System.out.println(name1);
		//调用context中user对象的setName方法
		String name2 = (String) Ognl.getValue("#users1.setName('tom2'),#users1.getName()", oc, oc.getRoot());
		System.out.println(name2);
	}
	
	//调用静态方法
	public static void fun5() throws OgnlException {
		Users users = new Users("jerry",18);
		Map<String, Users> context = new HashMap<String, Users>();
		context.put("users1", new Users("tom",20));
		context.put("users2", new Users("lucy",22));
		OgnlContext oc = new OgnlContext();
		oc.setRoot(users);
		oc.setValues(context);
		//调用其他包下的静态方法(@类名全路径@方法名(参数))
		String name1 = (String) Ognl.getValue("@com.staticmethod.StaticMethod@selectName('name123')", oc, oc.getRoot());
		System.out.println(name1);
		//调用Math包下的常量PI
		//Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());
		Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
		System.out.println(pi);
	}
	
	//ognl创建对象-list|map
	public static void fun6() throws OgnlException {
		Users users = new Users("jerry",18);
		Map<String, Users> context = new HashMap<String, Users>();
		context.put("users1", new Users("tom",20));
		context.put("users2", new Users("lucy",22));
		OgnlContext oc = new OgnlContext();
		oc.setRoot(users);
		oc.setValues(context);
		//创建list对象
		Integer size = (Integer)Ognl.getValue("{'张三','李四','王五','赵六'}.size()", oc, oc.getRoot());
		String name1 = (String)Ognl.getValue("{'张三','李四','王五','赵六'}[1]", oc, oc.getRoot());
		String name2 = (String)Ognl.getValue("{'张三','李四','王五','赵六'}.get(2)", oc, oc.getRoot());
		System.out.println(size);
		System.out.println(name1);
		System.out.println(name2);
		System.out.println("---------------------------------");
		//创建map对象
		Integer size1 = (Integer)Ognl.getValue("#{'name':'张三','age':22}.size()", oc, oc.getRoot());
		String name3 = (String)Ognl.getValue("#{'name':'张三','age':22}['name']", oc, oc.getRoot());
		Integer age = (Integer)Ognl.getValue("#{'name':'张三','age':22}.get('age')", oc, oc.getRoot());
		System.out.println(size);
		System.out.println(name3);
		System.out.println(age);
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值