Struts+Spring+Ibatis

前几天因为需要,将spring2、struts2和ibatis进行了整合,整合过程涉及到很多技巧,因此作为日志保存在此,以便今后查询。

各个框架在本项目内的作用:

spring2:主要利用ioc,以及对事物的管理,减少硬性编码和脱离手动事务控制。

struts2:主要用于MVC以及数据校验。struts2摆脱了struts1性能上的瓶颈,达到了新的高度,配置更灵活,全面支持ajax,freemark等等,采用ognl动态语言使得输出也更加灵活。

iBatis:主要用于作orm。开发效率不如hibernate,但是由于是半自动映射,因此更加灵活,并且效率更好,维护更方便。

整合过程(eclipse导包过程省略)

主要配置如下:

配置web.xml


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   
        index.jsp
   

   
   
        struts2
      
       
            org.apache.struts2.dispatcher.FilterDispatcher
       

   

   
        struts2
        /*
   

   
   
        CharacterFilter
       
            com.afl.system.filter.CharacterFilter
       

   

   
        CharacterFilter
        /*
   

  
   
       
       
org.springframework.web.context.ContextLoaderListener
       

   

   
   
        contextConfigLocation
       
       
            /WEB-INF/springframework/applicationContext-*.xml
       

   

   
   
   
        dwr
        uk.ltd.getahead.dwr.DWRServlet
       
            debug
            true
       

   

   
        dwr
        /dwr/*
   

配置Spring
applicationContext-iBatis.xml


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
   
           class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
       
       
       
       
       
       
       
                    value="${jdbc.acquireIncrement}" />
       
                    value="${jdbc.initialPoolSize}" />
                    value="${jdbc.idleConnectionTestPeriod}" />
                    value="${jdbc.acquireRetryAttempts}" />
   

   
           class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
       
            /WEB-INF/iBatis/SqlMapConfig.xml
       

       
       
           
       

   

  
           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       
            /WEB-INF/iBatis/jdbc.properties
       

   

  
           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       
           
       

   


           class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
       
       
           
       

       
       
           
                PROPAGATION_REQUIRED,-Exception
                PROPAGATION_REQUIRED,-Exception
                PROPAGATION_REQUIRED,-Exception
                PROPAGATION_REQUIRED,-Exception
                PROPAGATION_REQUIRED,-Exception
                PROPAGATION_REQUIRED,readOnly
                PROPAGATION_REQUIRED,readOnly
                PROPAGATION_REQUIRED
           

       

   

   
            class="com.afl.register.service.dao.impl.RegisterDAOImpl">
       
           
       

       
           
       

   

   
   
       
           
               
                   
               

           

       

   

jdbc.properties
这个就是spring里面c3p0对应的值
jdbc.driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=数据库名
jdbc.user=sa
jdbc.password=密码
jdbc.minPoolSize=5
jdbc.maxPoolSize=20
jdbc.maxIdleTime=1800
jdbc.acquireIncrement=5
jdbc.maxStatements=50
jdbc.initialPoolSize=10
jdbc.idleConnectionTestPeriod=1800
jdbc.acquireRetryAttempts=30

接下来继续iBatis和spring的整合
根据上面的配置,在WEB-INF/iBatis中应该还有SqlMapConfig.xml文件,内容如下



"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

   
   
   

接着就是User.Java和User.xml的内容
User.java

public class User {
    // 主键 id
    private Long id;

    // 用户名
    private String username;

    // 密码
    private String password;
      ....... 以下省略 ......
}
User.xml


"http://ibatis.apache.org/dtd/sql-map-2.dtd">

   
   
   
        insert into
        AFL_User(username,password)values(#username#,#password#)
   

至此,iBatis与Spring的整合完成。接着是整合struts2

由于从spring配置文件中读取bean会经常用到WebApplicationContext,并且struts2采用了vs机制,因此想要像struts1那样操作request,response,session需要做一些处理,所以建立了一个基类,以后的action由此派生

BaseAction.java
下面红色的就是需要的基类,可以集成后获得request,response,session以为web上下文,从此除了按struts2方式处理外,遇到难题仍然可以像struts1那样处理问题了

/**
* 所有Action的基类,继承自BaseAction的action都可以直接使用HttpServletRequest,HttpServletResponse和Session
*/
package com.afl.system.struts2.action;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionSupport;

/**
* @author zcpeng
*
*/
public class BaseAction extends ActionSupport implements SessionAware,
        ServletRequestAware, ServletResponseAware, ServletContextAware {
    protected Map session;

    protected HttpServletRequest request;

    protected HttpServletResponse response;

    protected ServletContext context;

    /*
     * (non-Javadoc)
     *
     * @see org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet.ServletContext)
     */
    public void setServletContext(ServletContext context) {
        // TODO Auto-generated method stub
        this.context = context;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.struts2.interceptor.ServletResponseAware#setServletResponse(javax.servlet.http.HttpServletResponse)
     */
    public void setServletResponse(HttpServletResponse response) {
        // TODO Auto-generated method stub
        this.response = response;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest)
     */
    public void setServletRequest(HttpServletRequest request) {
        // TODO Auto-generated method stub
        this.request = request;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
     */
    public void setSession(Map session) {
        // TODO Auto-generated method stub
        this.session = session;
    }
   

    public ApplicationContext getApplicationContext() {
        return WebApplicationContextUtils.getWebApplicationContext(context);
    }

    public Object getObject(String beanName) {
        return getApplicationContext().getBean(beanName);
    }

}

接下来在/web-inf/springframework目录下建立applicationContext-action.xml,这里全是struts2的action,并且交给spring产生
生成方式采用的是prototype,至于为什么,参阅spring文档


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
   
   
            class="com.afl.system.struts2.action.BaseAction" scope="prototype">
   

   
            class="com.afl.register.action.RegisterAction" scope="prototype">
   

最后,也是最重要的一个配置文件struts.xml,这个文件是在classes目录下


"http://struts.apache.org/dtds/struts-2.0.dtd">

   
   
 
   
 
   

            value="messageResource">
   

   

            abstract="baseAction">
                    method="addUser">
           
           
                /register/register_success.jsp
           

           
            /register/register.jsp
       

   

至此,spring2+struts2+ibatis整合完毕,代码省略……

你可能感兴趣的:(框架)