关于页面循环枚举的用法及原理
来源:互联网 发布:软件怎么加注册码 编辑:程序博客网 时间:2024/06/11 00:13
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,WOMAN3.${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
- 关于页面循环枚举的用法及原理
- 关于枚举的一些用法
- 关于枚举的一种用法
- Enum枚举的原理及各种使用方法
- 关于枚举的用法和类型转换
- Java枚举的定义及用法
- 枚举类及用法
- finder的用法及原理
- 黑马学习日记-2关于循环分支方法数组的一些概念及用法
- 关于增强for循环的用法
- 关于SQL 的with as 循环用法
- Python 循环及函数range的用法
- 页面回到顶部的实现及原理
- html页面渲染的原理及优化
- java中关于枚举enum类型的用法
- Java中关于枚举的7种用法
- 关于group by的用法 原理
- 枚举的原理
- Windows2008 iis ASP.net 4.0 配置
- iOS view的圆角和阴影并存
- 判断平台是小端序还是大端序
- invalid conversion from `const char*' to `char*'
- Android中几种图像特效处理
- 关于页面循环枚举的用法及原理
- Java 并发编程之性能和可伸缩性
- 显示窗口的水平和垂直滚动条
- BZOJ 1008 - HNOI 2008 越狱 乘法原理 快速幂
- 注册表操作(VC_Win32)
- Android ListView完全解析
- VB6 FORM窗体上控件实现滚动的方法
- 栈的实例[仅为自己验证某些理论,请绕道,hoho]
- HTML学习笔记(2)---文字标签