设计3个相关联的HTML页面,首页(index.html)为用户登录,输入用户名、密码与数据库相匹配,信息错误时会显示信息“您输入的用户名或密码错误,请重新输入!”;点击“登录”会跳转到用户列表页面(show.html),该页面以列表形式输出所有用户的ID与Name,并可进行删除操作,点击删除会跳转到确认页面,"yes"删除成功,"no"则取消删除;由首页点击“注册”进入注册页面(zhuce.html),该页面包括ID、用户名、密码、密码确认
登录失败时,错误信息的输出:<span th:text="${message}"></span>
@RequestMapping(value = "/index", method = RequestMethod.POST, params="search") public String initLogin(UserBean userBean, Model model) { UserBean result = helloWorldService.searchUser(userBean); Listresul = helloWorldService.searchUserall(userBean); if(result!=null){ model.addAttribute("List", resul); return "search"; }else{ model.addAttribute("message", "您输入的用户名或密码错误,请重新输入!"); return "index"; } }
3.注册时密码确认
@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·删除确认
@RequestMapping(value = "/init", method = RequestMethod.POST,params="yes") public String initYes(UserBean userBean,Model model) { int result1 = helloWorldService.deleteUser(userBean); Listresult = 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 result = helloWorldService.searchallUser(userBean); model.addAttribute("list", result); return "show"; }
5、注意SQL表达式的正确书写规则,如:
SELECT userid as userId,userPassword FROM user WHERE userid = #userId# and userPassword=#userPassword#
“where”条件并列需用“and”
6、int的比较用“=”,而string类型的比较则需要“a.equals(b)”的形式