2016-03-15 111 views
0

向控制器執行請求時,顯示錯誤404。 我不明白我做錯了什麼。缺少映射到控制器的請求。 Spring MVC

下面發佈的web.xml,* -servlet.xml,控制器類。

的web.xml

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

</web-app> 

通訊高爾夫-servlet.xml中

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

    <mvc:default-servlet-handler/> 

    <context:component-scan base-package="com.vlad.home.controllers.*" /> 

    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" 
        value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

和控制器類

@Controller 
@RequestMapping("/") 
public class TestController { 

    @RequestMapping(value = "/test", method = RequestMethod.GET) 
    public ModelAndView render() { 
     System.out.println("WAS CALLED CONTROLLER METHOD render()"); 
     ModelAndView modelAndView = new ModelAndView("test"); 
     return modelAndView; 
    } 

    @RequestMapping(value = "/*") 
    public ModelAndView others() { 
     System.out.println("WAS CALLED CONTROLLER METHOD others()"); 
     ModelAndView modelAndView = new ModelAndView("test"); 
     return modelAndView; 
    } 

} 

而且在tomcat的輸出控制檯日誌錯過映射URL路徑我的控制器,所以很奇怪。

мар 15, 2016 5:57:26 PM org.apache.catalina.startup.TldConfig execute 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
мар 15, 2016 5:57:26 PM org.springframework.web.context.ContextLoader initWebApplicationContext 
INFO: Root WebApplicationContext: initialization started 
мар 15, 2016 5:57:26 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh 
INFO: Refreshing Root WebApplicationContext: startup date [Tue Mar 15 17:57:26 EET 2016]; root of context hierarchy 
мар 15, 2016 5:57:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/golfing-servlet.xml] 
мар 15, 2016 5:57:27 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler 
INFO: Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0' 
мар 15, 2016 5:57:27 PM org.springframework.web.context.ContextLoader initWebApplicationContext 
INFO: Root WebApplicationContext: initialization completed in 328 ms 
мар 15, 2016 5:57:27 PM org.springframework.web.servlet.DispatcherServlet initServletBean 
INFO: FrameworkServlet 'golfing': initialization started 
мар 15, 2016 5:57:27 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh 
INFO: Refreshing WebApplicationContext for namespace 'golfing-servlet': startup date [Tue Mar 15 17:57:27 EET 2016]; parent: Root WebApplicationContext 
мар 15, 2016 5:57:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/golfing-servlet.xml] 
мар 15, 2016 5:57:27 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler 
INFO: Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0' 
мар 15, 2016 5:57:27 PM org.springframework.web.servlet.DispatcherServlet initServletBean 
INFO: FrameworkServlet 'golfing': initialization completed in 76 ms 

我真的不明白錯誤的地方在哪裏。

+0

我認爲你缺少''在golfing-servlet.xml中。添加並嘗試它。 – Ashrumochan

回答

0

現在結構有一定的另一視圖:

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_2_5.xsd" 
     id="WebApp_ID" version="2.5"> 
    <display-name>Spring3MVC</display-name> 
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 

    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/config/spring-app-context.xml</param-value> 
    </context-param> 

    <servlet> 
    <servlet-name>jdbc</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>jdbc</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app> 

彈簧APP-context.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 


    <context:component-scan base-package="com.vlad.home.*" /> 

    <context:property-placeholder location="classpath:dataSource.properties, classpath:hibernate.properties" /> 

    <mvc:annotation-driven/> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${jdbc.driverClassName}" /> 
     <property name="url" value="${jdbc.url}"/> 
     <property name="username" value="${jdbc.username}" /> 
     <property name="password" value="${jdbc.password}"/> 

    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" > 
     <property name="dataSource" ref="dataSource"/> 
     <property name="packagesToScan"> 
      <list> 
       <value>com.vlad.home.*</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm.auto}</prop> 
       <prop key="hibernate.c3p0.min_size">${hibernate.c3p0.min_size}</prop> 
       <prop key="hibernate.c3p0.acquire_increment">${hibernate.c3p0.acquire.increment}</prop> 
       <prop key="hibernate.c3p0.max_size">${hibernate.c3p0.max.size}</prop> 
       <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop> 
       <prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</prop> 
       <prop key="hibernate.c3p0.idle_test_period">${hibernate.c3p0.idle_test_period}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager"/> 


    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" 
        value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

和JDBC-servlet.xml中

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

    <mvc:default-servlet-handler/> 

    <context:component-scan base-package="com.vlad.home.controllers.or"/> 

    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/> 
</beans> 

<mvc:annotation-driven/>是需要applicationContext.xml