springMvc 的参数验证 BindingResult result 的使用

首先导入依赖包bean-validator.jar

private String username;  
    private String password;  
    private String nickname;  
    private String email;  
      
    public User() {  
    }  
  
    @NotEmpty(message="用户名不能为空")  
    public String getUsername() {  
        return username;  
    }  
  
    public void setUsername(String username) {  
        this.username = username;  
    }  
  
    @Size(min=1,max=10,message="密码的长度应该在1和10之间")  
    public String getPassword() {  
        return password;  
    }  
  
    public void setPassword(String password) {  
        this.password = password;  
    }  
  
    public String getNickname() {  
        return nickname;  
    }  
  
    public void setNickname(String nickname) {  
        this.nickname = nickname;  
    }  
  
    @Email(message="邮箱的格式不正确")  
    public String getEmail() {  
        return email;  
    }  
  
    public void setEmail(String email) {  
        this.email = email;  
    } 

  后台controller处理

@RequestMapping("save")  
    public String save( @Valid User user,BindingResult result) {  
        //  
        if(result.hasErrors()){  
            List<ObjectError> ls=result.getAllErrors();  
            for (int i = 0; i < ls.size(); i++) {  
                System.out.println("error:"+ls.get(i));  
            }  
        }  
        return "adduser";  
    }  

  参数集合实体

public class CashJournalsVo implements Serializable{
 
	private static final long serialVersionUID = 1L;
	
	/**
	 * 账套UID
	 */
	@NotEmpty
	private String accSetUid;
 
	/**
	 * 账期
	 */
	@NotEmpty
	private String fiscalPeriod;
 
	/**
	 * 流水号
	 */
	@NotEmpty
	private String flowSeq;
	
	/**
	 * 现金明细数据集合
	 */
	@Valid
	private List<CashJournalVO> cashJournalVOs;
 
	public String getAccSetUid() {
		return accSetUid;
	}
 
	public void setAccSetUid(String accSetUid) {
		this.accSetUid = accSetUid;
	}
 
	public String getFiscalPeriod() {
		return fiscalPeriod;
	}
 
	public void setFiscalPeriod(String fiscalPeriod) {
		this.fiscalPeriod = fiscalPeriod;
	}
 
	public String getFlowSeq() {
		return flowSeq;
	}
 
	public void setFlowSeq(String flowSeq) {
		this.flowSeq = flowSeq;
	}
 
	public List<CashJournalVO> getCashJournalVOs() {
		return cashJournalVOs;
	}
 
	public void setCashJournalVOs(List<CashJournalVO> cashJournalVOs) {
		this.cashJournalVOs = cashJournalVOs;
	}
 
 
}

  参数实体

public class CashJournalVO implements Serializable {
	private static final long serialVersionUID = 1L;
	// 日记账表id
	private String id;
	// 日期
	@NotEmpty
	private String tradeDate;
	// 业务类型
	private String busitype;
	// 业务类型Uid
	@NotEmpty
	private String busitypeUid;
	// 往来单位
	private String dealings;
	// 业务主数据Uid
	@NotEmpty
	private String dealingsUid;
	// 收入金额
	private BigInteger incomeAmount;
	// 支出金额
	private BigInteger expenseAmount;
	// 备注
	@NotEmpty
	private String remark;
	// 主数据Uid
	@NotEmpty
	private String mdataUid;
 
	public String getId() {
		return id;
	}
 
	public void setId(String id) {
		this.id = id;
	}
 
	public String getTradeDate() {
		return tradeDate;
	}
 
	public void setTradeDate(String tradeDate) {
		this.tradeDate = tradeDate;
	}
 
	public String getBusitype() {
		return busitype;
	}
 
	public void setBusitype(String busitype) {
		this.busitype = busitype;
	}
 
	public String getBusitypeUid() {
		return busitypeUid;
	}
 
	public void setBusitypeUid(String busitypeUid) {
		this.busitypeUid = busitypeUid;
	}
 
	public String getDealings() {
		return dealings;
	}
 
	public void setDealings(String dealings) {
		this.dealings = dealings;
	}
 
	public String getDealingsUid() {
		return dealingsUid;
	}
 
	public void setDealingsUid(String dealingsUid) {
		this.dealingsUid = dealingsUid;
	}
 
	public BigInteger getIncomeAmount() {
		return incomeAmount;
	}
 
	public void setIncomeAmount(BigInteger incomeAmount) {
		this.incomeAmount = incomeAmount;
	}
 
	public BigInteger getExpenseAmount() {
		return expenseAmount;
	}
 
	public void setExpenseAmount(BigInteger expenseAmount) {
		this.expenseAmount = expenseAmount;
	}
 
	public String getRemark() {
		return remark;
	}
 
	public void setRemark(String remark) {
		this.remark = remark;
	}
 
	public String getMdataUid() {
		return mdataUid;
	}
 
	public void setMdataUid(String mdataUid) {
		this.mdataUid = mdataUid;
	}
}

  controller写法

@PostMapping("/updateCashJournal")
	public ResultJson updateCashJournal(@RequestHeader("tenantUid") String tenantUid,
			@Valid @RequestBody CashJournalsVo cashJournalsVo, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			logger.info("传递参数有误:" + new JSONObject(bindingResult.getFieldError()).toString());
			return buildResultJson(FAIL, "编辑失败", "编辑现金日记账");
		}
		return cashJournalAccountService.updateCashJournal(tenantUid, cashJournalsVo, personName);
	}

  

posted @ 2021-02-19 10:19  巧克力曲奇  阅读(204)  评论(0编辑  收藏  举报