2013-04-08 127 views
2

控制器3用SpringMVC 405 - 请求方法 'POST' 不支持

@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 - 不支持Request方法'POST',如果从conotroller删除方法属性,并删除方法=后“从HTML,以及它的工作原理,任何人都知道如何解决这个问题?

更新

我想我找到了问题,这个问题引起sitemesh3,之后我从web.xml中删除sitemesh3功能,开机自检工作正常,但我不知道如何解决它。

+0

所以它的工作与get方法,但不与POST方法? – 2013-04-08 16:43:10

+0

是的,它适用于get方法,但不适用于post方法 – Shore 2013-04-09 02:22:51

+1

web.xml中关于servlet url-pattern的代码是什么? – OQJF 2013-04-09 02:50:48

回答

0

那么你发现问题出在sitemesh上。 this link是一个项目,SiteMesh的

+0

你好,我没有找到你的链接的任何信息,请你请再次粘贴适当的链接给我?谢谢 – Shore 2013-04-15 13:59:54

+0

以及它是一个链接到一个真正的项目,它使用sitemesh和springMVC,我认为这将有助于检查出你的机器,你可以使用这个链接http://petclinicplus.googlecode.com/svn/因为你'不是这个项目的成员,那么你将不得不检查它为只读 – 2013-04-15 15:00:21

+0

是的,我审查了代码,我认为代码使用sitemesh2.3,但我使用sitemesh3,我不知道2.3是否有同样的问题,会做一个测试 – Shore 2013-04-15 15:58:49

0

用SpringMVC整合,我不知道这是有关您的设置,但我有同样的错误(不支持405-POST)

起初,我以为是的sitemesh相关。然而,当我在我的案例中进一步研究它时,这是因为我使用了<mvc:resources />来为装饰器提供静态映射。

它是<mvc:resources />,它不接受装饰文件的发布请求,因为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> 
相关问题