問題描述:
創建了web工程,配置pom.xml,web.xml,編寫controller類,在spring-mvc-servlet.xml文件中指定開啓註解和掃描的包位置 <mvc:annotation-driven /> <context:component-scan base-package="controller" />
.打開tomcat,在瀏覽器中訪問,提示404錯誤java
問題分析:
一. 肯定配置文件是否配置正確(springmvc的配置文件,web.xml和pom.xml基本不會配置出錯)mysql
由於提示的是404錯誤,能夠肯定正確加載了配置文件,處於排查須要,仍是檢查了配置文件的配置問題.
a) 放在WEB-INF/文件夾下,名稱爲spring-mvc-servlet.xml,不須要在web.xml中配置,系統自動讀取
b) 放在WEB-INF/文件夾下,名稱爲自定義,或者放在src/main/resource文件夾下,須要在web.xml中配置,
配置代碼以下:web
<servlet> <servlet-name>dispatch</servlet> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatch</servlet-name> <servlet-pattern>*.*</servlet-pattern> </servlet-mapping>
二. 肯定maven 依賴正確配置 a) 通常使用兩個依賴就能使用註解配置springmvc項目,並能正常運行 pom中的依賴以下:spring
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.6.RELEASE</spring.version> <hibernate.version>4.3.8.Final</hibernate.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
b) 我使用spring+springmvc+hibernate的搭配,pom依賴以下:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.6.RELEASE</spring.version> <hibernate.version>4.3.8.Final</hibernate.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- springframework 4 dependencies begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- springframework 4 dependencies end --> <!-- log --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- hibernate 配置 begin --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate.common</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>4.0.5.Final</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.1-api</artifactId> <version>1.0.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate.version}</version> <!-- hibernate 配置 end --> </dependency> <!-- mysql數據庫的驅動包 start --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> <!-- 數據鏈接池 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> <version>${hibernate.version}</version> </dependency> </dependencies> </dependencyManagement>
我已經依賴了a中所說的兩個包,可是不能在瀏覽器中訪問,會報404錯誤.sql
解決辦法:
看了不少spring mvc的maven依賴,發現有人使用以下依賴:數據庫
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
我在b中的pom文件中加入這個依賴,就能正常訪問了,不知道什麼緣由.(狗屁問題,浪費了我兩個小時的時間,一度懷疑人生)api