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

后端 未结 1 602
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 14:27

I am using spring framework in my project,

Here is part of my web.xml:


    SpringMvcServlet
             


        
相关标签:
1条回答
  • 2021-01-21 14:41

    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.

    0 讨论(0)
提交回复
热议问题