您的位置:首页 > 编程语言 > Java开发

Spring MVC使用RESTful风格的PUT请求

2015-08-05 18:56 375 查看
SpringMVC中使用RESTful风格的时候使用PUT请求遇到了错误。web.xml已经配置了如下代码解析POST请求。
<!-- 配置解析器,将POST 请求 解析成PUT或DELETE 请求 -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我的发送PUT请求页面部分代码为:
<form:form action="emp" method="post" modelAttribute="employee">
<c:if test="${employee.id == null }">
lastName:<form:input path="lastName"/><br>
</c:if>
<c:if test="${employee.id != null }">
<form:hidden path="id"/>
<span style="color:#ff0000;"><input type="hidden" name="_method" value="PUT"/>
</span>		</c:if>
email:<form:input path="email"/><br>
gender:<form:radiobuttons path="gender" items="${genders }" /><br>
department:<form:select path="department.id" items="${departments}"
itemLabel="department" itemValue="id"></form:select><br>
<input type="submit" value="submit"/>
</form:form>
在我的control控制器中也写好了接受PUT请求的处理代码
@RequestMapping(value="/emp",method=RequestMethod.PUT)
public String update(Employess employee){
/**处理逻辑代码**/
return "redirect:/emps";
}
这样子配置后运行时出现405错误,提示说不支持PUT请求方式,警告信息:
八月 05, 2015 6:38:54 下午 org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
警告: Request method 'PUT' not supported

查了很久,想不出是为什么,因为将POST请求解析成DELETE请求是没有问题的,但是PUT的时候就出现问题了,后来上网搜索各种资料找到两种解决办法。

1、将请求的应用路径名全部写上:即antion="/SpringMVC/emp"

2、在emp路径前加上${pageContext.request.contextPath }:即Action请求路径为${pageContext.request.contextPath }/emp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc spring