关于页面循环枚举的用法及原理

1.用法

1.1定义枚举ESex.java

package com.funi.core.gov.enumeration;
public enum ESex {
    //男
    MAN,
    //女
    WOMAN
}

1.2枚举属性文件中定义枚举描述 resource/config/env/enum.properties
com.funi.core.gov.enumeration.ESex.MAN=男
com.funi.core.gov.enumeration.ESex.WOMAN=女
1.3jsp页面循环使用方法

<select name="sex">
    <p:enumEach enum="com.funi.core.gov.enumeration.ESex" var="e">
        <option value="${e.name}">${e.description}</option>
    </p:enumEach>
</select>


1.enum="" 值为枚举全名

2.${e.name} 为枚举的值,如MAN,WOMAN
3.${e.description} 为枚举定义的描述值,如 男,女

2. 相关类及配置文件

2.1 EnumEachTag枚举循环tag类

package com.funi.property.gov.web.tag;

import com.funi.core.gov.support.EnumItem;
import com.funi.property.gov.web.support.EnumConfigure;
import org.apache.taglibs.standard.tag.common.core.ForEachSupport;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.jstl.core.LoopTag;
import javax.servlet.jsp.tagext.IterationTag;
import java.util.ArrayList;
import java.util.List;

/**
 * User: niuzhihuan
 * Date: 14-9-9
 * DESC: 枚举循环tag类
 */
public class EnumEachTag extends ForEachSupport implements LoopTag, IterationTag {
    // for tag attribute
    public void setBegin(int begin) throws JspTagException {
        this.beginSpecified = true;
        this.begin = begin;
        validateBegin();
    }

    // for tag attribute
    public void setEnd(int end) throws JspTagException {
        this.endSpecified = true;
        this.end = end;
        validateEnd();
    }

    // for tag attribute
    public void setStep(int step) throws JspTagException {
        this.stepSpecified = true;
        this.step = step;
        validateStep();
    }

    public void setEnum(String enumClassName) throws JspTagException {
        // for null items, simulate an empty list
        rawItems = new ArrayList();
        try {
            Class e = this.getClass().getClassLoader().loadClass(enumClassName);
            if (e.isEnum()) {
                List<EnumItem> enumItemList = new ArrayList<EnumItem>();
                Enum[] enums = (Enum[]) e.getEnumConstants();
                for (Enum en : enums) {
                    enumItemList.add(new EnumItem(en.name(), EnumConfigure.getDescription(en)));
                }
                rawItems = enumItemList;
            } else {
                rawItems = new ArrayList();
            }

        } catch (ClassNotFoundException c) {
            c.printStackTrace();
        }
    }
}


2.2 EnumConfigure读取properties文件,返回指定的枚举描述信息

package com.funi.property.gov.web.support;

import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ResourceBundleMessageSource;

import java.util.Locale;

/**
 * Created by niu on 2014/9/4.
 * 读取properties文件,返回指定的枚举描述信息
 */
public class EnumConfigure extends ResourceBundleMessageSource {
    private static MessageSourceAccessor messageSourceAccessor;

    private EnumConfigure() {
        setBasenames(new String[]{"config/env/enum"});
    }


    private static MessageSourceAccessor getAccessor() {
        if (messageSourceAccessor == null)
            messageSourceAccessor = new MessageSourceAccessor(new EnumConfigure(), Locale.getDefault());
        return messageSourceAccessor;
    }

    public static String getDescription(Enum e) {
        if (e == null) return null;
        try {
            return getAccessor().getMessage(e.getClass().getName().concat(".").concat(e.name()));
        } catch (NoSuchMessageException ex) {
            return null;
        }
    }
}


2.3 EnumItem Enum的name 和 description描述信息Dto

package com.funi.core.gov.support;

/**
 * User: niuzhihuan
 * Date: 14-9-9
 * Enum的name 和 description描述信息Dto
 */
public class EnumItem {
    /**
     * 枚举名称
     */
    private String name;
    /**
     * 枚举描述
     */
    private String description;

    public EnumItem(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {

        return description;
    }
}


2.4 resource/config/env/enum.properties

记录枚举值和对应的描述信息

com.funi.core.gov.enumeration.ESex.MAN=男
com.funi.core.gov.enumeration.ESex.WOMAN=女

2.5 WEB-INF/tlds/property.tld 配置tag信息

<tag>
        <name>enumEach</name>
        <tag-class>com.funi.property.gov.web.tag.EnumEachTag</tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <name>enum</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <name>begin</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <type>int</type>
        </attribute>
        <attribute>
            <name>end</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <type>int</type>
        </attribute>
        <attribute>
            <name>step</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <type>int</type>
        </attribute>
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>varStatus</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值