⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ognlutil.java

📁 管理公司合同
💻 JAVA
字号:
/*
 * Created on 2006-1-23 15:29:50
 *
 * By SinoBest
 * Copyright E3.org, 2005-2006, All rights reserved.
 */

package cn.com.juneng.system.common.util;

import java.util.Map;

import ognl.DefaultMemberAccess;
import ognl.Ognl;
import ognl.OgnlException;
import cn.com.juneng.system.common.exception.SystemRuleException;

/**
 * @author yehailong 解析ognl表达式工具类
 */

public class OgnlUtil {
	private Map context = null;

	private Object target = null;

	/**
	 * 默认构造函数,与当前class绑定
	 */
	public OgnlUtil() {
		context = Ognl.createDefaultContext(this);
		target = this;
	}

	/**
	 * 构造函数,绑定目标class
	 * 
	 * @param targetBean
	 *            绑定bean
	 */
	public OgnlUtil(String className) {
		try {
			target = Class.forName(className).newInstance();
			context = Ognl.createDefaultContext(target);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 构造函数,绑定目标bean
	 * 
	 * @param targetBean
	 *            绑定bean
	 */
	public OgnlUtil(Object bean) {
		try {
			target = bean;
			context = Ognl.createDefaultContext(target);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param ognlExpression
	 *            标准的OGNL表达式
	 * @return
	 * @throws EecmisRuleException
	 */
	public Object getValue(String expression) throws SystemRuleException {
		DefaultMemberAccess aMemberAccess = new DefaultMemberAccess(true);
		Ognl.setMemberAccess(context, aMemberAccess);
		try {
			return Ognl.getValue(expression, context, target);
		} catch (OgnlException e) {
			e.printStackTrace();
			throw new SystemRuleException("ognl表达式解析失败:" + e.getMessage());
		}
	}

	// TODO setValue

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -