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:

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"       
    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">     
    <!-- 定义函数版本 -->  
    <tlib-version>1.0</tlib-version>  
    <!-- 定义函数名称 -->  
    <short-name>constant</short-name>
    <uri>/constant-tags</uri>
    <!-- 定义第一个函数 -->  
    <function>  
        <!-- 定义第一个函数:reverse -->  
        <name>get</name>
        <!-- 定义函数处理类 -->  
        <function-class>com.milo.base.common.constant.ConstantUtil</function-class>
        <!-- 定义函数的对应方法 -->  
        <function-signature>  
            java.lang.Object get(java.lang.String)
        </function-signature>
    </function>
</taglib>

 

使用:

jsp页面引入

<%@ taglib prefix="constant" uri="/constant-tags" %>

获取值

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

${constant:get("YES")}

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

<c:if test='${constant:get("PayType.WEIXIN") == item.payType}'>
    ...
</c:if>

 

转载于:https://my.oschina.net/u/3413592/blog/912017

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值