Web.xml中监听器的作用是监听servletContext对象的创建,web中最大的域,此对象创建则代表项目启动了,项目启动就要加载spring配置文件
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans";
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns:p="http://www.springframework.org/schema/p";
xmlns:aop="http://www.springframework.org/schema/aop"; xmlns:context="http://www.springframework.org/schema/context";
xmlns:tx="http://www.springframework.org/schema/tx";
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd";>
<!-- 本程序采用注解的方式开发,比较简单 -->
<!-- 1.开启注解,spring注解,后面还要开启事务注解-->
<context:component-scan base-package="com.ruicai.ssh.*"/>
<!-- 2.dataSource配置,首先配置数据库的配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 使用dpcp数据源 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 3.sqlSessionFactory.Hibernate持久层框架需要这个,在spring-orm的jar包中-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- mappingLocation和mappingResources二选其一 -->
<!-- <property name="mappingLocations" value="classpath:com/ruicai/dsx/entity/*.hbm.xml"/> -->
<property name="mappingResources">
<list>
<value>com/ruicai/ssh/domain/Userinfo.hbm.xml</value>
</list>
</property>
<!-- 将Hibernate的一些配置属性加入到spring的主配置文件中 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
</bean>
<!-- 4.HibernateTemplate配置 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 5.spring事务管理配置,并开启事务注解扫描功能 有两个选择,一是针对Hibernate的事务在spring-ormjar包中,
二是针对数据库的事务,在spring-jdbc的jar包中-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<!-- 既然是Hibernate的事务,则里面要配置SessionFactory,如果是针对jdbc则是配置dataSource属性 -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd";>
<struts>
<package name="dsx" namespace="/" extends="struts-default">
<!-- package的namespace属性和action的name属性共同构成了url的地址 -->
<action name="user_*" class="userinfoAction" method="{1}">
<!-- name代表方法的返回结果,由此指定跳转的页面,type代表跳转方式, -->
<result name="user_{1}" type="dispatcher">user_{1}.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xmlns="http://java.sun.com/xml/ns/javaee"; xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"; id="WebApp_ID" version="3.0">
<display-name>Day1121_SSH2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置 struts过滤器,所有的请求都由该过滤器管理 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>