博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring mvc 4 校验
阅读量:6582 次
发布时间:2019-06-24

本文共 4600 字,大约阅读时间需要 15 分钟。

一、controller中添加:

@Resource GatewayValidator gatewayValidator;
@RequestMapping(value = "/gateway/signup", method = RequestMethod.POST)public ApiResponse signup(@Valid Gateway gateway, BindingResult result) {    ApiResponse rspResult = new ApiResponse();    gatewayValidator.validate(gateway, result);    if(result.hasErrors()){        rspResult.setCode(ApiCode.PARMAS_ERROR);        rspResult.setMessage(BaseUtils.GetValidErrorMessage(result));    }    else {        //do something    }    return rspResult;}

二、model中添加(简单的校验可以直接卸载model类中):

package smarthome.api.models;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import org.apache.commons.lang3.time.DateUtils;import org.hibernate.validator.constraints.NotEmpty;import java.io.Serializable;import java.util.ArrayList;import java.util.Date;import java.util.List;public class Gateway extends BaseClass implements Serializable {        /**     * None     */    protected String vendorCode;    /**     * None     */    protected String productCode;    /**     * None     */    @NotEmpty(message = "序列号不能为空")    protected String serialNumber;    public Gateway() {    }        public String getVendorCode() {        return vendorCode;    }    public void setVendorCode(String vendorCode) {        this.vendorCode = vendorCode == null ? null : vendorCode.trim();    }    public String getProductCode() {        return productCode;    }    public void setProductCode(String productCode) {        this.productCode = productCode == null ? null : productCode.trim();    }    public String getSerialNumber() {        return serialNumber;    }    public void setSerialNumber(String serialNumber) {        this.serialNumber = serialNumber == null ? null : serialNumber.trim();    }}

三、简单的校验

package smarthome.api.validators;import org.apache.commons.lang3.StringUtils;import org.springframework.stereotype.Component;import org.springframework.validation.Errors;import org.springframework.validation.ValidationUtils;import org.springframework.validation.Validator;import javax.annotation.Resource;import java.util.List;@Componentpublic class GatewayValidator implements Validator {    public boolean supports(Class clazz) {        return Gateway.class.equals(clazz);    }    public void validate(Object target, Errors errors) {        Gateway gateway = (Gateway) target;        if(gateway.getGatewayId() == null && StringUtils.isEmpty(gateway.getSerialNumber())){            errors.rejectValue("gatewayId", null, "网关编号和序列号不能同时为空");            errors.rejectValue("serialNumber", null, "网关编号和序列号不能同时为空");        }    }}

 

四、嵌套的校验

1、父类(gateway)中添加校验

package smarthome.api.validators;import org.apache.commons.lang3.StringUtils;import org.springframework.stereotype.Component;import org.springframework.validation.Errors;import org.springframework.validation.ValidationUtils;import org.springframework.validation.Validator;import javax.annotation.Resource;import java.util.List;@Componentpublic class GatewayValidator implements Validator {    @Resource    private ModuleValidator moduleValidator;    public boolean supports(Class clazz) {        return Gateway.class.equals(clazz);    }    public void validate(Object target, Errors errors) {        Gateway gateway = (Gateway) target;        if(gateway.getGatewayId() == null && StringUtils.isEmpty(gateway.getSerialNumber())){            errors.rejectValue("gatewayId", null, "网关编号和序列号不能同时为空");            errors.rejectValue("serialNumber", null, "网关编号和序列号不能同时为空");        }        for(Module module : (List
)gateway.getModuleList()){ ValidationUtils.invokeValidator(moduleValidator, module, errors); } }}

2、子类(module)中的校验

package smarthome.api.validators;import org.apache.commons.lang3.StringUtils;import org.springframework.stereotype.Component;import org.springframework.validation.Errors;import org.springframework.validation.ValidationUtils;import org.springframework.validation.Validator;import javax.annotation.Resource;@Componentpublic class ModuleValidator implements Validator {    @Resource    private IModuleService moduleService;    public boolean supports(Class clazz) {        return Module.class.equals(clazz);    }    public void validate(Object target, Errors errors) {        Module module = (Module) target;        if (StringUtils.isEmpty(module.getSerialNumber())) {            errors.rejectValue("serialNumber", null, "模块序号不能为空");        }        else {            if(moduleService.getFirst(module.getSerialNumber()) != null){                errors.rejectValue("serialNumber", null, "模块序列号已经存在");            }        }    }}

 

转载地址:http://vfino.baihongyu.com/

你可能感兴趣的文章
1.2 linux哲学思想
查看>>
jQuery基础
查看>>
BZOJ5312:冒险——题解
查看>>
echarts,两点连线,中间断裂
查看>>
samba简易配置
查看>>
庆祝在CNBlogs开博!
查看>>
javascript reverse string
查看>>
南阳oj 题目6 喷水装置(一)
查看>>
运筹学上机实验 - 单纯形方法的两阶段法
查看>>
CF294C Shaass and Lights
查看>>
oracle 11g 报错记录
查看>>
文件状态是否变化
查看>>
MongoDB的副本集Replica Set
查看>>
Maven项目中的配置文件找不到以及打包问题
查看>>
面向对象
查看>>
HDU 1058 Humble Numbers
查看>>
NYOJ The Triangle
查看>>
wps10.1中将txt转为excel
查看>>
并发同步知多少
查看>>
解决执行脚本报syntax error: unexpected end of file或syntax error near unexpected token `fi'错误的问题...
查看>>