spring mvc web程序中post请求返回405,使用get能正常访问

等花的牛粪 发布于 2015/08/27 13:09
阅读 6K+
收藏 1

我搭了一个spring mvc 的web程序,不过使用post方式访问总返回405.

控制代码:

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

@Controller
public class LoginController {
    private static Logger logger = Logger.getLogger(LoginController.class);
    
    @RequestMapping("/login")
    public String login(){
        logger.info("show login page.");
        return "login.html";
    }
    
    @RequestMapping(value="/doLogin", method=RequestMethod.POST)
    public ModelAndView doLogin(HttpSession session){
        logger.info("user login.");
        session.setAttribute("USER", "admin");
        
        //return "superMain.html";
        return new ModelAndView(new InternalResourceView("/view/superMain.html"));
    }

}

mvc配置:

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="mediaTypes">
            <map>
                <entry key="json" value="application/json"/>
                <entry key="xml" value="application/xml"/>
                <entry key="html" value="text/html"/>
            </map>
        </property>
    </bean>

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1"/>
        <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/view/" />
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
            </list>
        </property>
    </bean>

错误信息:

11:53:38.819 [http-apr-8080-exec-6] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
11:54:35.105 [http-apr-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'springMvc' processing POST request for [/cbos-web-1.0/doLogin]
11:54:35.118 [http-apr-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /doLogin
11:54:35.133 [http-apr-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public org.springframework.web.servlet.ModelAndView com.gtstar.cbos.web.controller.LoginController.doLogin(javax.servlet.http.HttpSession)]
11:54:35.137 [http-apr-8080-exec-3] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'loginController'
- user login.
11:54:35.181 [http-apr-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.InternalResourceView: unnamed; URL [/view/superMain.html]] in DispatcherServlet with name 'springMvc'
11:54:35.183 [http-apr-8080-exec-3] DEBUG o.s.w.s.view.InternalResourceView - Forwarding to resource [/view/superMain.html] in InternalResourceView 'null'
11:54:35.183 [http-apr-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'springMvc' processing POST request for [/cbos-web-1.0/view/superMain.html]
11:54:35.189 [http-apr-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /view/superMain.html
11:54:35.194 [http-apr-8080-exec-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Did not find handler method for [/view/superMain.html]
11:54:35.209 [http-apr-8080-exec-3] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Matching patterns for request [/view/superMain.html] are [/view/**]
11:54:35.226 [http-apr-8080-exec-3] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - URI Template variables for request [/view/superMain.html] are {}
11:54:35.238 [http-apr-8080-exec-3] DEBUG o.s.w.s.h.SimpleUrlHandlerMapping - Mapping [/view/superMain.html] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/WEB-INF/view/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@783c52dc]]] and 1 interceptor
11:54:35.245 [http-apr-8080-exec-3] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/WEB-INF/view/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@783c52dc]]]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
11:54:35.250 [http-apr-8080-exec-3] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/WEB-INF/view/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@783c52dc]]]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
11:54:35.257 [http-apr-8080-exec-3] WARN  o.s.web.servlet.PageNotFound - Request method 'POST' not supported
11:54:35.275 [http-apr-8080-exec-3] WARN  o.s.w.s.m.s.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Request method 'POST' not supported
11:54:35.282 [http-apr-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'springMvc': assuming HandlerAdapter completed request handling
11:54:35.316 [http-apr-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
11:54:35.317 [http-apr-8080-exec-3] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request


各位大神,请指点下。

加载中
0
杨三更
杨三更
这个方法不支持POST 需要再加注解吧
等花的牛粪
doLogin是有method的指定的啊,主要是doLogin
0
猫神
猫神
method = { RequestMethod.POST, RequestMethod.GET }
等花的牛粪
这种方式尝试过了,同样是405
0
分流砥柱
分流砥柱
 public @Response doLogin(HttpSession session){
        logger.info("user login.");
        session.setAttribute("USER", "admin");
        
        return "test"

    }

这样会不会有问题呢?先排除一下,View的问题。

分流砥柱
分流砥柱
我的意思返回json看看有没有错误!不好意思上面代码忘了返回值。
等花的牛粪
我的ViewResolver没有配置后缀,如果这样的话估计是返回404了
0
LucEsape
LucEsape
spring 4.x+ 注意 crsf
返回顶部
顶部