SSH配置
struts2要在web.xml中配置拦截器StrutsPrepareAndExecuteFilter
在classpath要有struts.xml中配置package
//项目启动时运行此方法InitOperation
<servlet>
<servlet-name>initOperation</servlet-name>
<servlet-class>com.uplooking.club.base.util.InitOperation</servlet-class>
</servlet>
spring在web.xml中需要配置需要配置监听器
<!-- 项目启动的时候,spring把所有xml中的对象加载到 ContextLoaderListener调用的 BeanFactory容器里 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
//加载其他的配置文件的配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/applicationContext.xml,classpath*:/spring/spring-bean.xml</param-value>
</context-param>
还有applicationContext.xml文件(web-inf下默认的名字)
要有:
<!--数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
与数据库交互的信息
<!--二级缓存-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
有datasource与实体类
<!-- 一级缓存-->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
session缓存有二级缓存配置
hibernate在web.xml中没有配置,只需要在spring中将hibernante配置到spring中的datasource与sessionfactory中。
hibernate有两个配置文件一个***.cfg.xml一个是****.hbm.xml
1、先配置web.xml
/*配置spring的bean.xml*/
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean2.xml</param-value>
</context-param>
/*配置监听器*/
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
/*配置过滤器*/
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
2、配置spring的xml(bean.xml)
/*配置数据源*/
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/uplooking"/>
<property name="user" value="root"/>
<property name="password" value=""/>
<!--初始化时获取的连接数 必须在minPoolSize与maxPoolSize之间-->
<property name="initialPoolSize" value="2"/>
<!--连接池中保留的最小连接数-->
<property name="minPoolSize" value="1"/>
<!--连接池中保留的最大连接数-->
<property name="maxPoolSize" value="300"/>
<!--最大空闲时间 60s内未使用则链接被丢弃(默认为0代表永不丢弃)-->
<property name="maxIdleTime" value="60"/>
<!--当连接池中的链接耗尽的时候,c3p0一次新创建的连接数为5(默认为3)-->
<property name="acquireIncrement" value="5"/>
<!--每60S检查连接池中的所有链接中的空闲链接数-->
<property name="idleConnectionTestPeriod" value="60"/>
</bean>
/*配置session工厂*/
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources" >
<list>
<value>springStruts2/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
</value>
</property>
</bean>
<!--事物处理必须加上这句-->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<context:component-scan base-package="springStruts2"/>
3、配置struts2
/*这句话表明所有的action的类有spring管理*/
<constant name="struts.objectFactory" value="spring"></constant>
<package name="myuser" namespace="/" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<result name="list">/userlist.jsp</result>
<result name="addUI">/addUI.jsp</result>
</action>
</package>