我对某个问题感到有点困惑,我在更新用户对象时反复遇到405 Method Not Allowed的问题。但是,控制器代码和HTML表单似乎是正确的。如果我将RequestMethod.PUT更改为RequestMethod.POST它可以正常工作。我在这里错过了什么吗?
我使用以下网址方案
/user [GET] - user list
/user/new [GET] - form to create a new user
/user [POST] - create user
/user/{id}/edit [GET] - form to edit user
/user/{id} [PUT] - update the user
/user/{id} [DELETE] - delete the user
从HTTPFox
捕获的HTTP流量请求标题
(Request-Line) POST /searchtool/user/12 HTTP/1.1
Host localhost:8080
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-US,en;q=0.5
Accept-Encoding gzip, deflate
Referer http://localhost:8080/searchtool/user/12/edit
Cookie JSESSIONID=685471429B1BC65305E2B0390F91CBC1
Connection keep-alive
Cache-Control max-age=0
Content-Type application/x-www-form-urlencoded
Content-Length 128
响应标题
(Status-Line) HTTP/1.1 405 Method Not Allowed
Server Apache-Coyote/1.1
Allow DELETE, PUT
Content-Type text/html;charset=ISO-8859-1
Content-Language en-US
Transfer-Encoding chunked
Date Tue, 27 May 2014 16:01:01 GMT
POST数据
_method PUT
id 12
firstName User
lastName Test
email test@test.com
userName test1
password
password-confirm
role 2
Controller Snippet
@Secured(value={"ROLE_ADMIN"})
@Transactional
@RequestMapping(value="/user/{id}", method=RequestMethod.PUT)
public String userUpdate(User user, BindingResult userBinding, Model model, @PathVariable long id) {
if (userBinding.hasErrors()) {
return (String.format("user/{%s}/edit", String.valueOf(user.getId())));
}
try {
userService.updateUser(user);
}
catch (Exception e) {
logger.error("Unable to update user", e);
}
return "redirect:/user";
}
JSP
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<div class="container">
<div class="row">
<div class="col-xs-12">
<spring:bind path="user.*">
<c:if test="${status.errors.errorCount > 0}">
<div class="alert alert-danger">
<p>Error: Unable to create user, please see the highlighted fields below.</p>
</div>
</c:if>
</spring:bind>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-8 col-md-6">
<h3>User - Add</h3>
<br>
<c:choose>
<c:when test="${user.id > 0}">
<c:set var="formMethod" value="PUT" />
<c:url var="formAction" value="/user/${user.id}" />
</c:when>
<c:otherwise>
<c:set var="formMethod" value="POST" />
<c:url var="formAction" value="/user" />
</c:otherwise>
</c:choose>
<sf:form method="${formMethod}" action="${formAction}" commandName="user">
<sf:hidden path="id" id="user-id" />
<spring:bind path="user.firstName">
<div class="form-group">
<label for="first-name">First Name</label>
<sf:input path="${status.expression}" id="first-name" class="form-control" placeholder="First Name" />
<sf:errors path="${status.expression}" cssClass="error-message"></sf:errors>
</div>
</spring:bind>
<spring:bind path="user.lastName">
<div class="form-group">
<label for="last-name">Last Name</label>
<sf:input path="${status.expression}" id="last-name" class="form-control" placeholder="Last Name" />
<sf:errors path="${status.expression}" cssClass="error-message"></sf:errors>
</div>
</spring:bind>
<spring:bind path="user.email">
<div class="form-group">
<label for="email">Email</label>
<sf:input path="${status.expression}" id="email" class="form-control" placeholder="Email" />
<sf:errors path="${status.expression}" cssClass="error-message"></sf:errors>
</div>
</spring:bind>
<spring:bind path="user.userName">
<div class="form-group">
<label for="user-name">Username</label>
<sf:input path="${status.expression}" id="user-name" class="form-control" placeholder="Username" />
<sf:errors path="${status.expression}" cssClass="error-message"></sf:errors>
</div>
</spring:bind>
<spring:bind path="user.password">
<div class="form-group">
<label for="password">Password</label>
<sf:password path="${status.expression}" id="password" class="form-control" placeholder="" />
<sf:errors path="${status.expression}" cssClass="error-message"></sf:errors>
</div>
</spring:bind>
<div class="form-group">
<label for="confirm-password">Confirm Password</label>
<input type="password" name="password-confirm" id="confirm-password" class="form-control" placeholder="" />
</div>
<spring:bind path="user.role">
<div class="form-group">
<label for="role">Role</label>
<sf:select path="${status.expression}" id="role" class="form-control" >
<sf:options items="${roles}" itemLabel="friendlyName" itemValue="id"/>
</sf:select>
<sf:errors path="${status.expression}" cssClass="error-message"></sf:errors>
</div>
</spring:bind>
<button type="submit" class="btn btn-default">Save</button>
<button type="button" class="btn btn-default">Cancel</button>
</sf:form>
</div>
</div>
答案 0 :(得分:2)
您必须在web.xml中使用HiddenHttpMethodFilter,请参阅下文。
<filter>
<filter-name>hiddenmethodfilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenmethodfilter</filter-name>
<servlet-name>dispatchers-name</servlet-name>
</filter-mapping>
在你的html / jsp中使用Spring tld表单标签,并在表单中包含 _method = PUT 。