修改時根據id傳值,點擊用戶名時跳轉到修改頁面,對應名稱後面顯示將要修改的內容,id靜態顯示,密碼不顯示html
<td><a th:href="@{delete-new(userId=${user.userId})}"><span th:text="${user.userId}"></span></a></td> <td><a th:href="@{update-new(userId=${user.userId})}"><span th:text="${user.userName}"></span></a></td>
2. string類型判斷是否爲空時可用java
if(id.isEmpty()||password.isEmpty()) 或是 if("".equals(id)||"".equals(password))
3. 修改和刪除時,漢字的傳值方式爲Get方法,這時,後臺取不到值,會顯示爲亂碼,這樣沒法進行操做,需用以下代碼sql
String type = new String(goodsForm.getType().getBytes("iso8859-1"),"utf-8");
4. 修改頁面(update-new.html)的佈局數據庫
<form action="init" th:object="${userBean}" method="post"> <div> <h3>修改我的信息</h3> <table> <tr> <td>用 戶 ID </td> <td><span th:text="${userBean.userId}"></span></td> <td><input name="userId" type="hidden" value="" th:value="${userBean.userId}"/> </td> </tr> <tr> <td>用 戶 名</td> <td><input name="userName" type="text" value="" th:value="${userBean.userName}"/> </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="${updatemessage}"></span></label></div> <tr> <td><input type="submit" value="Update" name="update" /> </td> <td><input type="submit" value="Cancel" name="cancel" /> </td> <td><input type="submit" value="Return" name="return-show" /> </td> </tr> </table> </div> </form>
5. 以列表形式展現用戶信息頁面(show.html)app
<body> <div><label><span style="display:inline;color:red;" th:text="${yes}"></span></label></div> <table> <tr> <td>UserId</td> <td>UserName</td> <td>Delete</td> <td>Update</td> </tr> <div th:each="user,s:${list}"> <tr> <td><a th:href="@{delete-new(userId=${user.userId})}"><span th:text="${user.userId}"></span></a></td> <td><a th:href="@{update-new(userId=${user.userId})}"><span th:text="${user.userName}"></span></a></td> <td><a th:href="@{delete-new(userId=${user.userId})}">delete</a></td> <td><a th:href="@{update-new(userId=${user.userId})}">update</a></td> </tr> </div> </table> <form action="init" th:object="${userBean}" method="post"> <div><input type="submit" value="返回首頁" name="returnindex" /></div> </form> </body>
6. controller中的相關代碼
佈局
全局分佈
post
@Autowired HelloWorldService helloWorldService; //跳轉到程序的首頁(index.html) @RequestMapping(value = "/", method = RequestMethod.GET) public String index(Model model) { return "index"; } //首頁(index.html)的登陸驗證 @RequestMapping(value = "/init", method = RequestMethod.POST,params="searchall") public String initLoginall(UserBean userBean, Model model) { UserBean result1 = helloWorldService.searchUser(userBean); if( result1 != null) { List<UserBean> result = helloWorldService.searchallUser(userBean); model.addAttribute("list", result); return "show"; } else {model.addAttribute("message", "您輸入的用戶ID或密碼錯誤,請從新輸入!"); } return "index"; } //首頁(index.html)跳轉到註冊頁面(zhuce.html) @RequestMapping(value = "/init", method = RequestMethod.POST,params="zhuce") public String initZhuce(UserBean userBean,Model model) { return "zhuce"; } //註冊頁面(zhuce.html)的提交 @RequestMapping(value = "/init", method = RequestMethod.POST,params="submit") public String initSubmit(UserBean usersubmit, Model model) { String id=usersubmit.getUserId().toString(); String password=usersubmit.getUserPassword().toString(); String passwordconfirm=usersubmit.getPasswordConfirm().toString(); // if("".equals(id)||"".equals(password)) if(id.isEmpty()||password.isEmpty()) { model.addAttribute("userBean", usersubmit); model.addAttribute("success", "用戶名和密碼不能爲空,請從新填寫!"); return "zhuce"; } else if(!password.equals(passwordconfirm)) { model.addAttribute("userBean", usersubmit); model.addAttribute("success", "兩次密碼輸入不一致,請從新確認密碼!"); return "zhuce"; } else { int submit = helloWorldService.submit(usersubmit); model.addAttribute("success", "註冊成功,請登陸!"); return "zhuce";} } //註冊頁面(zhuce.html)返回到首頁(index.html)登陸 @RequestMapping(value = "/init", method = RequestMethod.POST,params="back") public String initBack(UserBean userBean,Model model) { return "index"; } //列表頁面(show.html)跳轉到刪除確認頁面(confirm.html) @RequestMapping(value = "/delete-new", method = RequestMethod.GET) public String initConfirm(UserBean userBean,Model model) { return "confirm"; } //列表頁面(show.html)跳轉到更新頁面(update-new.html) @RequestMapping(value = "/update-new", method = RequestMethod.GET) public String initUpdatenew(UserBean userBean,Model model) { UserBean result = helloWorldService.searchupUser(userBean); model.addAttribute("userBean", result); return "update-new"; } //列表頁面(show.html)跳轉到程序的首頁(index.html) @RequestMapping(value = "/init", method = RequestMethod.POST,params="returnindex") public String returnindex(UserBean userBean,Model model) { return "index"; } //更新頁面(update-new.html)的修改判斷 @RequestMapping(value = "/init", method = RequestMethod.POST,params="update") public String initUpdate(UserBean userupdate, Model model) { String updateps=userupdate.getUserPassword().toString(); String updatepsc=userupdate.getPasswordConfirm().toString(); if(updateps.equals(updatepsc)) {int update = helloWorldService.update(userupdate); model.addAttribute("updatemessage", "修改爲功!");} else {model.addAttribute("updatemessage", "請從新確認密碼!");} return "update-new"; } //更新頁面(update-new.html)取消操做 @RequestMapping(value = "/init", method = RequestMethod.POST,params="cancel") public String initCancel(UserBean usercancel, Model model) { return "update-new"; } //更新頁面(update-new.html)跳轉回列表頁面(show.html) @RequestMapping(value = "/init", method = RequestMethod.POST,params="return-show") public String initReturn(UserBean userreturn, Model model) { List<UserBean> result = helloWorldService.searchallUser(userreturn); model.addAttribute("list", result); return "show"; } //刪除確認頁面(confirm.html)的確認操做 @RequestMapping(value = "/init", method = RequestMethod.POST,params="yes") public String initYes(UserBean userdelete,Model model) { int delete = helloWorldService.delete(userdelete); List<UserBean> result = helloWorldService.searchallUser(userdelete); model.addAttribute("list", result); model.addAttribute("yes", "刪除成功!"); return "show"; } //刪除確認頁面(confirm.html)的取消操做 @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); model.addAttribute("yes", "取消刪除!"); return "show"; }
7. service中的相關代碼spa
@Autowired QueryDAO queryDao; @Autowired UpdateDAO updateDao; public UserBean searchUser(UserBean frm) { // UserBean result= queryDao.executeForObject("User.selectUser", frm, UserBean.class); frm = queryDao.executeForObject("User.selectUser", frm, UserBean.class); //frm.setUserId("test"); return frm; } public UserBean searchupUser(UserBean frm) { // UserBean result= queryDao.executeForObject("User.selectUser", frm, UserBean.class); frm = queryDao.executeForObject("User.selectupUser", frm, UserBean.class); //frm.setUserId("test"); return frm; } public List<UserBean> searchallUser(UserBean frm) { List<UserBean> result= queryDao.executeForObjectList("User.selectallUser", frm); //frm = queryDao.executeForObject("User.selectUser", frm, UserBean.class); //frm.setUserId("test"); return result; } public int submit(UserBean frm){ return updateDao.execute("User.submitUser",frm); } public int delete(UserBean frm){ return updateDao.execute("User.deleteUser",frm); } public int insert(UserBean frm){ return updateDao.execute("User.insertUser",frm); } public int update(UserBean frm){ return updateDao.execute("User.updateUser",frm); }
8. sqlMap.xml中相關數據庫操做的代碼code
<sqlMap namespace="User"> <select id="selectUser" parameterClass="cn.training.bean.UserBean" resultClass="cn.training.bean.UserBean"> SELECT userid as userId,userPassword FROM user WHERE userid = #userId# and userPassword=#userPassword# </select> <select id="selectupUser" parameterClass="cn.training.bean.UserBean" resultClass="cn.training.bean.UserBean"> SELECT userid as userId,userName FROM user WHERE userid = #userId# </select> <select id="selectallUser" parameterClass="cn.training.bean.UserBean" resultClass="cn.training.bean.UserBean"> SELECT userid as userId,userName FROM user </select> <delete id="deleteUser" parameterClass="cn.training.bean.UserBean"> delete from user where userid=#userId# </delete> <insert id="submitUser" parameterClass="cn.training.bean.UserBean"> INSERT INTO user VALUES(#userId#,#userName#,#userPassword#) </insert> <insert id="insertUser" parameterClass="cn.training.bean.UserBean"> INSERT INTO user VALUES(#userId#,#userName#) </insert> <update id="updateUser" parameterClass="cn.training.bean.UserBean"> update user set userName=#userName# , userPassword=#userPassword# where userid=#userId# </update> </sqlMap>
9.註冊頁面(zhuce.html)orm
<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>
10.首頁-登陸頁面(index.html)
<div align="center"> <form action="init" th:object="${userBean}" method="post"> <div><h3>用 戶 登 錄</h3></div> <div><label><span style="display:inline;color:red;" th:text="${message}"></span></label></div> <div> <tr> <td>用戶ID</td> <td><input name="userId" type="text" /></td> </tr> </div> <div> <tr> <td>密 碼</td> <td><input name="userPassword" type="password" /></td> </tr> </div> <div> <tr> <td><input type="submit" value="登 錄" name="searchall" /></td> <td><input type="submit" value="注 冊" name="zhuce" /></td> </tr> </div> </form> </div>