遇到的问题
一个List<Object>的列表。。
列表中有枚举类型
枚举类型在页面显示的 时候就不能通过。属性来取数据
用反射
根据名字去匹配对应的类名 从而获取对应的属性值(反射)
jsp页面
<%@taglib uri="/WEB-INF/tld/MethodUtil.tld" prefix="methodUtil"%>
<td> <methodUtil:value className="xxxUtil" path="com.yq1012.utils" method="convert" param="枚举类型"/> </td>
tld
<?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.4</jsp-version> <short-name>m</short-name> <uri>/tlds/MethodUtil.tld</uri> <description> This Tag Library makes user develope JSP with method Value component easily. </description> <!--**********************************************--> <!-- Tag --> <!--**********************************************--> <tag> <name>value</name> <tag-class>com.yq1012.MethodUtilTag</tag-class> <body-content>empty</body-content> <attribute> <name>className</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description> className </description> </attribute> <attribute> <name>method</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description> method </description> </attribute> <attribute> <name>paramType</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> paramType </description> </attribute> <attribute> <name>param</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> param </description> </attribute> <attribute> <name>path</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description> </description> </attribute> </tag> </taglib>
反射帮助类
import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport; @SuppressWarnings("unused") public class MethodUtilTag extends SimpleTagSupport { private String className; private String method; private String paramType; private String param; private String path; @Override @SuppressWarnings("unchecked") public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); Class[] parasClass = null; Object[] args = null; if(StringUtil.isNotEmpty(param)) { if(param.contains(",")) { String[] params = StringUtil.split(param, ",", false); parasClass = new Class[params.length]; args = new Object[params.length]; for (int i = 0; i < parasClass.length; i++) { parasClass[i] = String.class; args[i] = params[i]; } } } Class classType = String.class; String value = ""; if("int".equals(paramType) || "Integer".equals(paramType)) { classType = Integer.class; } else if("double".equals(paramType) || "Double".equals(paramType)) { classType = Double.class; } if(StringUtil.isNotEmpty(className) && StringUtil.isNotEmpty(method)) { try { String packagePath = this.className; if(StringUtil.isNotEmpty(path)) { packagePath = this.path + "." + packagePath; } else { packagePath = "这里是类的包名字." + packagePath; } Class clazz = Class.forName(packagePath); Object newInstance = clazz.newInstance(); Method m = null; Object object = null; if(parasClass != null) { m = clazz.getMethod(method, parasClass); object = m.invoke(newInstance, args); } else { m = clazz.getMethod(method, classType); object = m.invoke(newInstance, new Object[]{param}); } value = object.toString(); } catch (Exception e) { value = ""; e.printStackTrace(); } } out.print(value); } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getParam() { return param; } public void setParam(String param) { this.param = param; } public String getParamType() { return paramType; } public void setParamType(String paramType) { this.paramType = paramType; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } }
反射转换实体类
public class xxxUtil { public String convert(String value){ if ("yq1012".equals(value)) { return 枚举.getName();//枚举的要导入对应的包名字 } else { return null; } } }