更新用户信息,list页中单击name连接,但仍然以ID做为条件传值,并且更新时,对输入的内容须要进行验证,验证时要导入包validation-api-1.1.0.Final.jar,hibernate-validator-5.1.3.Final.jarapi
<td><a th:href="@{update(userID=${user.userID})}"><span th:text="${user.username}"></span></a></td>app
在Oontroller中spa
@RequestMapping(value = "/update", method = RequestMethod.GET)
public String update(UserBean UserBean,Model model) {
UserBean result=helloWorldService.sUser(UserBean);
model.addAttribute("UserBean",result);
return "update";
}
@RequestMapping(value = "/tupdate", method = RequestMethod.POST)
public String Login(HttpServletRequest req,Model model,@Valid @ModelAttribute("UserBean") UserBean UserBean,BindingResult results) throws SQLException {
if (results.hasErrors()) {
log.info("内容验证失败");
model.addAttribute("xinxi", "用户名或密码不能为空");
UserBean result=helloWorldService.sUser(UserBean);
model.addAttribute("UserBean",result);
return "update";
}
if (!UserBean.getPassword().equals(UserBean.getRepassword())) {
log.info("内容验证失败");
model.addAttribute("xinxi", "两次输入密码不一致");
UserBean result=helloWorldService.sUser(UserBean);
model.addAttribute("UserBean",result);
return "update";
}
if(helloWorldService.updateUser(UserBean)!=0){
List<UserBean> result=helloWorldService.listUser(UserBean);
model.addAttribute("list",result);
return "list";
}
else
return "update";
}hibernate
SQL文get
<update id="updatename"
parameterClass="cn.training.controller.UserBean">
UPDATE user SET username=#username#,password=#password# WHERE UserID = #userID#
</update>
<select id="sUser"
parameterClass="cn.training.controller.UserBean"
resultClass="cn.training.controller.UserBean">
SELECT UserID as userID,username,password FROM user WHERE UserID = #userID#
</select>io