枚举类型数据定义和使用
1,web.xml定义一个系统的过滤器
<listener>
<listener-class>com.zainta.pregnancy.web.listener.ServletContextLoaderListener</listener-class>
</listener>
2,写这个filter的内容
package com.zainta.pregnancy.web.listener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ServletContextLoaderListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
// 通过WebApplicationContextUtils 得到Spring容器的实例。
ApplicationContext application = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
// 返回Bean的实例。
SystemServletContextLoader systemServletContextLoader = (SystemServletContextLoader) application
.getBean("systemServletContextLoader");
//初始化所有的枚举类型的实体
systemServletContextLoader.closeServletContext(servletContext);
}
public void contextInitialized(ServletContextEvent event) {
ServletContext servletContext = event.getServletContext();
// 通过WebApplicationContextUtils 得到Spring容器的实例。
ApplicationContext application = WebApplicationContextUtils
.getWebApplicationContext(servletContext);
// 返回Bean的实例。
SystemServletContextLoader systemServletContextLoader = (SystemServletContextLoader) application
.getBean("systemServletContextLoader");
//初始化
systemServletContextLoader.initServletContext(servletContext);
}
}
3,spring配置一个systemServletContextLoader的bean
<bean id="systemServletContextLoader" class="com.zainta.pregnancy.web.listener.SystemServletContextLoader">
<property name="enumEntityPackage" value="${enumration.entity.package}"></property>
</bean>
${enumration.entity.package}这个是系统定义枚举类型所放置的包,比方叫com.xxxx.entity.enumeration
4,这个bean所得对应的内容如下
/**
*
*/
package com.zainta.pregnancy.web.listener;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.util.ResolverUtil;
/**
* @author Downpour
*
*/
@Component("systemServletContextLoader")
public class SystemServletContextLoader {
private static final Log logger = LogFactory
.getLog(SystemServletContextLoader.class);
private String enumEntityPackage;
/*
* (non-Javadoc)
*
* @see
* com.demo2do.core.web.loader.ServletContextLoader#initServletContext(javax
* .servlet.ServletContext)
*/
public void initServletContext(ServletContext servletContext) {
if (logger.isDebugEnabled()) {
logger.debug("Initial ServletContext for system");
}
// alias enum entity to servletContext
aliasEnumEntity(servletContext);
servletContext.setAttribute("ctx", servletContext.getContextPath());
}
/*
* (non-Javadoc)
*
* @see
* com.demo2do.core.web.loader.ServletContextLoader#closeServletContext(
* javax.servlet.ServletContext)
*/
public void closeServletContext(ServletContext servletContext) {
if (logger.isDebugEnabled()) {
logger.debug("Destroying ServletContext for system");
}
servletContext.removeAttribute("enums");
servletContext.removeAttribute("ctx");
}
/**
* Alias Enum entity under entity package
*
* @param servletContext
*/
protected void aliasEnumEntity(ServletContext servletContext) {
// first find out all the enum classes according to the entity packages
ResolverUtil<Object> resolver = new ResolverUtil<Object>();
resolver.find(new ResolverUtil.Test() {
@SuppressWarnings({ "rawtypes" })
public boolean matches(Class type) {
return Enum.class.isAssignableFrom(type);
}
@Override
public boolean matches(URL resource) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean doesMatchClass() {
return true;
}
@Override
public boolean doesMatchResource() {
// TODO Auto-generated method stub
return false;
}
}, enumEntityPackage);
Map<String, Object> enums = new HashMap<String, Object>();
for (Class<?> clazz : resolver.getClasses()) {
String key = clazz.getName().substring(
clazz.getName().lastIndexOf(".") + 1);
try {
enums.put(key, clazz.getMethod("values").invoke(clazz));
} catch (Exception e) {
e.printStackTrace();
}
}
servletContext.setAttribute("enums", enums);
}
public String getEnumEntityPackage() {
return enumEntityPackage;
}
public void setEnumEntityPackage(String enumEntityPackage) {
this.enumEntityPackage = enumEntityPackage;
}
}
这个类通过配置的枚举类型所放的包,通过反射的方式拿到枚举类型 5,枚举类型的类如下
package com.zainta.pregnancy.entity.enumeration;
/**
*@author : rannie.chen@zainta.com
*@version: 2012-10-24上午10:16:27
*@desc:
*/
public enum FakeStatus {
//0,非冒名
NOTFAKED("enum.fake_status.notfaked"),
//1,疑似
UNSURE("enum.fake_status.unsure"),
//2,冒名
FAKED("enum.fake_status.faked");
FakeStatus(String key) {
this.key = key;
}
String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public int getOrdinal() {
return this.ordinal();
}
}
message_zh_CN.properties配置文件如下
#enum
enum.fake_status.notfaked=非冒名
enum.fake_status.unsure=疑似
enum.fake_status.faked=冒名
6,jsp页面如使用方式 a,<%@ taglib prefix="fmt" uri= "http://java.sun.com/jsp/jstl/fmt" %> <fmt:setBundle basename ="message" />这是配置枚举类型显示的文件 message_zh_CN.properties,这个要在spring配置文件预先加载 b,使用如下
<select name = "">
<option value = "">--请选择--</option>
<c:forEach items="${enums['fakestatus']}" var = "fakeStatus">
<option value = "${fakeStatus}">
<fmt:message key = "${fakeStatus.key}"></fmt:message>
</option>
</c:forEach>
</select>