2017-08-14 29 views
0

你好我收到以下錯誤Spring MVC中PUT例如org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpRequestMethodNotSupported

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

with following 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; 
    } 

//干將setter方法ommitted }

回答

1

你放測繪預計年底@PutMapping(path = "/{id}/edit") '編輯',但你的表單的action <form:form action="../${user.id}"沒有編輯,因此控制器無法攔截該操作。

+0

感謝將其更改爲'../$ {user.id} /編輯'工作正常,但現在我得到所有空值。你也可以幫我解決這個問題 – user7036414

+0

請用用戶代碼 – StanislavL

+0

發表用戶代碼,你的意思是'用戶域代碼?' – user7036414

相關問題