org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported

时间:2017-08-14 07:04:20

标签: spring spring-mvc http-put

您好我在Spring MVC PUT示例

中遇到以下错误
Aug 14, 2017 12:28:57 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported
WARNING: Request method 'PUT' not supported

在以下表格

<form:form action="../${user.id}" method="PUT" commandName="user">
                    <div class="form-group">
                        <label for="txtUserName">User-name</label>
                        <form:input path="userName" class="form-control" id="txtUserName"
                            placeholder="User Name" />
                    </div>
                    <div class="form-group">
                        <label for="txtName">First Name</label>
                        <form:input path="name" class="form-control" id="txtName"
                            placeholder="Full Name" />
                    </div>
                    <div class="form-group">
                        <label for="calDob">Date of Birth</label>
                        <form:input path="dateOfBirth" class="form-control" id="calDob"
                            placeholder="dd/MM/yyyy" />
                    </div>
                    <input type="hidden" name="_method" value="PUT">

                    <input type="submit" class="btn btn-success" value="SAVE">


                </form:form>

带控制器

@GetMapping(path = "/{id}/edit")
    public String editUser(@PathVariable(value = "id") Long id, Model model) {
        model.addAttribute("user", userService.findById(id));
        return "user/edit";
    }

    @PutMapping(path = "/{id}/edit")
    public String updateUser(@PathVariable(value = "id") long id, @ModelAttribute("user") User user, Model model) {
        userService.updateUser(user);
        model.addAttribute("user", userService.findById(id));
        LOG.info("" + user.toString());
        return "redirect:/users/" + id;
    }

使用以下web.xml

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>httpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>

    <filter-mapping>
        <filter-name>httpMethodFilter</filter-name>
        <servlet-name>appServlet</servlet-name>
    </filter-mapping>
    <filter>
        <filter-name>httpPutFormFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>httpPutFormFilter</filter-name>
        <servlet-name>appServlet</servlet-name>
    </filter-mapping>
你可以指导我如何解决这个错误。

如果您需要更多信息,请与我们联系。

谢谢:)

编辑 User.java

public class User {

    private Long id;
    private String name;
    private String userName;
    private String password;
    private Date dateOfBirth;


    public User() {
    }

    public User(Long id, String name, String userName, String password, Date dateOfBirth) {
        super();
        this.id = id;
        this.name = name;
        this.userName = userName;
        this.password = password;
        this.dateOfBirth = dateOfBirth;
    }

//省略了getters setters }

1 个答案:

答案 0 :(得分:1)

Your put mapping expects 'edit' in the end @PutMapping(path = "/{id}/edit") but your form's action <form:form action="../${user.id}" does not have the edit so the controller can't intercept the action.