Spring进行mvc开发中主要有三个配置文件web.xml、root-context.xml(名字可以自己更改)、servlet-context.xml(名字可以自己更改)
1.web.xml作用
主要定义应用的上下文applicationContext、spring使用的核心配置文件地址(也即root-context.xml)、spring对http请求路由使用的dispatcher、相应的控制器配置文件地址(也即servlet-context.xml)、匹配怎样的url地址路由到控制器中,一个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">
<!-- spring使用的核心配置文件地址 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- 应用的上下文applicationContext -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 相应的控制器配置文件地址 -->
<servlet>
<servlet-name>baobaotao</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--匹配怎样的url地址路由到控制器中-->
<servlet-mapping>
<servlet-name>baobaotao</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
2.spring使用的核心配置文件(root-context.xml)作用
主要配置spring在启动过程中需要加载的数据库访问层、服务层对应的bean,以及一些spring启动时需要加载的bean、数据源的配置等等,一个实例:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--spring在启动过程中需要加载的数据库访问层、服务层对应的bean-->
<context:component-scan base-package="com.baobaotao.dao" />
<context:component-scan base-package="com.baobaotao.service" />
<!--数据源的配置-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/sampledb"
p:username="root"
p:password=""/>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:../mybatisconfig/mybatisConfig.xml"/>
<bean class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
</beans>
3.spring使用的控制器配置文件(servlet-context.xml)作用
主要配置视图解析器和视图文件位置以及格式、控制器对应的包、静态资源目录位置、对请求体和响应体的解析器等等,一个实例如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<!-- 开启@Controller可以被spring作为控制器 -->
<annotation-driven />
<!-- 将http中访问的静态文件映射到相应目录 -->
<resources mapping="/resources/**" location="/resources/" />
<!-- 配置视图解析器和视图文件位置以及格式-->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp">
</beans:bean>
<!-- 控制器对应的包 -->
<context:component-scan base-package="com.baobaotao.web" />
<!--请求体和响应体的解析器-->
<beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
p:messageConverters-ref="messageConverters"/>
<util:list id="messageConverters">
<beans:bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</util:list>
</beans:beans>