Shore
Shore

Reputation: 123

SpringMVC 3 405 - Request method 'POST' not supported

Controller

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

Question

Once set "method" attribute to "POST", when you hit submit button, it always turn into 405 - Request method 'POST' not supported, if delete method attribute from conotroller, and delete method="post" from HTML as well, it works, anyone know how to solve this problem?

Update

I think I found the problem, this issue caused by sitemesh3, after i removed sitemesh3 features from web.xml, POST works fine, but I don't know how to solve it.

Upvotes: 2

Views: 3158

Answers (2)

Rob
Rob

Reputation: 703

I am not sure if this is relevant to your setup but I had the same error (405-post not supported)

Initially I thought it was sitemesh related. However when I looked into it a bit more in my case it was because I was using <mvc:resources /> to provide a static mapping to the decorator.

it was <mvc:resources /> that was not accepting the post requests for the decorator file as sitemesh was trying to access it using a Post request.

I changed the mapping of my decorator file to make sure it was static and responded to POST and GET requests. More details here Spring: not accept POST request under mvc:resources? how to fix that

The code I used is

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

Upvotes: 1

Muhammad Bekette
Muhammad Bekette

Reputation: 1434

Well as you figured out that the problem was with sitemesh. this link is to a project integrating springMVC with sitemesh

Upvotes: 0

Related Questions