2014-07-16 37 views
1

獲取405錯誤:請求方法'POST'在Spring MVC JDBCTemplate CRUD操作中不受支持。請求的方法POST不受支持,Spring MVC JDBCTemplate

How do I resolve 405 'POST' method not supported in the piece of code using Spring MVC and JDBCTemplate for crud operation?

URL訪問DELETE語句:/JdbcTemplate/user/delete/9 這裏的方法是在控制類:

@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) 
    public ModelAndView editUser(@PathVariable("id") Integer id, ModelMap model) { 

     ModelAndView mav = new ModelAndView(USER_EDIT_PAGE); 
     User user = userService.getUserById(id); 
//  User user = new User(); 
     model.addAttribute("user", user); 
     model.addAttribute("id", id); 
     return mav; 

    } 

    @RequestMapping(value = "/update", method = RequestMethod.POST) 
    public String updateUser(@ModelAttribute("user") User user) { 
     userService.updateUser(user); 
     return "redirect:/user/list"; 
    } 

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) 
    public ModelAndView deleteUser(@PathVariable ("id") Integer id) { 

     ModelAndView mav = new ModelAndView(USER_LIST_VIEW); 

     userService.deleteUser(id); 

     String message = "Team was successfully deleted."; 
     mav.addObject("message", message); 

     return mav; 
    } 

現在不知道的JSP當URL傳遞正被稱爲: 名單。 JSP

<%@ include file="/WEB-INF/views/includes/taglibs.jsp"%> 

<!DOCTYPE html> 
<html> 
<head> 
<c:import url="/WEB-INF/views/includes/meta.jsp" /> 
</head> 
<body> 
    <div class="container"> 
     <h2>User List</h2> 
     <table border="1" class="table table-bordered table-striped"> 
      <tr> 
       <td>Id</td> 
       <td>Name</td> 
       <td>City</td> 
       <td>Zip</td> 
       <td>Country</td> 
       <!-- <td>Edit</td> --> 
       <td>Delete</td> 
      </tr> 
      <tbody> 
       <c:forEach var="user" items="${users}"> 
        <tr> 
         <td>${user.id}</td> 
         <td><%-- <a href="/user/edit/${user.id}"> --%>${user.lastName},${user.firstName}<!-- </a> --></td> 
         <td>${user.city}</td> 
         <td>${user.zip}</td> 
         <td>${user.country}</td> 
         <td><form:form 
           action="${pageContext.request.contextPath}/user/delete/${user.id}" 
           method="POST"> 
           <input type="submit" class="btn btn-danger btn-sm" 
            value="Delete" /> 
          </form:form></td> 
         <%-- <td><a href="edit/${user.id}">Edit</a></td> 
         <td><a href="delete?id=${user.id}">Delete</a></td> --%> 
        </tr> 
       </c:forEach> 
      </tbody> 
      <tr> 

      </tr> 
     </table> 
    </div> 
    </div> 
    <td colspan="7"><a href="register">Add New User</a></td> 
    </center> 
</body> 
</html> 

edit.jsp文件

<%@ include file="/WEB-INF/views/includes/taglibs.jsp" %> 

<!doctype html> 
<html> 
<head> 
    <c:import url="/WEB-INF/views/includes/meta.jsp" /> 
</head> 

<body> 
<div class="container"> 

    <c:if test="${empty user}"> 
     <h3>User not found: ${id}</h3> 
    </c:if> 
    <c:if test="${not empty user}"> 
     <h1>Edit User</h1> 
     <form:form method="POST" action="update" commandName="user"> 
      <form:hidden path="id" /> 
      <div class="form-group"> 
       <form:label path="firstName">First Name:</form:label> 
       <form:input path="firstName" class="form-control" placeholder="First Name"/> 
      </div> 
      <div class="form-group"> 
       <form:label path="lastName">Last Name:</form:label> 
       <form:input path="lastName" class="form-control" placeholder="Last Name"/> 
      </div> 
      <div class="form-group"> 
       <form:label path="city">City:</form:label> 
       <form:input path="city" class="form-control" placeholder="City"/> 
      </div> 
      <div class="form-group"> 
       <form:label path="zip">Zip:</form:label> 
       <form:input path="zip" class="form-control" placeholder="Zip"/> 
      </div> 
      <div class="form-group"> 
       <form:label path="country">Country:</form:label> 
       <form:input path="country" class="form-control" placeholder="Country"/> 
      </div> 
      <button type="submit" class="btn btn-default">Save</button> 
     </form:form> 
    </c:if> 

    <br><a href="/" class="btn btn-primary">Back</a> 

</div> 
</body> 
</html> 

感謝您的關注。

回答

0

控制器只能識別GET來/刪除/(編號):

@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET) 
public ModelAndView deleteUser(@PathVariable ("id") Integer id) { 

在你的List.jsp有發送POST到/刪除/ {ID}形式,因此,這似乎是在問題:

<form:form action="${pageContext.request.contextPath}/user/delete/${user.id}" method="POST"> 
    <input type="submit" class="btn btn-danger btn-sm" value="Delete" /> 
</form:form> 

將窗體上的方法從POST更改爲GET,並且應該可以解決您的問題。如果您希望允許GET和POST到/ delete/{id},那麼在deleteUser上從@RequestMapping刪除method = RequestMethod.GET將允許它處理所有HTTP方法,而不僅僅是GET。

+0

當我在deleteUser()方法中從'/ delete/{id}'中移除'method = RequestMethod.GET'時,沒有任何操作或發送的請求不會刪除表中的記錄。 – Thunder

相關問題