今天主要實現了用validation實現登錄頁面,註冊頁面,修改信息頁面用戶名,ID,密碼不能爲空的操做html
一 登錄頁面的信息不能爲空的實現,主要代碼:首先進行包的導入java
helloWorld.html中session
<span th:if="${#fields.hasErrors('${userBean.*}')}"><span th:errors="${userBean.*}"></span></span> <span th:text="${message}"></span>
HelloWorldCotrolller.java中
app
// 登錄(將查詢的結果返回給result) @RequestMapping(value = "/init", method = RequestMethod.POST, params = "a") public String initUpdate(Model model, HttpSession session, @Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException { int init = helloWorldService.update(userBean); if (results.hasErrors()) { log.info("用戶名或密碼不能爲空"); return "helloWorld"; }//主要實現用戶名密碼不能爲空 UserBean result = helloWorldService.searchUser(userBean); model.addAttribute("userBean", result);//存userBean的值 if (result != null) // 數據列表 { List<UserBean> result1 = helloWorldService.User1(); model.addAttribute("list", result1); return "login"; } else { model.addAttribute("message", "您的用戶名或密碼有誤"); } return "helloWorld"; }
二註冊頁面的信息不能爲空的實現,主要代碼:ui
HelloWorldCotrolller.java中this
public String register(Model model, HttpSession session, @Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException { int update1 = helloWorldService.update(userBean); if (results.hasErrors()) { log.info("用戶名或密碼不能爲空"); return "register";//當用戶名或密碼爲空時返回到註冊頁面 }
三信息修改頁面不能爲空的實現,主要代碼:spa
public String executeEdit(Model model, HttpSession session, @Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) throws SQLException { int update1 = helloWorldService.update(userBean); if (results.hasErrors()) { log.info("用戶名或密碼不能爲空"); return "update"; }
在UserBean.java中,主要設置ID,用戶名,密碼不能爲空的操做,code
@NotEmpty是不能爲空的標籤
public void setUserId(String userId) { this.userId = userId; } @NotEmpty(field = "用戶名", message = "{errors.required}") private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @NotEmpty(field = "用戶密碼", message = "{errors.required}") private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }