今天配置项目时遇到一个问题,tomcat启动没有报错,但是访问页面的时总是报404,后台打印的日志是:
8080-exec-1] WARN springframework.web.servlet.PageNotFound -No mapping found for HTTP request with URI [/wap/lm/test.html] in DispatcherServlet with name ‘Spring MVC Dispatcher Servlet‘
2015-11-06 12:47:28,455 [http-bio-8080-exec-1] DEBUG gframework.web.servlet.DispatcherServlet -Successfully completed request
遇到这个问题,第一感觉是Controller是没有扫描成功,但是启动的时候并没有报异常,该配置的也都有,问题应该在web.xml中,仔细看web.xml
看到了这个AnnotationConfigWebApplicationContext,这个是基于java的配置,这里暂不讨论这个;
因为在配置springMVC入口时是使用基于java的配置,Spring 容器这个类是一个拥有 bean 定义和依赖项的配置类。@Bean
注释用于定义 bean。自然找不到我们做的一系列的xml配置文件,所以找不到页面,报404错误,改错很简单,讲基于java的配置注释掉,就行了,web.xml文件修改如下面。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!--<context-param>--> <!--<param-name>contextClass</param-name>--> <!--<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>--> <!--</context-param>--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:application*.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--SpringMVC的入口--> <servlet> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!--这里是通过注解的方式配置SpringMVc的入口--> <!--<param-name>contextClass</param-name>--> <!--<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>--> <!--这里是通过xml方式--> <param-name>contextConfigLocation</param-name> <param-value>classpath*:web-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <url-pattern>*.json</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <url-pattern>*.api</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.htm</welcome-file> </welcome-file-list> </web-app>
报404错误,不知怎么找错,其实仔细看后台启动日志,就可以找到原因,如果一切OK的,那么会有下面标记的信息。
这里有个介绍spring基于注解配置的博客:http://blog.csdn.net/chjttony/article/details/6286144