`
ccq502849336
  • 浏览: 15885 次
  • 性别: Icon_minigender_1
  • 来自: 贵阳
文章分类
社区版块
存档分类
最新评论

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

阅读更多
关键字: struts2, ibatis, spring
首先配置Struts
web.xml配置如下:
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> 

<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代码
<?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> 

<?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内容:
Xml代码
# 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 

# 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内容:
Java代码
####################################  
# Database Connectivity Properties  
####################################  
 
driver=com.mysql.jdbc.Driver  
url=jdbc:mysql://192.168.1.38:3306/ssh  
username=netjava  
password=netjava 

####################################
# 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代码
<?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> 

<?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代码
<?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> 

<?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代码
<?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> 

<?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代码
<?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> 

<?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文件:
Java代码
<?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> 

<?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整合的配置都已完成。
分享到:
评论

相关推荐

    Struts1.2+Struts2.1.6+spring 2.0+hibernate3.1+Ibatis2.3(第二部分)

    Struts1.2+Struts2.1.6+spring 2.0+hibernate3.1+Ibatis2.3 SSH

    Struts1.2+Struts2.1.6+spring 2.0+hibernate3.1+Ibatis2.3(第一部分)

    Struts1.2+Struts2.1.6+spring 2.0+hibernate3.1+Ibatis2.3内个框架的集成

    struts2.1.6+Spring2.0.8+Ibatis2.3.4工程

    struts2.1.6+Spring2.0.8+Ibatis2.3.4工程 开发工具:eclipse3.3+myeclipse6.5 应用技术:Struts2,Spring,Ibatis 数据库:oracle 测试JDK:JDK1.6 测试服务器:Tomcat6.0 非常完整的工程,部署到Tomcat上就可以使用...

    Struts+Spring+Ibatis整合框架搭建文档

    ### Struts+Spring+Ibatis整合框架搭建知识点详解 #### 一、框架介绍与整合意义 **Struts**是一款开源的MVC框架,主要用于构建基于Java的Web应用程序。它简化了开发过程,使得开发者能够更加关注业务逻辑而非基础...

    Spring struts ibatis Mysql 集成

    这个项目集成了Spring 2.5.5、Struts 2.1.6、iBatis 2.3.4以及MySQL 5.1数据库,使用IntelliJ IDEA 9作为开发环境。下面将详细介绍这些技术和它们的集成方式。 **Spring框架**: Spring是一个开源的Java平台,主要...

    Struts2.1.6 Spring2.5.6 Ibaits2.3.4 自己搭一个框架吧

    Struts2.1.6、Spring2.5.6 和 iBATIS2.3.4 是经典的Java Web开发框架组合,它们在企业级应用中有着广泛的应用。搭建这样一个框架可以帮助开发者更好地理解和掌握这些技术,同时提高开发效率。下面将详细阐述这三个...

    一个S2S+ibatis一个增删改查的例子(由原先SSH2改编)

    4. **源码分析**:在博客链接中,作者可能会详细解释如何配置Struts2和iBatis的XML配置文件,如何定义Action类,如何编写SQL映射文件,以及如何在Action中调用iBatis的SqlSession执行SQL语句。此外,还会涉及到如何...

    jmesa学习笔记。。。

    2. **环境搭建**: 笔者基于SSI框架(Struts2.1.6 + Spring3.0 + iBatis-2.3.4.726)搭建了学习环境,并按照官方示例进行了环境的配置与测试。 #### 三、配置JMesa环境 1. **文件结构**: - 在`webRoot`目录下创建...

    框架jar包大全(SSH)

    标题中的"框架jar包大全(SSH)"指的是三个流行的Java Web开发框架:Struts2、Spring和Hibernate的集合,这些框架的jar包集合通常被称为SSH整合。这个描述表明提供了一个包含所有SSH框架所需的jar包的压缩文件,使得...

    个人整理的很新的jar包

    首先,`struts2.1.6`是一个Struts 2框架的版本。Struts 2是基于MVC(Model-View-Controller)设计模式的开源Web应用框架,它整合了WebWork与Struts 1的优点,提供了一种强大的、可扩展的和易于使用的开发框架。...

    SSI必须jar包

    5. **struts2-core-2.1.6.jar**:Struts2的核心库,负责处理HTTP请求,管理动作映射,以及执行视图渲染。Struts2是一个流行的MVC框架,它极大地简化了Java Web应用的开发。 6. **mysql-connector-java-5.1.0.5.jar*...

Global site tag (gtag.js) - Google Analytics