使用 Spring MVC 3.0,我正在开发一个具有基本 CRUD 操作的成员管理系统,但我的控制器遇到了问题。
我无法使用 Controller 方法更新成员记录。我有显示和处理我的“添加成员”表单的方法和另一个显示我的“编辑成员”表单的方法,它们都工作正常。
这是我的控制器:
@Controller
public class MemberController {
private MemberService memberService;
@Inject
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
// Show member list
@RequestMapping(value = "/members/list", method = RequestMethod.GET)
public String showMemberList(Map<String, Object> model) {
model.put("members", memberService.getAllMembers());
return "members/list";
}
// Display add member form
@RequestMapping(value = "/members/add", method = RequestMethod.GET)
public String showAddMemberForm(Model model) {
model.addAttribute(new Member());
return "members/add";
}
// Process add member form
@RequestMapping(value = "/members/add", method = RequestMethod.POST)
public String addMember(Member member) {
memberService.addMember(member);
return "redirect:list";
}
// Display edit member form
@RequestMapping(value = "/members/edit/{id}", method = RequestMethod.GET)
public String showEditMemberForm(Model model, @PathVariable int id) {
String idAsString = Integer.toString(id);
model.addAttribute("member", memberService.getMemberById(idAsString));
return "members/edit/{id}";
}
// Process edit member form
@RequestMapping(value = "/members/edit/{id}", method = RequestMethod.POST)
public String updateMember(@PathVariable int id,
HttpServletRequest request, Member member) {
// Uncommenting the next line of code sets the member objects forename attribute
// to "test forename"
// member.setForename("test forename");
// Uncommenting the next line of code gives the following error:
// HTTP Status 405 - Request method 'POST' not supported
//member.setSurname(request.getParameter("surname"));
memberService.updateMember(member);
return "redirect:/members/list";
}
}
这是我的更新方法:
public void update(Member member) {
jdbcTemplate.update(
"UPDATE members SET forename=?, surname=?, address1=?, address1=?, "
+ "city=?, postcode=? WHERE memberId=?",
member.getForename(), member.getSurname(),
member.getAddress1(), member.getAddress2(), member.getCity(),
member.getPostcode(), member.getId());
}
这是我的编辑成员 JSP 表单:
<sf:form method="POST" modelAttribute="member"
enctype="multipart/form-data" action="/members/edit/${member.id}">
<fieldset>
<table cellspacing="0">
<tr>
<th><label for="member_id">Id:</label></th>
<td><sf:input path="id" size="15" id="member_id" /></td>
</tr>
<tr>
<th><label for="member_forename">Forename:</label></th>
<td><sf:input path="forename" size="15" id="member_forename" /></td>
</tr>
<tr>
<th><label for="member_surname">Surname:</label></th>
<td><sf:input path="surname" size="15" id="member_surname" /></td>
</tr>
<tr>
<th><label for="member_address1">Address 1:</label></th>
<td><sf:input path="address1" size="15" id="member_address1" />
</td>
</tr>
<tr>
<th><label for="member_address2">Address 2:</label></th>
<td><sf:input path="address2" size="15" id="member_address2" />
</td>
</tr>
<tr>
<th><label for="member_city">City:</label></th>
<td><sf:input path="city" size="15" id="member_city" /></td>
</tr>
<tr>
<th><label for="member_postcode">Postcode:</label></th>
<td><sf:input path="postcode" size="15" id="member_postcode" />
</td>
</tr>
</table>
<input type="submit" value="Update" />
</fieldset>
</sf:form>
因为我的控制器中的 'member.setForename("test forename")' 代码设置了测试字符串,所以我想我可以使用从表单中发布的参数来设置属性,如下所示:
'member.setSurname(request.getParameter("surname"))'
但这会导致 HTTP 405 错误。使用 Firebug,我已经确认“编辑成员”表单正在发布“名字”、“姓氏”、“地址 1”等参数,所以我无法弄清楚为什么我不能将它们从请求中删除。
任何帮助将不胜感激,因为我已经在这 2 天了,并且正在转圈。