春季:不接受mvc:resources下的POST请求吗?如何解决 [英] Spring: not accept POST request under mvc:resources? how to fix that

查看:78
本文介绍了春季:不接受mvc:resources下的POST请求吗?如何解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用spring框架,

I am using spring framework in my project,

这是我的web.xml的一部分:

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>

并配置:

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

但是我发现我的日志中有很多错误,有些请求是这样的:

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

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

它们在我的服务器中不存在,因此将调用"/system/404.html",但是mvc:resources不接受POST方法,因此它将返回500错误.

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.

该如何解决?或解决?

谢谢

推荐答案

首先:我想尝试将其用于POST请求时会滥用ResourceHttpRequestHandler.如果您创建了该处理程序来处理POST请求,则不能确定所有事情都能正常工作.

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 />配置类org.springframework.web.servlet.resource.ResourceHttpRequestHandler的实例.该类具有父类WebContentGenerator,并且该父类具有属性Set<String> supportedMethods.

<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.

所有您需要做的是:

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

不幸的是,这要求您手动配置ResourceHttpRequestHandler而不是使用<mvc:resources />

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>

我还没有证明这种配置,只是从ResourceBeanDefintionParser所做的工作中写下来.

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

这篇关于春季:不接受mvc:resources下的POST请求吗?如何解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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