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

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

嗨,我在 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 }

您的放置映射期望 'edit' 最后 @PutMapping(path = "/{id}/edit") 但您的表单的操作 <form:form action="../${user.id}" 没有编辑,因此控制器无法拦截操作。