SpringMVC 3 405 - 不支持请求方法“POST”

时间:2013-04-08 12:23:07

标签: spring-mvc sitemesh

控制器

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

问题

一旦设置&#34;方法&#34;属性为&#34; POST&#34;,当你点击提交按钮时,它总是变成405 - 请求方法&#39; POST&#39;不支持,如果从conotroller删除方法属性,并删除方法=&#34; post&#34;从HTML开始,它有效,任何人都知道如何解决这个问题?

更新

我想我发现了问题,这个问题是由sitemesh3造成的,在我从web.xml中删除了sitemesh3功能后,POST工作正常,但我不知道如何解决它。

2 个答案:

答案 0 :(得分:1)

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

最初我认为这与sitemesh有关。但是,当我在我的案例中更多地研究它时,那是因为我使用的是<mvc:resources /&gt;提供到装饰器的静态映射。

由于sitemesh试图使用Post请求访问它,因此<mvc:resources />不接受装饰器文件的发布请求。

我更改了装饰器文件的映射,以确保它是静态的并响应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>

答案 1 :(得分:0)

当你发现问题出在sitemesh时。 this link是一个将springMVC与sitemesh

集成在一起的项目
相关问题