第一次寫博文寫的不好,但希望能幫助大家,有什麼偏頗的地方希望大家多多斧正。在這個問題上困擾了我兩天,這兩天翻來覆去睡不著。一直在想這個問題。廢話不多說下麵進入正題。 1.創建創建web項目,加入SpringMVC的jar,我這裡演示用spring-framework-4.2.3.RELEASE。ja ...
第一次寫博文寫的不好,但希望能幫助大家,有什麼偏頗的地方希望大家多多斧正。在這個問題上困擾了我兩天,這兩天翻來覆去睡不著。一直在想這個問題。廢話不多說下麵進入正題。
1.創建創建web項目,加入SpringMVC的jar,我這裡演示用spring-framework-4.2.3.RELEASE。jar包如下圖所示:
2.配置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">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="cn.xugang.."></context:component-scan>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManagerFactoryBean"/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean" id="contentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false"/>
&