Spring MVC可以处理来自POST和GET之外的HTML表单的请求吗? [英] Can Spring MVC handle requests from HTML forms other than POST and GET?

查看:132
本文介绍了Spring MVC可以处理来自POST和GET之外的HTML表单的请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring 3 MVC支持所有4种RESTful方法:GET,POST,PUT和DELETE.但是它的视图技术是否在表单上支持它们?如果没有,form:form标记中method属性的真正用途是什么?

Spring 3 MVC supports all 4 of RESTful methods: GET, POST, PUT and DELETE. But does its view technology support them on forms? If not, what is the real use of method attribute in form:form tag?

我尝试在表单上使用PUT方法:

I tried to use PUT method on the form:

<form:form action="/myaction" method="PUT">
   ...
</form:form>

生成的HTML是:

<form id="command" action="/myaction" method="post">
   <input type="hidden" name="_method" value="PUT"/>
   ...
</form>

很明显,因为大多数浏览器都不支持GET和POST .但是Spring可以使用名称为_method和值METHOD_NAME的其他input来处理它.可以?

It is clear since most browsers don't support other methods besides GET and POST. But Spring can handle it with additional input with name _method and value METHOD_NAME. Does it?

当我将指定的表单发送到带有

When I send specified form to a controller method annotated with

@RequestMapping(method=RequestMethod.PUT)

它声称不支持请求方法 POST .但是为什么是POST而不是PUT?幕后到底发生了什么?

it claims, that request method POST is not supported. But why POST and not PUT? What actually happens under the hoods?

推荐答案

使用名为_method的隐藏参数"并不特定于Spring MVC的标签库,但其他一些客户端框架也使用了它.春天就是遵循惯例,就像这样.

The use of the "hidden parameter" called _method is not specific to Spring MVC's tag library, but is also used by a few other client frameworks. Spring is just following the convention, such as it is.

为了正确使用此功能,您需要向web.xml添加过滤器(HiddenHttpMethodFilter,请参见PUT和DELETE是浏览器问题的事实-servlet API很好地支持了它.

In order to use this properly, you need to add a filter to your web.xml, (HiddenHttpMethodFilter, see javadoc), which turns the _method parameter into a "real" HTTP method representation in the HttpServletRequest. This is done as a filter to emphasise the fact the the lack of PUT and DELETE is a browser problem - the servlet API supports it just fine.

因此,如果要在表单中使用这些方法,则需要添加该过滤器.

So if you want to use these methods in your form, you need to add that filter.

P.S.收到不支持POST"消息的原因是您的表单使用POST,并且您的处理程序带有PUT注释,因此不匹配.由于未定义过滤器,因此_method参数将被忽略.

P.S. The reason you get the "POST not supported" message is that your form uses POST, and your handler is annotated with PUT, so it doesn't match. Because you don't have the filter defined, the _method parameter is ignored.

这篇关于Spring MVC可以处理来自POST和GET之外的HTML表单的请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆