2014-07-16 129 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

相关问题