Felix Chang
Felix Chang

Reputation: 25

Spring: not accept POST request under mvc:resources? how to fix that

I am using spring framework in my project,

Here is part of my web.xml:

<servlet>
    <servlet-name>SpringMvcServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>SpringMvcServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
<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>

<error-page>
    <error-code>404</error-code>
    <location>/system/404.html</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/system/500.html</location>
</error-page>

And configure:

<mvc:resources mapping="/system/**" location="/WEB-INF/pages/system/" />

But I find so many error in my log, some request like this:

They were not exists in my server, so will call "/system/404.html", but the mvc:resources don't accept POST method, so it will return 500 Error.

How to fix that? or work around?

Thanks

Upvotes: 2

Views: 6172

Answers (1)

Ralph
Ralph

Reputation: 120791

First of all: I think you abuse the ResourceHttpRequestHandler when you try to use it for POST requests. -- And I am not sure that every thing works correct if you made this handler to handle POST requests.


<mvc:resources /> configure an instance of class org.springframework.web.servlet.resource.ResourceHttpRequestHandler. This has the super class WebContentGenerator and this super class has a property Set<String> supportedMethods.

So all what you need to do is:

<property name="supportedMethods">
    <list>
       <value>GET</value>
       <value>HEAD</value>
       <value>POST</value>
    </list>
</property>

Unfortunately this requires that you configure the ResourceHttpRequestHandler by hand instead of using <mvc:resources />

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="urlMap">
         <map>
              <entry key="/system/**" value="myResourceHandler" />
         </map>
     </property>
     <property name="order" value="100000" />       
</bean>

<bean id="myResourceHandler" name="myResourceHandler"
      class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
      <property name="locations" value="/WEB-INF/pages/system/" />
      <property name="supportedMethods">
         <list>
            <value>GET</value>
            <value>HEAD</value>
            <value>POST</value>
         </list>
     </property>
     <!-- cacheSeconds: maybe you should set it to zero because of the posts-->
</bean>

I have not proved this configuration, I have just written it down from what the ResourceBeanDefintionParser does.

Upvotes: 2

Related Questions