【发布时间】:2015-02-20 09:37:07
【问题描述】:
我已经开始通过开发一个简单的 SPRING MVC 应用程序来学习 Spring MVC。
我创建了一个用于列出的 JSP 文件 - 像这样编辑用户列表:
当我单击“编辑”链接(来自列表项)时,表单应由项目信息填充,因此用户可以编辑信息并保存项目(请注意,此表单用于创建/编辑项目)
这是与JSP文件相关的代码:
<c:url var="addAction" value="/admin/users/add"></c:url>
<form:form action="${addAction}" commandName="user">
<table>
<c:if test="${!empty user.adminName}">
<tr>
<td><form:label path="id">
<spring:message text="ID" />
</form:label></td>
<td><form:input path="id" readonly="true" size="8"
disabled="true" /> <form:hidden path="id" /></td>
</tr>
</c:if>
<tr>
<td><form:label path="adminName">
<spring:message text="Name" />
</form:label></td>
<td><form:input path="adminName" /></td>
</tr>
<tr>
<td><form:label path="adminEmail">
<spring:message text="adminEmail" />
</form:label></td>
<td><form:input path="adminEmail" /></td>
</tr>
<tr>
<td><form:select path="groups" items="${groupList}" /></td>
</tr>
<tr>
<td colspan="2"><c:if test="${!empty user.adminName}">
<input type="submit" value="<spring:message text="Edit Person"/>" />
</c:if> <c:if test="${empty user.adminName}">
<input type="submit" value="<spring:message text="Add Person"/>" />
</c:if></td>
</tr>
</table>
</form:form>
<br>
<div class="bs-example">
<table class="table">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<c:forEach var="i" items="${users}">
<tr>
<td>${i.id}</td>
<td>${i.firstName}</td>
<td>${i.adminEmail}</td>
<td><a href="<c:url value='/admin/users/edit/${i.id}'/>">Edit</a></td>
<td><a href="<c:url value='/admin/users/remove/${i.id}'/>">Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
我的控制器方法是:
@Controller
@RequestMapping(value = "/admin/users/")
public class UserController {
private static final Logger LOGGER = LoggerFactory
.getLogger(UserController.class);
@Autowired
UserService userService;
@Autowired
GroupService groupService;
@Autowired
LabelUtils messages;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
// @PreAuthorize("hasRole('STORE_ADMIN')")
@RequestMapping(value = "list.html", method = RequestMethod.GET)
public String displayUsers(Model model) throws Exception {
List<User> users = userService.listUser();
List<Group> groups = groupService.list();
List<String> groupList = new ArrayList<String>();
model.addAttribute("users", users);
model.addAttribute("user", new User());
for (Group group : groups) {
groupList.add(group.getGroupName());
}
model.addAttribute("groupList", groupList);
return "admin/userAdmin";
}
@RequestMapping("remove/{id}")
public String removePerson(@PathVariable("id") int id) {
User user = userService.getById((long) id);
try {
userService.delete(user);
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "redirect:/admin/users/list.html";
}
// For add and update person both
@RequestMapping(value = "add" , method = RequestMethod.POST)
public String addPerson(@ModelAttribute("user") User user,
BindingResult result) {
if (result.hasErrors()) {
return "error";
}
try {
if (user.getId() == 0) {
// new person, add it
this.userService.create(user);
} else {
// existing person, call update
this.userService.saveOrUpdate(user);
}
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "redirect:/admin/users/list.html";
}
@RequestMapping(value = "edit/{id}" )
public String editPerson(@PathVariable("id") int id , Model model) {
// Prepare Groups
List<Group> groups = groupService.list();
List<String> groupList = new ArrayList<String>();
for (Group group : groups) {
groupList.add(group.getGroupName());
}
model.addAttribute("groupList", groupList);
model.addAttribute("user", this.userService.getById((long) id));
model.addAttribute("users", this.userService.list());
return "admin/userAdmin";
}
}
问题是当我单击编辑链接时,表单由项目信息正确填充,因此用户可以编辑检索到的数据,但对象“用户”是从控制器中“addPerson”方法中的“editPerson”方法检索到的(
代码行:model.addAttribute("user", this.userService.getById((long) id));在 editPerson 方法中) 让所有其他字段为空,所以当我合并数据时,保存操作失败。
示例:用户项有另一个字段“AdminAPassword”未在 JSP 中打印并且未更改 bu 用户,当从表单中检索数据时该字段为空。
你能帮忙吗
提前致谢
【问题讨论】:
-
从 Get 到 Post 方法传递对象的方式是正确的根据 MVC spring 规则。谢谢
标签: spring spring-mvc