validation

替换UserBean.java里的private为

@NotEmpty(field="用户ID",  message="{errors.required}")

private String userId;

@NotEmpty(field="用户姓名",  message="{errors.required}")

private String userName;

@NotEmpty(field="用户密码",  message="{errors.required}")

private String password;

为登陆,注册,修改,添加条件

(Model model, HttpSession session, @Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException {

if (results.hasErrors()) {

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";
	    	}		


你可能感兴趣的:(validation)