您的位置:首页 > 其它

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

2014-09-09 14:02 309 查看

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: