我们首先定义一个用来取得静态变量的Map,重写了Get方法
/**
- 功能描述:
- 相关配置文件:
- @author nofeng */ package com.cnhuike.util;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
public class ConstantUtil extends HashMap implements Map{
public ConstantUtil(){
}
public Object getValue(String constantPath)throws Exception{
String classPath=StringUtils.substringBeforeLast(constantPath, ".");
String fieldName=StringUtils.substringAfterLast(constantPath, ".");
Field field=Class.forName(classPath).getField(fieldName);
return field.get( null );
}
@Override
public Object get( Object key ) {
try {
return getValue( key.toString() );
}
catch (Exception e) {
throw new IllegalArgumentException( " 找不到相关的变量: " + e.getMessage()
);
}
}
}
如何在jsp中调用:
<jsp:useBean id="constantUtil" class="com.cnhuike.util.ConstantUtil"></jsp:useBean>
${constantUtil['com.cnhuike.util.constant.UserConstant.ISADMIN']}