設計3個相關聯的HTML頁面,首頁(index.html)爲用戶登陸,輸入用戶名、密碼與數據庫相匹配,信息錯誤時會顯示信息「您輸入的用戶名或密碼錯誤,請從新輸入!」;點擊「登陸」會跳轉到用戶列表頁面(show.html),該頁面以列表形式輸出全部用戶的ID與Name,並可進行刪除操做,點擊刪除會跳轉到確認頁面,"yes"刪除成功,"no"則取消刪除;由首頁點擊「註冊」進入註冊頁面(zhuce.html),該頁面包括ID、用戶名、密碼、密碼確認html
登陸失敗時,錯誤信息的輸出:<span th:text="${message}"></span>java
@RequestMapping(value = "/index", method = RequestMethod.POST, params="search") public String initLogin(UserBean userBean, Model model) { UserBean result = helloWorldService.searchUser(userBean); List<UserBean> resul = helloWorldService.searchUserall(userBean); if(result!=null){ model.addAttribute("List", resul); return "search"; }else{ model.addAttribute("message", "您輸入的用戶名或密碼錯誤,請從新輸入!"); return "index"; } }
3.註冊時密碼確認數據庫
<form action="init" th:object="${userBean}" method="post"> <div> <h3>註冊信息</h3> <table> <tr> <td>ID </td> <td><input name="userId" type="text" /> </td> </tr> <tr> <td>用 戶 名</td> <td><input name="userName" type="text" /> </td> </tr> <tr> <td>密 碼</td> <td><input name="userPassword" type="password"/> </td> </tr> <tr> <td>確認密碼 </td> <td><input name="passwordConfirm" type="password" /> </td> </tr> <div><label><span style="display:inline;color:red;" th:text="${success}"></span></label></div> <tr> <td><input type="submit" value="Submit" name="submit" /> </td> <td><input type="submit" value="Back" name="back" /> </td> </tr> </table> </div> </form>
@RequestMapping(value = "/init", method = RequestMethod.POST,params="submit") public String initSubmit(UserBean usersubmit, Model model) { // String id=usersubmit.getUserId().toString(); // String name=usersubmit.getUserName().toString(); String password=usersubmit.getUserPassword().toString(); // String password2=usersubmit.getPasswordConfirm().toString if(password.equals(password2)) { int submit = helloWorldService.submit(usersubmit); // UserBean user=new UserBean(); // model.addAttribute("userBean", user); if(submit==1) {model.addAttribute("success", "註冊成功,請登陸!"); return "zhuce"; } else {model.addAttribute("success", "註冊失敗,請從新填寫!"); return "zhuce";} } else {model.addAttribute("success", "註冊失敗,請從新填寫!"); return "zhuce";} }
4·刪除確認app
<form action="init" th:object="${userBean}" method="post"> <input name="userId" type="hidden" th:value="${userBean.userId}"/> <div><input type="submit" value="確認(Y)" name="yes" /> </div> <div><input type="submit" value="取消(N)" name="no" /> </div> </form>
@RequestMapping(value = "/init", method = RequestMethod.POST,params="yes") public String initYes(UserBean userBean,Model model) { int result1 = helloWorldService.deleteUser(userBean); List<UserBean> result = helloWorldService.searchallUser(userBean); model.addAttribute("list", result); return "show"; } @RequestMapping(value = "/init", method = RequestMethod.POST,params="no") public String initNo(UserBean userBean,Model model) { List<UserBean> result = helloWorldService.searchallUser(userBean); model.addAttribute("list", result); return "show"; }
五、注意SQL表達式的正確書寫規則,如:post
SELECT userid as userId,userPassword FROM user WHERE userid = #userId# and userPassword=#userPassword#
「where」條件並列需用「and」spa
六、int的比較用「=」,而string類型的比較則須要「a.equals(b)」的形式設計