javascript学习指南|java中反射,枚举实际应用例子

更新时间:2019-11-17    来源:php应用    手机版     字体:

【www.bbyears.com--php应用】


遇到的问题

一个List的列表。。

列表中有枚举类型

枚举类型在页面显示的 时候就不能通过。属性来取数据

用反射

根据名字去匹配对应的类名 从而获取对应的属性值(反射)

jsp页面

<%@taglib uri="/WEB-INF/tld/MethodUtil.tld" prefix="methodUtil"%>


tld


    PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
    "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

 1.0
 2.4
 m
 /tlds/MethodUtil.tld
 
  This Tag Library makes user develope JSP with method Value component easily.
 

 
   
   
   
 
  value
  com.yq1012.MethodUtilTag
  empty
 
  
   className
   true
   true
   
             className
           

  
  
  
   method
   true
   true
   
             method
           
  
   
 
       
            paramType
            false
            true
           
             paramType
           

       
          
       
            param
            false
            true
           
             param
           

         
       
            path
            false
            true
           
           

                
    
          

反射帮助类

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

本文来源:http://www.bbyears.com/jiaocheng/79715.html

热门标签

更多>>

本类排行