【问题标题】:SpringMVC 3 405 - Request method 'POST' not supportedSpringMVC 3 405 - 不支持请求方法“POST”
【发布时间】:2013-03-30 12:36:16
【问题描述】:

控制器

@Controller
public class Tester {
    @RequestMapping(value="testPost", method = RequestMethod.POST)
    public ModelAndView testPost(){
      ModelAndView _mv = new ModelAndView();
      _mv.setViewName("shared/post");
      return _mv;
    }
}

HTML

<form action="testPost" method="post"> 
    <input type="submit" value="Submit" />
</form>

Web.xml

<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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>iCubeHRS</display-name>

   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

   <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

   <filter>
        <filter-name>site_mesh</filter-name>
        <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>site_mesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

   <session-config>
        <session-timeout>20</session-timeout>
    </session-config>

</web-app>

问题

一旦将“method”属性设置为“POST”,当你点击提交按钮时,它总是变成405 - 不支持请求方法'POST',如果从控制器中删除方法属性,并从HTML中删除方法=“post”同样有效,有大神知道怎么解决吗?

更新

我想我找到了问题,这个问题是由sitemesh3引起的,在我从web.xml中删除sitemesh3特性后,POST工作正常,但我不知道如何解决。

【问题讨论】:

  • 所以它使用 get 方法而不是 post 方法?
  • 是的,它适用于 get 方法,但不适用于 post 方法
  • web.xml 中关于 servlet url-pattern 的代码是什么?
  • 你好,请看web.xml,我添加了sitemesh支持
  • 粘贴您在控制器类上的注释

标签: spring-mvc sitemesh


【解决方案1】:

我不确定这是否与您的设置有关,但我遇到了同样的错误(不支持 405-post)

最初我认为它与站点网格有关。但是,当我对它进行更多研究时,这是因为我使用 &lt;mvc:resources /> 来提供到装饰器的静态映射。

&lt;mvc:resources /&gt; 不接受装饰器文件的 post 请求,因为 sitemesh 试图使用 Post 请求访问它。

我更改了我的装饰器文件的映射以确保它是静态的并响应 POST 和 GET 请求。更多细节在这里Spring: not accept POST request under mvc:resources? how to fix that

我使用的代码是

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 <property name="urlMap">
     <map>
          <entry key="/DecTest/**" 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="/DecTest/" />
      <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>

【讨论】:

    【解决方案2】:

    好吧,您发现问题出在站点网格上。 this link是一个将springMVC与sitemesh集成的项目

    【讨论】:

    • 您好,我无法从您的链接中找到任何信息,请您再次粘贴正确的链接给我好吗?谢谢
    • 嗯,它是一个使用sitemesh和springMVC的真实项目的链接,我认为这将有助于在你的机器上检查它你可以使用这个链接petclinicplus.googlecode.com/svn,因为你不是该项目的成员,那么您必须将其检查为只读
    • 是的,我查看了代码,我认为代码使用的是sitemesh2.3,但我使用的是sitemesh3,我不知道2.3是否有同样的问题,会做一个测试跨度>
    猜你喜欢
    • 2013-03-03
    • 2014-06-10
    • 2018-04-13
    • 2017-07-01
    • 1970-01-01
    • 2012-04-20
    • 2020-07-24
    • 2017-05-03
    • 1970-01-01
    相关资源
    最近更新 更多