struts2+hibernate+spring2.5用properties集成整合配置方法

struts2+hibernate+spring2.5用properties集成整合配置方法
[code]
在一些项目中可能需要用properties代替hibernate.cfg.xml数据库相关配置, 可以简化管理, 方便维护或者用于动态属性加载. 首先SSH三大框架中的jar包导入到项目中, 接下来进行配置:

1.首先配置WEB.XML文件
<!-- spring 配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

<!-- struts2 配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

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

2.导入struts2-spring-plugin-2.1.6.jar包, 无需配置struts+spring即可集成使用
如果要动态调用ACTION中的方法, 请在struts.xml中配置:
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

3.主要的applicationContext.xml文件配置:
现在<beans>标签中配置以下属性
① <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

② 配置properties文件:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:system.properties</value>
</property>
</bean>

说明: <value>标签中的classpath:不能省略, 它代表是类环境根目录下, 也就是项目中SRC的目录底下去寻找
system.properties文件. 这是properties属性文件中的配置信息(根据实际信息进行修改):
#System Database Config
database.driverClassName=net.sourceforge.jtds.jdbc.Driver
database.connectionString=jdbc:jtds:sqlserver://10.1.0.6:1433/vendorDoc
database.username=sa
database.password=sql
hibernate.dialect=org.hibernate.dialect.SQLServerDialect

③ 配置SESSIONFACTORY
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>cn/vendordoc/entity/DocRef.hbm.xml</value>
<value>cn/vendordoc/entity/Staff.hbm.xml</value>
<value>...</value>
</list>
</property>

<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.driver_class">${database.driverClassName}</prop>
<prop key="hibernate.connection.url">${database.connectionString}</prop>
<prop key="hibernate.connection.username">${database.username}</prop>
<prop key="hibernate.connection.password">${database.password}</prop>

<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>

</props>
</property>
</bean>

④ 配置事务特性:

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- <tx:method name="execute*" propagation="REQUIRED"/> -->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<!-- 配置那些类的方法进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution (* cn.test..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>

</aop:config>
说明: expression中的表达式意思是在cn.test包下的所有子包及类的所有方法都进行事务管理, 如果你项目中分有数据层, 则把数据层的包路径替换cn.test. 在项目中, 可能会有其他层例如WEB层调用数据库内容进行延迟加载, 这样会SESSION会报错, 解决的办法, 一, 增添lazy = false 属性; 二, 在web.xml中配置spring延迟加载
<!-- WEB层延迟加载,-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping >
<filter-name>hibernateFilter</filter-name>
<url-pattern>/forum/*</url-pattern>
</filter-mapping>

4. 你可以把struts的action交给spring管理, 或者直接调用也可以.
如果需要spring管理, 在applicationContext.xml文件中进行定义:
<bean name="userAction" parent="baseAction" class="cn.vendor.web.action.UserAction" scope="prototype" >
</bean>

然后在struts.xml中配置
<package name="user" extends="default" namespace="/user" >
<action name="jumpLogin" class="userAction" method="loginView">
<result>/WEB-INF/user/login.jsp</result>
</action>
</package>

这样整个配置就已经完成了.
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值