el java常量_jsp页面使用el表达式获取静态常量值

主要使用的常量类:

package com.milo.base.common.constant;

public class Constants {

private Constants() {}

public static final Integer YES = 1;

public static final Integer NO = 0;

/**

* 支付超时时间 2小时

*/

public static final Long PAY_TIMEOUT = 2 * 60 * 60 * 1000L;

/**

* 支付最短失效时间间隔 5分钟

*/

public static final Long MIN_PAY_TIME_INTERVAL = 5 * 60 *1000L;

/**

* 支付类型

*/

public class PayType {

/**

* 支付宝

*/

public static final String ALIPAY = "alipay";

/**

* 微信

*/

public static final String WEIXIN = "weixin";

}

}

反射获取常量值工具类:

package com.milo.base.common.constant;

import org.apache.commons.lang3.StringUtils;

import org.apache.log4j.Logger;

public class ConstantUtil {

private static final Logger log = Logger.getLogger(ConstantUtil.class);

private ConstantUtil() {}

/**

* 获取常量值

* @param key

* @return

*/

public static Object get(String key) {

log.info(String.format("获取常量值【key:%s】", key));

if (StringUtils.isBlank(key)) {

throw new IllegalArgumentException("获取常量的key不能为空");

}

String classPath = null;

String fieldName = key;

if (key.contains(".")) {

classPath = StringUtils.substringBeforeLast(key, ".");

fieldName = StringUtils.substringAfterLast(key, ".");

}

Class clazz = null;

// 默认常量类

if (StringUtils.isEmpty(classPath)) {

clazz = Constants.class;

} else {

try {

clazz = Class.forName(Constants.class.getName() + "$" + classPath.replace(".", "$"));

} catch (ClassNotFoundException e) {

log.info(String.format("【%s】不是直接在Constants内部类取的常量", key));

}

}

// 带包名类,根文件夹下常量类暂不考虑

while (null == clazz && StringUtils.contains(classPath, '.')) {

try {

clazz = Class.forName(classPath);

} catch (ClassNotFoundException e) {

String className = StringUtils.substringBeforeLast(classPath, ".");

String innerClass = StringUtils.substringAfterLast(classPath, ".");

classPath = className + "$" + innerClass;

}

}

if (null == clazz) {

throw new IllegalArgumentException("找不到常量类");

}

try {

return clazz.getField(fieldName).get(null);

} catch (NoSuchFieldException e) {

throw new IllegalArgumentException("常量名不存在");

} catch (Exception e) {

throw new IllegalArgumentException("找不到对应常量");

}

}

public static void main(String[] args) {

System.out.println(get("PayType.WEIXIN"));

}

}

tld文件constant.tld:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"

version="2.0">

1.0

constant

/constant-tags

get

com.milo.base.common.constant.ConstantUtil

java.lang.Object get(java.lang.String)

使用:

jsp页面引入

获取值

${constant:get("PayType.WEIXIN")}

${constant:get("YES")}

${constant:get("com.milo.base.common.constant.Constants.PayType.WEIXIN")}

...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值