Spring MVC Controller RequestMapping方法无法识别

时间:2016-09-16 15:17:56

标签: spring spring-mvc tomcat servlet-3.0

我在控制器类中定义了2个方法。当我注释掉第二个方法(displayHomePage)时,我可以访问登录页面,但是当我取消注释第二个方法时,返回(login.jsp或home.jsp)都不起作用。它提供HTTP 404资源未找到错误。下面是我的web.xml,dispatcher.xml和控制器类。

我哪里错了?此外,如果我删除" method = RequestMethod.GET"对于这两种方法,我可以毫无问题地访问这两个页面。这令人困惑。请帮帮我。

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
   <display-name>demo</display-name>
   <servlet>
      <servlet-name>demo-dispatcher</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>demo-dispatcher</servlet-name>
      <url-pattern>*.do</url-pattern>
   </servlet-mapping>
   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>
   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>
          /WEB-INF/demo-dispatcher-servlet.xml
       </param-value>
    </context-param>
</web-app>

演示调度-servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:c="http://www.springframework.org/schema/c"
   xmlns:p="http://www.springframework.org/schema/p" 
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.2.xsd
   http://www.springframework.org/schema/mvc 
   http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">


   <context:component-scan base-package="com.abc.demo.controller" />
   <context:annotation-config />

   <bean
   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
         <value>/WEB-INF/jsp/</value>
      </property>
      <property name="suffix">
         <value>.jsp</value>
      </property>
   </bean>

   <mvc:default-servlet-handler />
   <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/" />
</beans>

控制器:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

    @RequestMapping(name="/login" , method=RequestMethod.GET)
    public String displayLoginPage(){
        return "login";
    }

/*  @RequestMapping(name="/home", method=RequestMethod.GET)
    public String displayHomePage(){
        return "home";
    }*/
}

只是WEB-INF / jsp文件夹下的login.jsp和home.jsp页面

0 个答案:

没有答案