2013-04-08 125 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> 
相關問題