java中反射,枚舉實際應用例子

來源:互聯網
上載者:User


遇到的問題

一個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;
  }
 }
 
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.