获取405错误:Spring MVC JDBCTemplate CRUD操作中不支持请求方法'POST'
。
如何解决代码段中不支持的405'POST'方法 使用Spring MVC和JDBCTemplate进行crud操作?
访问DELETE语句的URL:/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;
}
现在不确定传递URL时调用的JSP: 的List.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 :(得分:0)
控制器仅识别GET为/ delete / {id}:
@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
public ModelAndView deleteUser(@PathVariable ("id") Integer id) {
在list.jsp上你有一个发送POST到/ delete / {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上的method = RequestMethod.GET
删除@RequestMapping
将允许它处理所有HTTP方法,而不仅仅是GET。