在Action控制器UserController.java中添加方法:
/**
* 修改用户前
*
* @param username
* @param model
* @return
*/
@RequestMapping(value = "/{username}/updateUserPro", method = RequestMethod.GET)
public String updateUserPro(@PathVariable String username, Model model) {
model.addAttribute(users.get(username));
return "user/updateUser";
}
/**
* 修改用户
*
* @param username
* @param user
* @return
*/
@RequestMapping(value = "/{username}/updateUser", method = RequestMethod.POST)
public String updateUser(@PathVariable String username, @Valid User user, BindingResult br) {
if (br.hasErrors()) {
return "user/updateUser";
}
users.put(username, user);
return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/userList";
}
/**
* 删除用户
*
* @param username
* @return
*/
@RequestMapping(value = "/{username}/deleteUser", method = RequestMethod.GET)
public String deleteUser(@PathVariable String username) {
users.remove(username);
return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/user/userList";
}
/**
* 展示用户详细信息
*
* @param username
* @param model
* @return
*/
@RequestMapping(value = "/{username}", method = RequestMethod.GET)
public String showUser(@PathVariable String username, Model model) {
model.addAttribute(users.get(username));
return "user/showUser";
}
这里,每个用户的信息包含在请求的url中(也就是所谓的rest风格)
修改用户列表页面/jsp/user/userList.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>用户列表</title>
</head>
<body>
登录用户名:${loginUser.username }<br>
<a href="addUserPro">添加用户</a>
<table border="1">
<tr>
<th>用户名</th>
<th>别名</th>
<th>密码</th>
<th>邮箱</th>
<th>操作</th>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td><a href="${user.key }">${user.value.username }</a></td>
<td>${user.value.nickname }</td>
<td>${user.value.password }</td>
<td>${user.value.email }</td>
<td><a href="${user.key }/updateUserPro">更新</a> <a href="${user.key }/deleteUser">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
添加用户新增页面/jsp/user/updateUser.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>修改用户</title>
</head>
<body>
<sf:form method="post" modelAttribute="user" action="updateUser">
用户名*:<sf:input path="username"/><sf:errors path="username" cssStyle="color:red"/><br>
别名:<sf:input path="nickname"/><br>
密码*:<sf:password path="password"/><sf:errors path="password" cssStyle="color:red"/><br>
邮箱*:<sf:input path="email"/><sf:errors path="email" cssStyle="color:red"/><br>
<input type="submit" value="修改"/>
</sf:form>
</body>
</html>
添加展示用户信息页面/jsp/user/showUser.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>展示用户信息</title>
</head>
<body>
<form>
用户名:${user.username },别名:${user.nickname },密码:${user.password },邮箱:${user.email }
</form>
</body>
</html>
在浏览器中输入:
http://127.0.0.1:9900/springmvc_005_rest_04/user/