Problems encountered
A List <Object> ..
Enumeration type in the list
The enumeration type cannot pass when the page is displayed. Attribute to retrieve data
Reflection
Match the corresponding class name based on the name to obtain the corresponding property value (reflection)
Jsp page
<% @ Taglib uri = "/WEB-INF/tld/MethodUtil. tld" prefix = "methodUtil" %>
<Td> <methodUtil: value className = "xxxUtil" path = "com. yq1012.utils" method = "convert" param = "enumeration type"/> </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>
Reflection help class
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 = "here is the class package name." + 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;
}
}
Reflection conversion entity class
Public class xxxUtil {
Public String convert (String value ){
If ("yq1012". equals (value )){
Return enumeration. getName (); // name of the package to be imported for enumeration
} Else {
Return null;
}
}
}