8

使用 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 天了,并且正在转圈。

4

3 回答 3

8
@RequestMapping(value = "/members/edit/{id}", method = RequestMethod.POST)
public String updateMember(@PathVariable int id, 
    HttpServletRequest request,@ModelAttribute("member") Member member) {

//希望您的 MemberService 看起来相似

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);

}

于 2013-03-09T18:03:13.667 回答
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。只需将此实例保存在数据库中。

于 2013-03-09T17:03:30.253 回答
1

这可能与 form 属性有关:
enctype="multipart/form-data"

在 Spring 中未解决多个部分时没有错误日志(这非常棘手)。
Spring 无法读取 POST 请求中的多个附件部分,并且 Spring 不会更新模型。

请确保您已在调度程序 servlet XML 文件中设置了多部分解析器。
调度程序-servlet.xml:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

并将以下依赖项添加到您的 pom.xml 中。
pom.xml:

<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.2</version>
</dependency>
于 2016-10-12T13:41:57.150 回答