springMvc 的引數驗證 BindingResult result 的使用
阿新 • • 發佈:2018-12-07
ttp://blog.sina.com.cn/s/blog_6829be5c0101alxh.html 非常詳細的一篇講解 並且值得深入探討
http://bbs.csdn.NET/topics/360254599 簡單明瞭
1. 首先匯入依賴包bean-validator.jar
2.在實體類上面寫一些相關的驗證資訊:可以搜尋更多的一些驗證方式,這只是一部分
可以參考:點選開啟連結http://blog.csdn.Net/caihaijiang/article/details/7463514
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; }
2、後臺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"; }
1、引數集合實體
package com.yzf.cloud.funds.model.vo; import java.io.Serializable; import java.util.List; import javax.validation.Valid; import org.hibernate.validator.constraints.NotEmpty; 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; } }
2、引數實體
package com.yzf.cloud.funds.model.vo;
import java.io.Serializable;
import java.math.BigInteger;
import org.hibernate.validator.constraints.NotEmpty;
/**
* 現金日記明細表
*
* @author win 10
*
*/
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;
}
}
3、controer寫法
@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);
}