关于页面循环枚举的用法及原理
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>
相关文章推荐
- 关于不同页面获取文本值应用[opener 用法]
- 关于刷新页面和用法.(转)
- Java中关于枚举的7种用法
- ring0 关于SSDTHook使用的绕过页面写保护的原理与实现
- ionic2中 NavController导航组件用法方法关于页面的导航
- 关于页面的重定向的几个用法
- 关于react-redux中的connect用法介绍及原理解析
- 关于group by的用法 原理
- 关于cookie的部分用法(页面中新消息声音提醒开关)
- 关于Java泛型实现原理的思考与一般用法示例总结
- oracle rownum用法关于分页,只要看懂了原理,你就明白了,也算是自己学习过程的一个思考
- 关于枚举的一种用法
- 关于for循环与异常 continue、break的用法
- json原理和jquey循环遍历获取所有页面元素
- 枚举原理与常见用法与技巧
- 关于枚举的用法和类型转换
- 关于枚举的用法和类型转换
- 关于用户权限页面的实现原理
- Spring3.1.0实现原理分析(五).关于循环引用的探讨
- 关于shell中的for循环用法