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>