验证类
public class BeanValidUtil { /** * 验证参数bean(包括内部的子bean) * * @param t * 参数对象 例如Fxfa * @param optype * 操作类型 枚举 * @throws FkptServiceException */ @SuppressWarnings({"rawtypes", "unchecked"}) public static void valid(Object t, OperateType optype) throws FkptServiceException { try { // 验证主的参数bean validSingle(t, optype); // 验证主bean中的list对象 Class cls = t.getClass(); for (Field field : cls.getDeclaredFields()) { if (field.getType() == List.class) { PropertyDescriptor pd = new PropertyDescriptor(field.getName(), cls); Method getMethod = pd.getReadMethod();// 获得get方法 List<Object> list = (List<Object>) getMethod.invoke(t); if (!CollectionUtils.isEmpty(list)) { for (Object o : list) { validSingle(o, optype); } } } } } catch (Exception e) { throw new FkptServiceException(e.getMessage(), e); } } @SuppressWarnings("rawtypes") public static void validSingle(Object t, OperateType optype) throws FkptServiceException { StringBuilder err = new StringBuilder(""); // 获取注解信息 BeanVaild bv = null; Class cls = t.getClass(); // 检测field是否存在 try { Field[] fields = cls.getDeclaredFields(); for (Field f : fields) { bv = f.getAnnotation(BeanVaild.class); // 不为空执行校验 if (bv != null) { // 新增验证 if (optype == OperateType.ADD && bv.addOp()) { err.append(checkType(f, bv.type(), t)); } else if (optype == OperateType.UPDATE && bv.updateOp()) { // 更新验证 err.append(checkType(f, bv.type(), t)); } } } String errmsg = err.toString(); if (!StringUtil.isNullString(errmsg)) { throw new FkptServiceException(errmsg); } } catch (Exception e) { throw new FkptServiceException(ServiceExcuteMessage.FKPT_BEAN_VALID_001, e, t.getClass().getName(), e.getMessage()); } } // 根据不同的验证类型执行验证操作 public static StringBuilder checkType(Field field, ValidTypes type, Object t) throws Exception { StringBuilder err = new StringBuilder(); switch (type) { case NOTNULL: err.append(checkNotNull(field.getName(), type, t)); break; default: err.append(""); break; } return err; } // 非空验证 private static StringBuilder checkNotNull(String fieldName, ValidTypes type, Object t) throws Exception { PropertyDescriptor pd = new PropertyDescriptor(fieldName, t.getClass()); Method getMethod = pd.getReadMethod();// 获得get方法 String fieldVal = (String) getMethod.invoke(t); if (StringUtil.isNullString(fieldVal)) { return new StringBuilder("获取【" + fieldName + "】失败,该字段不能为空\n"); } else { return new StringBuilder(""); } } }
注解接口
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface BeanVaild { /** * 验证类型 */ ValidTypes type() default ValidTypes.NOTNULL; /** * 新增操作类型 */ boolean addOp() default false; /** * 更新操作类型 */ boolean updateOp() default false; }
类型枚举
public enum OperateType { /** * 操作类型 新增 */ ADD, /** * 操作类型 更新 */ UPDATE }
public enum ValidTypes { /** * 不能为空 */ NOTNULL }
javabean主类
public class Fxfa extends BaseBean { private static final long serialVersionUID = 8239685879814948678L; /** * 方案编号 */ @BeanVaild(type = ValidTypes.NOTNULL, updateOp = true) private String fabh; /** * 方案名称 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String famc; /** * 方案类型 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true) private String falx; /** * 优先级 */ private String yxj; /** * 优先级名称 */ private String yxjMc; /** * 版本 */ private String bb; /** * 预警值测算方案 */ private String yjzcsfa; /** * 预警值测算方案名称 */ private String yjzcsfaMc; /** * 父方案代码,即上一版本方案代码 */ private String fabhF; /** * 初方案代码,即初始版本方案代码 */ private String fabhC; /** * 有效标志(Y、N) */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true) private String yxbz; /** * 来源(1:系统自建;2:外部导入) */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true) private String ly; /** * 制定人员代码 */ private String zdRyDm; /** * 制定税务机关代码 */ private String zdSwjgDm; /** * 制定时间 */ private String zdsj; /** * 修改人员代码 */ private String xgRyDm; /** * 修改税务机关代码 */ private String xgSwjgDm; /** * 修改时间 */ private String xgsj; /** * 关键词 */ private String gjc; /** * 说明 */ private String sm; /** * 纳税评估项目ID */ private String nspgxmid; /** * 纳税评估项目MC */ private String nspgxmmc; /** * 系统代码 */ private String xtdm; /** * 分析模型列表 */ private List<FxfaFxmxXx> lstFxmxXx = new ArrayList<FxfaFxmxXx>(); /** * 指标元条件 */ private List<FxfaZbyTj> lstZbyTj = new ArrayList<FxfaZbyTj>(); /** * 维度条件列表 */ private List<FxfaWdTj> lstWdTj = new ArrayList<FxfaWdTj>(); /** * @return the lstZbyTj */ public List<FxfaZbyTj> getLstZbyTj() { return lstZbyTj; } /** * @param lstZbyTj * the lstZbyTj to set */ public void setLstZbyTj(List<FxfaZbyTj> lstZbyTj) { this.lstZbyTj = lstZbyTj; } /** * @return the lstWdTj */ public List<FxfaWdTj> getLstWdTj() { return lstWdTj; } /** * @param lstWdTj * the lstWdTj to set */ public void setLstWdTj(List<FxfaWdTj> lstWdTj) { this.lstWdTj = lstWdTj; } /** * @return the fabh */ public String getFabh() { return fabh; } /** * @param fabh * the fabh to set */ public void setFabh(String fabh) { this.fabh = fabh; } /** * @return the famc */ public String getFamc() { return famc; } /** * @param famc * the famc to set */ public void setFamc(String famc) { this.famc = famc; } /** * @return the falx */ public String getFalx() { return falx; } /** * @param falx * the falx to set */ public void setFalx(String falx) { this.falx = falx; } /** * @return the yxj */ public String getYxj() { return yxj; } /** * @param yxj * the yxj to set */ public void setYxj(String yxj) { this.yxj = yxj; } /** * @return the bb */ public String getBb() { return bb; } /** * @param bb * the bb to set */ public void setBb(String bb) { this.bb = bb; } /** * @return the fabhF */ public String getFabhF() { return fabhF; } /** * @param fabhF * the fabhF to set */ public void setFabhF(String fabhF) { this.fabhF = fabhF; } /** * @return the fabhC */ public String getFabhC() { return fabhC; } /** * @param fabhC * the fabhC to set */ public void setFabhC(String fabhC) { this.fabhC = fabhC; } /** * @return the yxbz */ public String getYxbz() { return yxbz; } /** * @param yxbz * the yxbz to set */ public void setYxbz(String yxbz) { this.yxbz = yxbz; } /** * @return the ly */ public String getLy() { return ly; } /** * @param ly * the ly to set */ public void setLy(String ly) { this.ly = ly; } /** * @return the zdRyDm */ public String getZdRyDm() { return zdRyDm; } /** * @param zdRyDm * the zdRyDm to set */ public void setZdRyDm(String zdRyDm) { this.zdRyDm = zdRyDm; } /** * @return the zdSwjgDm */ public String getZdSwjgDm() { return zdSwjgDm; } /** * @param zdSwjgDm * the zdSwjgDm to set */ public void setZdSwjgDm(String zdSwjgDm) { this.zdSwjgDm = zdSwjgDm; } /** * @return the zdsj */ public String getZdsj() { return zdsj; } /** * @param zdsj * the zdsj to set */ public void setZdsj(String zdsj) { this.zdsj = zdsj; } /** * @return the xgRyDm */ public String getXgRyDm() { return xgRyDm; } /** * @param xgRyDm * the xgRyDm to set */ public void setXgRyDm(String xgRyDm) { this.xgRyDm = xgRyDm; } /** * @return the xgSwjgDm */ public String getXgSwjgDm() { return xgSwjgDm; } /** * @param xgSwjgDm * the xgSwjgDm to set */ public void setXgSwjgDm(String xgSwjgDm) { this.xgSwjgDm = xgSwjgDm; } /** * @return the xgsj */ public String getXgsj() { return xgsj; } /** * @param xgsj * the xgsj to set */ public void setXgsj(String xgsj) { this.xgsj = xgsj; } /** * @return the gjc */ public String getGjc() { return gjc; } /** * @param gjc * the gjc to set */ public void setGjc(String gjc) { this.gjc = gjc; } /** * @return the yjzcsfa */ public String getYjzcsfa() { return yjzcsfa; } /** * @param yjzcsfa * the yjzcsfa to set */ public void setYjzcsfa(String yjzcsfa) { this.yjzcsfa = yjzcsfa; } /** * @return the lstFxmxXx */ public List<FxfaFxmxXx> getLstFxmxXx() { return lstFxmxXx; } /** * @param lstFxmxXx * the lstFxmxXx to set */ public void setLstFxmxXx(List<FxfaFxmxXx> lstFxmxXx) { this.lstFxmxXx = lstFxmxXx; } /** * @return the sm */ public String getSm() { return sm; } /** * @param sm * the sm to set */ public void setSm(String sm) { this.sm = sm; } /** * @return the yjzcsfaMc */ public String getYjzcsfaMc() { return yjzcsfaMc; } /** * @param yjzcsfaMc * the yjzcsfaMc to set */ public void setYjzcsfaMc(String yjzcsfaMc) { this.yjzcsfaMc = yjzcsfaMc; } /** * @return the yxjMc */ public String getYxjMc() { return yxjMc; } /** * @param yxjMc * the yxjMc to set */ public void setYxjMc(String yxjMc) { this.yxjMc = yxjMc; } /** * @return 获取 nspgxmid */ public String getNspgxmid() { return nspgxmid; } /** * @param nspgxmid * 设置 nspgxmid */ public void setNspgxmid(String nspgxmid) { this.nspgxmid = nspgxmid; } /** * @return 获取 nspgxmmc */ public String getNspgxmmc() { return nspgxmmc; } /** * @param nspgxmmc * 设置 nspgxmmc */ public void setNspgxmmc(String nspgxmmc) { this.nspgxmmc = nspgxmmc; } /** * @return the xtdm */ public String getXtdm() { return xtdm; } /** * @param xtdm * the xtdm to set */ public void setXtdm(String xtdm) { this.xtdm = xtdm; } }
字bean
public class FxfaWdTj implements Serializable { private static final long serialVersionUID = -7624527525952631847L; /** * 方案编号 */ private String fabh; /** * 序号 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String xh; /** * 条件名称 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String tjmc; /** * 条件表 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String tjb; /** * 条件字段 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String tjzd; /** * 条件符 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String tjf; /** * 条件值 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String tjz; /** * 页面显示代码 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String xsdm; /** * 页面显示名称 */ @BeanVaild(type = ValidTypes.NOTNULL, addOp = true, updateOp = true) private String xsmc; /** * @return the fabh */ public String getFabh() { return fabh; } /** * @param fabh * the fabh to set */ public void setFabh(String fabh) { this.fabh = fabh; } /** * @return the tjmc */ public String getTjmc() { return tjmc; } /** * @param tjmc * the tjmc to set */ public void setTjmc(String tjmc) { this.tjmc = tjmc; } /** * @return the tjb */ public String getTjb() { return tjb; } /** * @param tjb * the tjb to set */ public void setTjb(String tjb) { this.tjb = tjb; } /** * @return the tjzd */ public String getTjzd() { return tjzd; } /** * @param tjzd * the tjzd to set */ public void setTjzd(String tjzd) { this.tjzd = tjzd; } /** * @return the tjf */ public String getTjf() { return tjf; } /** * @param tjf * the tjf to set */ public void setTjf(String tjf) { this.tjf = tjf; } /** * @return the tjz */ public String getTjz() { return tjz; } /** * @param tjz * the tjz to set */ public void setTjz(String tjz) { this.tjz = tjz; } /** * @return the xsdm */ public String getXsdm() { return xsdm; } /** * @param xsdm * the xsdm to set */ public void setXsdm(String xsdm) { this.xsdm = xsdm; } /** * @return the xsmc */ public String getXsmc() { return xsmc; } /** * @param xsmc * the xsmc to set */ public void setXsmc(String xsmc) { this.xsmc = xsmc; } /** * @return the xh */ public String getXh() { return xh; } /** * @param xh * the xh to set */ public void setXh(String xh) { this.xh = xh; } }
验证调用
// 新增 if (StringUtil.isNullString(fxfa.getFabh())) { // 新增时验证参数 BeanValidUtil.valid(fxfa, OperateType.ADD); fxfa.setFabh(UUIDUtil.nextVal()); ServiceExecuteUtil.executeDao("fxpt.core.fak.fxfa.dao.insertFxfa", new Object[] {fxfa, loginuser}); } else { // 修改 // 修改时验证参数 BeanValidUtil.valid(fxfa, OperateType.UPDATE); ServiceExecuteUtil.executeDao("fxpt.core.fak.fxfa.dao.updateFxfa", new Object[] {fxfa, loginuser}); }
相关推荐
java反射实现javabean转json实例代码.docx
利用Javabean+JSP 实现文件的上传、显示、下载 实现文件上传功能和下载功能,显示文件列表和读取文件内容功能。
很多公司里面不会用到H 所以只需要用SS不用h 相反来说用jdbc实现H的功能。
03 JavaBean验证用户登录实例.exe03 JavaBean验证用户登录实例.exe
简单实现如何不创建简单JavaBean对象,而仅仅利用反射实现向普通JavaBean中set值和get值
注解javaBean的优点,对应相应的数据库字段
使用java反射机制封装javabean 项目当中使用的一个通用的方法
Jsp中利用JavaBean技术实现分页功能 Jsp中利用JavaBean技术实现分页功能
这份文档是我开会的时候记录下来有关javabean的注解方式大概怎么用和一些操作
使用 Java 反射封装 JavaBean <br> 说明:这个程序在没有使用Hiberante时挺有用的 需要一个数据库查询语句 对应的JavaBean的全名(现在是Class,原来Class.getName()) 返回List包含多个JavaBean
javaBean/Log验证javaBean/Log验证javaBean/Log验证javaBean/Log验证javaBean/Log验证javaBean/Log验证javaBean/Log验证javaBean/Log验证javaBean/Log验证javaBean/Log验证
简单的J2EE系统实现对学生姓名和学号进行验证,身份确任代码与身份验证代码
本例程绝对能运行,是一个在MyEcplise下开发的工程文件,jar包完整,利用MVC框架实现了文件的上传,上传文件保存在服务器端的upload文件夹下。
基于标注和反射机制的通用JavaBean持久化工具类.pdf
1.通过Java反射机制获取JavaBean对象。 2.通过JavaBean全路径字符串获取JavaBean对象。 3.获取JavaBean注解信息
Servlet+JSP+JavaBean(MVC)模式适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据。 Servlet+JSP、JavaBean模式程序各个模块之间层次清晰,web开发推荐采用此...
NULL 博文链接:https://luyuwww.iteye.com/blog/1988355
利用servlet+jsp+javabean进行分页查询,用到的数据库为mysql
jsp+servlet+javaBean实现MVC jsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+...