替換UserBean.java裏的private爲java
@NotEmpty(field="用戶ID", message="{errors.required}")session
private String userId;ui
@NotEmpty(field="用戶姓名", message="{errors.required}")spa
private String userName;code
@NotEmpty(field="用戶密碼", message="{errors.required}")orm
private String password;對象
爲登錄,註冊,修改,添加條件ip
(Model model, HttpSession session, @Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException {it
if (results.hasErrors()) {io
log.info("內容驗證出錯");
return "register";}}
其中登錄中須建立新的實例對象
UserBean userBean=new UserBean();
model.addAttribute("userBean",userBean);
對應的頁面中顯示內容需添加
<span th:if="${#fields.hasErrors('${userBean.*}')}"><span th:errors="${userBean.*}"></span></span>
<span th:text="${message}"></span>
注意驗證信息條件:
if(device.isNormal()) { return "mobile/alipay/distributorAlipayConfirm"; } else { return "mobile/alipay/distributorAlipayConfirm"; }