使用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>
因为我的Controller中的'member.setForename(“test forename”)'代码设置了测试字符串,我想我可以使用从表单中发布的参数来设置atrributes,如下所示:
'member.setSurname(用request.getParameter( “姓”))'
但是这会导致HTTP 405错误。使用Firebug,我已经确认'编辑成员'表单正在发布'forename','surname','address1'等参数,所以我无法弄清楚为什么我不能将它们从请求中删除。< / p>
任何帮助都会非常感激,因为我已经在这里待了2天并且正在围成一圈。
答案 0 :(得分:8)
@RequestMapping(value = "/members/edit/{id}", method = RequestMethod.POST)
public String updateMember(@PathVariable int id,
HttpServletRequest request,@ModelAttribute("member") Member member) {
//希望你的会员服务看起来很相似
public void edit(Member member) {
logger.debug("Editing existing member");
// Retrieve session from Hibernate, if you are using hibernate
Session session = sessionFactory.getCurrentSession();
// Retrieve existing member via id
Member existingMember = (Member) session.get(Member.class, member.getId());
// Assign updated values to this member
existingMember.setForeName(member.getForeName());
existingMember.setSurName(member.getSurName());
...
...
existingMember.setPostcode(member.getPostcode());
// Save updates
session.save(existingMember);
}
答案 1 :(得分:1)
@RequestMapping(value = "/members/edit/{id}", method = RequestMethod.POST)
public String updateMember(@PathVariable int id,
HttpServletRequest request,@ModelAttribute("member") Member member) {
在@ModelAttribute
之前添加Member
。
然后这个实例将进行所有编辑。因此您无需按request.getParameter
设置新值。只需将此实例保存在DB中。
答案 2 :(得分:1)
这可能与表单属性enctype="multipart/form-data"
有关
在Spring中没有解决多个部分时没有错误日志(这非常棘手)。
Spring在POST请求中无法读取多个附件部分,而Spring不会更新模型。
请确保已在调度程序servlet XML文件中设置了多部分解析程序。
调度员servlet.xml中:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
并在下面添加以下依赖关系
pom.xml中:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>