Velocity uses enum classes on pages

1.java tool class
package com.***.util;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.google.common.collect.Maps;

/**
* Provides a method for the page to directly call the enumeration class
* @author: gegewuqin9
* @since: December 8, 2016 at 6:31:00 pm
* @history:
*/
public class EnumHelper {

    /**The scanned classes are cached*/
    private static final Map<String, Class<?>> ENUM_CACHE        = Maps.newConcurrentMap();

    /** Scan the enumeration classes under this package and expose them to the page. The specific exposed enumeration class needs to add the name in the following subpackage */
    private static final String                BASE_PACKAGE_PATH = "com.***.enums";

    /**Scan enumeration classes under specific subpackages under the basic package specified above*/
    private static final String[]              BIZ_PACKAGE_NAMES = { "common", "company", "configure", "order"                               };

    /**
     * Function description: Get all the items of the enumeration class directly on the page
     *
     * @param enumName
     * @return
     * Object
     */
    public static Object getList(String enumName) {
        Class<?> clazz = getEnum(enumName);
        if (clazz == null) {
            return null;
        }
        try {
            Method method = clazz.getMethod("values");
            if (method != null) {
                return method.invoke(clazz);
            }
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * Function description: Get the value of the enumeration directly according to the code on the page
     *
     * @param enumName
     * @param code
     * @return
     * Object
     */
    public static Object getName(String enumName, Integer code) {
        Class<?> clazz = getEnum(enumName);
        if (clazz == null) {
            return null;
        }
        try {
            Method method = clazz.getMethod("getName", Integer.class);
            if (method != null) {
                return method.invoke(clazz, code);
            }
        } catch (Exception e) {
        }
        return null;
    }
    
    /**
     * Function description: Get the value of the enumeration directly according to the code on the page
     *
     * @param enumName
     * @param code
     * @return
     * Object
     */
    public static Object getName(String enumName, String code) {
        Class<?> clazz = getEnum(enumName);
        if (clazz == null) {
            return null;
        }
        try {
            Method method = clazz.getMethod("getName", String.class);
            if (method != null) {
                return method.invoke(clazz, code);
            }
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * Function description: Get the specific enumeration class Class according to the enumeration class name
     *
     * @param enumName
     * @return
     * Class<?>
     */
    private static Class<?> getEnum(String enumName) {
        if (StringUtils.isEmpty(enumName)) {
            return null;
        }
        if (ENUM_CACHE.containsKey(enumName)) {
            return ENUM_CACHE.get(enumName);
        }
        for (String packageName : BIZ_PACKAGE_NAMES) {
            String classPath = BASE_PACKAGE_PATH + "." + packageName + "." + enumName;
            try {
                Class<?> clazz = Class.forName(classPath);
                if (clazz != null) {
                    ENUM_CACHE.put(enumName, clazz);
                    return clazz;
                }
            } catch (Exception e) {
            }
        }
        return null;
    }
}


Note:
1. Our project is also divided into packages according to the specific business under the enums package. If the enumeration class is directly in the emuns directory, you can just define BASE_PACKAGE_PATH without the for loop.
2. The getName method needs to be defined as a static method in the enumeration class.

2. Configure toolbox.xml
<toolbox>
	<tool>
		<key>enumHelper</key>
		<scope>application</scope>
	   	<class>com.***.util.EnumHelper</class>
	</tool>
</toolbox>


3. The page uses the
loop : #foreach($!type in $!enumHelper.getList("OrderTypeEnum"))
Get the value according to the code: $!enumHelper.getName("OrderTypeEnum", "1")

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327046854&siteId=291194637