工具类
2015-06-12 16:33
148 查看
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(); }
相关文章推荐
- net_kernel:monitor_nodes 订阅node连接\断开消息
- Leetcode[113]-Path Sum II
- DTcmsV4.0分析学习——(3)URL重写
- 触摸事件的setSwallowTouches()方法
- css position:absolute align center bottom
- UIView全部API的学习。
- 三层架构篇
- Redis集群部署(二)
- mysql存储过程注释
- 小白胡说:继承
- Android平台第三方应用分享到微信开发
- gRPC版本的 Google APIs
- WCF 超时情形
- 新GRE写作高分速成句式
- centos 谷歌浏览器安装
- Android 有关文件路径的常用方法
- linux下分析Java程序内存汇总
- Eclipse发生异常Unhandled event loop exception No more handles
- Failed to load ldlinx.c32
- MapReduce编程之WordCount