工具類

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.validator.constraints.Length;

import com.hgs.framework.annotation.CustomEditDisplayField;
import com.hgs.framework.annotation.CustomInputTypeField;
import com.hgs.framework.annotation.CustomListDisplayField;
import com.hgs.framework.component.CodeBean;

public class MultlUtil {

	/**
	 * 列表與維護頁面,獲取要顯示的字段
	 */
	public List multlFieldDisplay(Class clazz, boolean isList) {
		List fieldslist = new ArrayList();
		 // 獲取該類所有的字段
        Field[] fields = clazz.getDeclaredFields();
        // 遍歷賦值
        for(Field f : fields){
            String filedName = f.getName();
            CodeBean cb = new CodeBean();
            if(isList){
            	CustomListDisplayField cd = f.getAnnotation(CustomListDisplayField.class);
    			if(cd!=null && cd.isDisplay()){
    				cb.setName(filedName);
    				cb.setType(getType(f));
    				fieldslist.add(cb);
    			}
            }else{
            	CustomEditDisplayField cd = f.getAnnotation(CustomEditDisplayField.class);
    			if(cd==null || cd.isDisplay()){
    				cb.setName(filedName);
    				cb.setType(getType(f));
    				cb.setLen(getLength(f));
    				fieldslist.add(cb);
    			}
            }
        }
        return fieldslist;
	}

	public String getType(Field field){
		
		String result = "input";
		CustomInputTypeField ctf = field.getAnnotation(CustomInputTypeField.class);
		if(ctf!=null && "textarea".equals(ctf.inputType())){
			result = "textarea";
		}
		return result;
	}
	
	public int getLength(Field field){
		Length ctf = field.getAnnotation(Length.class);
		int result = 0;
		if(ctf!=null && ctf.max()!=0){
			result = ctf.max();
		}
		return result;
	}
	
	
	public Object getObj(String fn, Integer fv, String dataModel, HttpServletRequest request){
		Class clazz;
		Object obj = null;
		try {
			clazz = Class.forName(dataModel);
			obj = clazz.newInstance();
			
			Field[] fields = clazz.getDeclaredFields();
			String method = getMethod(fn);
			Method m = getMethod(clazz, method);
			m.invoke(obj, fv);
			
	        for(Field f : fields){
	        	Type ftype = f.getGenericType();
	            String filedName = f.getName();
	            if(isCZ(request, filedName)){
	            	String vlu = request.getParameter(filedName);
		            method = getMethod(filedName);
		            m = getMethod(clazz, method);
		            
		            if(vlu!=null && vlu.length()>0){
		            	if (ftype.toString().equals("class java.lang.String")){
		            		m.invoke(obj, vlu);
		            	}else if (ftype.toString().equals("class java.lang.Integer")) {
		            		m.invoke(obj, Integer.parseInt(vlu));
		            	}
		            }
		            //System.out.println(filedName+"="+vlu);
	            }
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		
        return obj;
	}
	
	public boolean isCZ(HttpServletRequest req, String name){
		boolean is = false;
		Map<String,String> ms = req.getParameterMap();
		for(String key : ms.keySet()){
			if(name.equals(key)){
				is = true;
			}
		}
		return is;
	}
	
	public String getMethod(String mname){
		String first = mname.substring(0,1);
		String left = mname.substring(1);
		return "set"+first.toUpperCase()+left;
	}
	
	public Method getMethod(Class cl, String method){
		Method mt = null;
		 Method[] ms = cl.getMethods();
		 for(Method m : ms){
			 String mname = m.getName();
	        if(mname.equals(method)){
	        	mt = m;
	        }
		 }
		 return mt;
	}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 用於Model屬性設置,是否列表顯示
 * 
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomEditDisplayField {
	
	boolean isDisplay();
}

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 用於Model屬性設置,是否列表顯示
 * 
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomInputTypeField {
	
	String inputType();
}


發佈了55 篇原創文章 · 獲贊 13 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章