Spring REST没有创建_method隐藏元素

时间:2013-08-19 02:22:34

标签: spring-mvc

我曾经让它工作正常,不知何故经过一些开发我意识到HTML元素_method不是自动在页面上创建的,因此REST不起作用。

下面是我的web.xml,其中包含HiddenHttpMethodFilter

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
             /WEB-INF/spring/root-context.xml,
             /WEB-INF/spring/appServlet/*-context.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>trimSpaces</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter>
        <filter-name>HttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <error-page>
        <error-code>403</error-code>
        <location>/errors/403.html</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/errors/404.html</location>
    </error-page>
    <error-page>
        <error-code>405</error-code>
        <location>/errors/405.html</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/errors/500.html</location>
    </error-page>
</web-app>

我尝试了什么

  • Maven更新项目
  • Maven Clean&gt; Maven安装
  • 清除工作目录

已更新

我的jsp部分

<html:form modelAttribute="user" id="user-form" formUrl="/user">

form.tag将其转换为Spring表单

<form:form modelAttribute="${modelAttribute}" id="${id}"
    action="${processedFormUrl}" class="form-horizontal" 
            target="${target}" method="post">
    <fieldset>
       <jsp:doBody />
    </fieldset>
</form:form>

1 个答案:

答案 0 :(得分:0)

只有当你的表单方法不是post,即它的PUT / DELETE时,才需要HiddenHttpMethodFilter,在这种情况下,spring会自动使用正确的表单方法添加_method隐藏字段。

如果你想让一个看跌请求改变method="put"并且spring-form将会处理休息

<强> EDITTED 我看了一下Spring FormTag的源代码,我发现了下面的

 if (!isMethodBrowserSupported(getMethod())) {
                assertHttpMethod(getMethod());
                String inputName = getMethodParameter();
                String inputType = "hidden";
                tagWriter.startTag(INPUT_TAG);
                writeOptionalAttribute(tagWriter, TYPE_ATTRIBUTE, inputType);
                writeOptionalAttribute(tagWriter, NAME_ATTRIBUTE, inputName);
                writeOptionalAttribute(tagWriter, VALUE_ATTRIBUTE, processFieldValue(inputName, getMethod(), inputType));
                tagWriter.endTag();
}


    protected boolean isMethodBrowserSupported(String method) {
            return ("get".equalsIgnoreCase(method) || "post".equalsIgnoreCase(method));
        }

因此,只有当您的表单方法没有获取/发布时,才会添加_method隐藏字段。

对于您的情况,当单击删除按钮时,您可以在子命令之前使用jquery为_method字段添加输入隐藏标记,

var input = $("<input>").attr("type", "hidden").attr("name", "_method").val("DELETE");
            $('form#yoorFormID').append(input).submit();