1、web.xml中配置:
该配置文件主要作用是启动服务器时加载初始化spring及springMVC。
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Form Handling</display-name>
<!-- Spring监听器配置,服务器启动时加载spring并初始化 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-servlet.xml</param-value>
</context-param>
<!-- springMVC的配置 -->
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc/springmvc-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2、spring配置文件:spring-servlet.xml
该配置文件主要是让spring管理bean(控制反转与依赖注入)。以及让spring管理hibernate(本示例将hibernate的配置全部由spring管理,不需要hibernate自己的配置文件。spring主要管理hibernate的事务及sessionFactory)。
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/redis
http://www.springframework.org/schema/redis/spring-redis.xsd">
<!-- properties文件的引入,引入数据源参数文件 -->
<!--<context:property-placeholder location="classpath:redis.properties" /> -->
<!-- bean的扫描组件,指定包下的bean交由spring管理 -->
<context:component-scan base-package="cn.test"/>
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/cc" />
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- hibernate的session工厂配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="false">
<!-- 注入datasource,给sessionfactoryBean内setdatasource提供数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- //加载实体类的映射文件位置及名称 -->
<property name="mappingLocations" value="classpath:cn/test/hibernate/pojo/*.hbm.xml"></property>
<!-- hibernate常用配置,不配置使用默认值 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- 设定transactionManager,将事务交由spring管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--启动spring事务注解功能-->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- hibernate模板,写出这部分注释主要是想说明hibernate4不支持hibernate模板,hibernate3支持 -->
<!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> -->
</beans>
3、springMVC配置文件:springmvc-servlet.xml
该配置文件是springMVC所需要的配置文件,该配置使用注解方式配置。
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 扫描controller层 -->
<context:component-scan base-package="cn.ct.hibernate.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- springMVC的注解驱动,在控制层使用@controller注解即可。使用该方式可以不用手写映射器和适配器的bean配置。 -->
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 拦截器 -->
<!--<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/testForm"/>
<bean class="mytest.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors> -->
</beans>