2013-03-23 68 views
2

我使用Spring框架在我的项目,Spring:不接受mvc下的POST请求:资源?如何解决

这里是我的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> 

和配置:

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

不过,我觉得这么多错误在我的日志中,一些这样的请求:

  • POST /index.php
  • POST /notexists.html

他们不是存在于我的服务器,所以会叫“/system/404.html”,但MVC:资源不接受POST方法,所以它会返回500错误。

如何解决这个问题?或者变通?

感谢

回答

1

首先:我觉得你滥用ResourceHttpRequestHandler当您尝试使用POST请求。 - 我不确定如果你让这个处理程序处理POST请求,所有的东西都能正常工作。


<mvc:resources />配置org.springframework.web.servlet.resource.ResourceHttpRequestHandler类的一个实例。这有超级类WebContentGenerator和这个超级类有一个属性Set<String> supportedMethods

因此,所有你需要做的是:

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

不幸的是这需要你手工配置ResourceHttpRequestHandler,而不是使用<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> 

我还没有证明这种配置中,我刚刚从ResourceBeanDefintionParser做的事情中写下来。

+0

谢谢,我会稍后再试。我不是使用“POST”,而是一些scaner或机器人POST这些请求,我只是认为返回正常404更好,不会返回500错误。 – 2013-03-23 16:44:43

+0

太棒了,它的作品! – 2013-03-23 16:57:39