【j2ee spring】31、巴巴运动网整合S2SH

整合hibernate4+spring4+struts2

1、项目图解

2、首先我们引入相应的jar包

3、我们配置对应的web.xml

当我们整合struts2的时候,就要扯到web方面的东西,那么就必须得配置web.xml的东西

<web-app xmlns=""xmlns:xsi=""xsi:schemaLocation=""version="3.0"><!– 在web容器中实例化spring容器 –><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring/beans.xml</param-value><!– 这个指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 –></context-param><!– 实例化spring容器 –><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><!–<init-param><param-name>config</param-name><param-value>struts-default.xml,struts-plugin.xml,classpath:config/struts/struts.xml</param-value>struts放置的目录</init-param> –></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></web-app>

其中

<!– 在web容器中实例化spring容器 –><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring/beans.xml</param-value><!– 这个指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 –></context-param>

为了web配置spring的位置

实现一个监听

<!– 实例化spring容器 –><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

配置struts2

<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!–<init-param><param-name>config</param-name><param-value>struts-default.xml,struts-plugin.xml,classpath:config/struts/struts.xml</param-value>struts放置的目录</init-param> –></filter>

不论是以什么结尾的页面到要经过struts2

<filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping>

4、我们配置一下spring的配置文件beans.xml<?xml version="1.0"encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xmlns:context=""xmlns:aop=""xmlns:tx=""xsi:schemaLocation="://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context/spring-context-4.1.xsd://www.springframework.org/schema/aop/spring-aop-4.1.xsd://www.springframework.org/schema/tx/spring-tx-4.1.xsd"><!– 扫描带有spring特殊机制的类,这是把这些包下面所有的类都添加到spring中进行管理 –><context:component-scan base-package="com.cutter_point" /><!– 属性遍历器 –><!– <context:property-placeholderlocation="classpath:jdbc.properties" /> –><!– 连接数据库属性配置,destroy-method="close"就是说在这个bean被摧毁的情况下可以调用这个bean默认的close方法–><bean id="myDataSource" class="org.apache.commons.dbcp2.BasicDataSource"destroy-method="close"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/><property name="url"value="jdbc:oracle:thin:@localhost:1522:orcl"/><property name="username"value="xf1205020116"/><property name="password"value="xf1205020116"/><!– 连接池启动时的初始值 –><property name="initialSize" value="1"/><!– 连接池的最大值 dbcp2里面似乎没有–><!– <property name="maxActive"value="500"/> –><!– 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 –><property name="maxIdle" value="2"/><!– 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 –><property name="minIdle" value="1"/></bean><!– hibernate二级缓存的配置 –><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!– configuration elided for brevity –><property name="dataSource" ref="myDataSource" /><!– <propertyname="mappingResources"><list> 映射文件<value>com/cutter_point/bean/product/ProductType.hbm.xml</value></list></property>–><property name="hibernateProperties"> <!– 用来配置hibernate的属性配置 –><value>hibernate.dialect=org.hibernate.dialect.OracleDialecthibernate.hbm2ddl.auto=update <!–其他取值 create、create-drop、update、validate none–>hibernate.show_sql=truehibernate.format_sql=true<!– 开启二级缓存功能 –>hibernate.cache.use_second_level_cache= truehibernate.cache.use_query_cache= falsehibernate.cache.region.factory_class= org.hibernate.cache.ehcache.EhCacheRegionFactory<!– hibernate3的二级缓存配置 –><!– <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>–></value></property><property name="packagesToScan" value="com.cutter_point.bean" /></bean><!– 事务管理器,吧上面配置的bean注入到这个里面 –><bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!– 我们采用注解的方式来使用这个事务,首先我们开启事务 –><tx:annotation-driven transaction-manager="txManager" /><!– Action交给spring托管 –><!– <bean id="productTypeAction"class="com.cutter_point.web.action.product.ProductTypeAction"scope="prototype"/> –></beans>

注意我们的spring配置文件里面也不必再写对应的xml文件在哪了

我们可以这样吧action交给spring托管,也就是我们的控制器,java web中是servlet,这里用一个类继承actionsupport的一个类作为action

可见内心底对旅行是多么的淡漠。

【j2ee spring】31、巴巴运动网整合S2SH

相关文章:

你感兴趣的文章:

标签云: