整合Struts2.1.6+ibatis2+Spring2.5配置详解

首先配置Struts
web.xml配置如下:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/configure/applicationContext.xml classpath:com/configure/applicationContext*.xml </param-value>

</context-param>

<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>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

完成以上配置后,我们可以先部暑项目启动服务器,如果启动正常没有报错,说明struts与spring的启动是正常的,接下来我们再在项目的根目录下(src目录下面)添加sturts.xm与log4j.properties具体配置如下:
struts.xml的内容:(如没有自定义拦截器,下面的拦截器需注悉掉)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<include file="struts-default.xml"></include>
<package name="struts1" extends="struts-default">

<interceptors>
<!-- 定义登陆验证拦截器 -->
<interceptor name="loginVlidate" class="com.interceptor.UserLoginVlidate"></interceptor>
<!-- 定义拦截器栈 -->
<interceptor-stack name="checkInterceptor">
<!-- 包含登陆证和默认拦截器 -->
<interceptor-ref name="loginVlidate"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 定义全局拦截器 -->
<default-interceptor-ref name="checkInterceptor"></default-interceptor-ref>
<!-- 定义一个全局的返回结果 -->
<global-results>
<result name="login">/login.jsp</result>
</global-results>

<action name="registerAction" class="RegisterAction">
<result>index.jsp</result>
<result name="error">error.jsp</result>
</action>

<action name="loginAction" class="LoginAction">
<result>main.jsp</result>
<result name="error">error.jsp</result>
</action>


</package>

<!-- Add packages here -->

</struts>



log4j.properties内容:

# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n


完成以上配置后,我们可以做一些不涉及持久层的action测试,如果测试没问题说明struts已经配置成功,接下来就是配置ibatis,具体配轩如下:
database.properties内容:

####################################
# Database Connectivity Properties
####################################

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.1.38:3306/ssh
username=netjava
password=netjava


sql-map-config.xml内容:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>

<sqlMap resource="com/persistence/sqlmapdao/sql/userInfo.xml"/>
<sqlMap resource="com/persistence/sqlmapdao/sql/replyInfo.xml"/>
<sqlMap resource="com/persistence/sqlmapdao/sql/articleInfo.xml"/>

</sqlMapConfig>

至此,struts与ibatis的配置都已完成,接下来我们要做的就是如何用spring这个粘合济将它们整合起来,下面是spring具体配置:
首先,配置applicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/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">


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:com/configure/database.properties</value>
</list>
</property>
</bean>
<!-- ========================= GENERAL DEFINITIONS ========================= -->

<!-- Configurer that replaces ${...} placeholders with values from properties files -->
<!-- (in this case, mail and JDBC related properties) -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:com/configure/sql-map-config.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 配置事务拦截器 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事务拦截器bean依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<!-- 下面定义事物的传播属性 -->
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- 定义BeanNameAutoProxyCreator的bean后处理器 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>UserInfoService</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>



</beans>

接下来再配置applicationContext-dao.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean id="UserInfoDao" class="com.persistence.sqlmapdao.UserInfoSqlMapDao">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>

</beans>

applicationContext-service.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean id="UserInfoService" class="com.service.impl.UserInfoServiceImpl">
<property name="userDao" ref="UserInfoDao"></property>
</bean>

</beans>

applicationContext-struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

<bean id="RegisterAction" class="com.action.RegisterAction">
<property name="userService" ref="UserInfoService"></property>
</bean>


<bean id="LoginAction" class="com.action.LoginAction">
<property name="userService" ref="UserInfoService"></property>
</bean>

</beans>

到此,关于struts+ibatis+spring整合的配置都已完成。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值