SSi框架整合

今年7月加入了中兴通讯,进了安全平台部门,在进来之前完全不知道是做什么的,怀揣着一颗不安的新,就这么进了项目组,见到了师傅。由于这边的新员工培训机制,我领导了长达6个月的培养计划,其中的只要任务就是学习SSI框架与ExtJS5.

由于原来真心没有做过SSI框架的Web项目,所以一切都从头开始,SSI9基础搭建攻略。由于项目是由Eclipse创建的动态web项目,并使用sencha cmd命令在WebContent目录下直接生成的extjs项目,所以在WEB-INF目录下找不到classes文件夹,这对后面的配置产生了一定的影响。



项目使用ibatis-2.3.2.715、spring-3.0.5、struts-2.3.24、postpreSQL9.3搭建。本人的搭建顺序是Struts-->Spring-->ibatis。

前往stuts官网下载struts2完全版http://struts.apache.org/download.cgi#struts2324
将struts/lib目录下的核心jar包导入到web项目的WEB-INF/lib目录:commons-fileupload、commons-io、freemarker、javassist、ognl、struts2-core。xwork-core。

首先要在web项目的web.xml文件中进行spring和struts的配置,在原本web.xml内容的基础上添加如下配置:

<!--  struts2配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <!--  让Struts2的核心Filter拦截所有请求 -->
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--  Spring 监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>

其中struts拦截的是所有的类型的请求,spring监听器中param-value的值是spring配置文件applicationcontext.xml的路径,由于eclipse会自动将该文件加载到WEB-INF/classes目录下,所以配置时直接配置在classes目录下即可。

接下来是在src根目录下创建struts配置文件,struts.xml

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

 <struts>
     <!--struts2的action由spring来负责实例化,指定struts默认的ObjectBean -->
    <constant name="struts.objectFactory" value="spring" /> 
    <package name="default" namespace="/" extends="struts-default">  
        <action name="login" class="com.zwb.action.LoginAction" method="login">
        </action>
    </package>
 </struts>

namespace:命名空间,默认为’/’,如果未填,当你页面请求action时就会到根目录寻找包名为login下的action,如果你填写了namespace为’/’,就会到相应的目录下寻找,如果没找到,还会到根目录下继续寻找对应的action,如果还是未找到就会报错
extends:类似于java中的继承特征,extends=”struts-default”就是继承struts-default.xml,它里面定义了许多跳转类型、拦截器等一些常用的东西。

这里的action实例是一个登陆action,loginAction代码如下:

package com.zwb.action;
import java.io.IOException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zwb.dao.UserDao;
import com.zwb.model.User;
public class LoginAction extends ActionSupport {
    private String userName;
    private String passWord;
    boolean resultBoolean = false;  
    private static final long serialVersionUID = 1L;    
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }


    public void login()
    {
        //todo
    }
}

这个action中的重点为action类要继承自ActionSupport 类,如果不在struts.xml中配置action对应的方法的话,则必须重写ActionSupport 类的execute方法;申明userdao类,并完成get和set方法,为spring依赖注入做准备。至此struts2的配置就基本完成了,下面开始配置spring,继续在lib目录下导入spring的核心jar包。

在src目录下创建spring配置文件applicationContext.xml:

    <bean id="LoginAction" class="com.zwb.action.LoginAction">
        <property name="userDao" ref="userDao" />
    </bean>
    <bean id="userDao" class="com.zwb.daoImpl.UserDaoImpl">
         <property name="dataSource" ref ="dataSource"/>
        <property name="sqlMapClient" ref ="sqlMapClient"/> 
    </bean>

这边的loginAction就是struts中的配置的action的bean,根据依赖关系,需要继续配置userdao类,userdao类又是需要对数据库进行操作的实体,所以需要开始配置dataSource与sqlMapClient,这里我们先配置dataSource,还是在applicationContext.xml文件中加入如下配置,数据库使用的postpresql9.3版本,并且数据库配置信息是直接写死的,在以后改进中可以使用el表达式从配置文件中获取,在此处先暂时直接这么做。

<bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName">
            <value>org.postgresql.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:postgresql://localhost:5432/extjstest</value>
            <!-- jdbc:mysql://127.0.0.1/test -->
        </property>
        <property name="username">
            <value>postgres</value>
        </property>
        <property name="password">
            <value>123456</value>
        </property>
        <property name="maxActive">
            <value>40</value>
        </property>
        <property name="maxIdle">
            <value>10</value>
        </property>
        <property name="maxWait">
            <value>18000</value>
        </property>
        <property name="defaultAutoCommit">
            <value>true</value>
        </property>
    </bean>

sqlMapClient配置如下,依旧在spring配置文件中:

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">   
        <property name="configLocation">   
            <value>WEB-INF/sqlMapConfig.xml</value>   
        </property>   
        <property name="dataSource" ref="dataSource"/>
    </bean> 
    <!-- Spring iBatisTemplate -->
    <bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
          <property name="sqlMapClient">
              <ref bean="sqlMapClient"/>
          </property>
    </bean>

SqlMapClientTemplate是org.springframework.orm.ibatis下的
而SqlMapClient是ibatis的
SqlMapClientTemplate是SqlMapClient的封装类.
SqlMapClient中包含着session的管理.
SqlMapClientTemplate用于session的封装,以及异常的捕捉.
所以按照以上的推断来说.应该尽量使用SqlMapClientTemplate.
保证session以及Exception的正常以及统一。
这里需要导入ibatis的jar包,并且在WEB-INF目录下创建sqlMapConfig.xml的ibatis的配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE sqlMapConfig 
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" 
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
        <settings
            cacheModelsEnabled="true"
            enhancementEnabled="true"
            lazyLoadingEnabled="true" 
             errorTracingEnabled="true"
             maxRequests="32"
             maxSessions="10"
             maxTransactions="5" 
             useStatementNamespaces="false" 
             />
             <sqlMap resource="com/zwb/dao/sqlMap/User.xml" /> 
</sqlMapConfig>
  • maxRequests

    同时执行 SQL 语句的最大线程数。大于这个值的线程将阻塞直到另一个线程执行完成。不同的 DBMS有不同的限制值,但任何数据库都有这些限制。通常这个值应该至少是maxTransactions(参见以下)的 10 倍,并且总 是大于 maxSessions 和maxTranactions。减小这个参数值通常能提高性能。

    例如:maxRequests=“256”
    缺省值:512

  • maxSessions

    同一时间内活动的最大 session 数。一个 session 可以maxSessions是代码请求的显式 session,也可以是当线程使用SqlMapClient 实例(即执行一条语句)自动获得的session。它应该总是大于或等于 maxTransactions 并小于 maxRequests。减小这个参数值通常能减少内存使用。

    例如:maxSessions=“64”
    缺省值:128

  • maxTransactions

    同时进入 SqlMapClient.startTransaction()的最大线程maxTransactions 数。大于这个值的线程将阻塞直到另一个线程退出。不同的 DBMS 有不同的限制值,但任何数据库都有这些限制。这个参数值应该总是小于或等于maxSessions 并总是远远小于 maxRequests。减小这个参数值通常能提高性能。

    例如:maxTransactions=“16”
    缺省值:32

  • cacheModelsEnabled

    全局性地启用或禁用 SqlMapClient 的所有缓存cacheModelsEnabled model。调试程序时使用。

    例如:cacheModelsEnabled=“true”
    缺省值:true(启用)

  • lazyLoadingEnabled

    全局性地启用或禁用SqlMapClient的所有延迟加载。lazyLoadingEnabled 调试程序时使用。
    例子:lazyLoadingEnabled=“true”
    缺省值:true(启用)

  • enhancementEnabled

    全局性地启用或禁用运行时字节码增强,以优化访enhancementEnabled
    问Java Bean属性的性能,同时优化延迟加载的性能。

    例子:enhancementEnabled=“true”
    缺省值:false(禁用)

  • useStatementNamespaces

    如果启用本属性,必须使用全限定名来引用 mapped useStatementNamespaces
    statement。Mapped statement 的全限定名由 sql-map 的名称和 mapped-statement 的名称合成。例如: queryForObject(“sqlMapName.statementName”);

    例如:useStatementNamespaces=“false”
    缺省值:false(禁用)

  • defaultStatementTimeout

    此设置值必须为一个整数,作为JDBC连接超时的时间,这个值可以被任意一个映射文件的statement属性来重新设置,
    如果没有指明,查询将无超时时间限制除非在映射文件中设置statement属性值。被设置的值以秒为单位等待连接失效

  • classInfoCacheEnabled

    With this setting enabled, iBATIS will maintain a cache of introspected
    classes. This will lead to a significant reduction in startup time if many
    classes are reused.
    例如: classInfoCacheEnabled=“true”
    缺省值: true (启用)

  • statementCachingEnabled

    (iBATIS 2.3.0以后版本中有)
    With this setting enabled, iBATIS will maintain a local cache of
    prepared statements. This can lead to significant performance
    improvements.
    例如: statementCachingEnabled=“true”
    缺省值: true (启用)

<sqlMap resource="com/zwb/dao/sqlMap/User.xml" />

这个配置信息指向的就是存放具体sql语句的配置文件,由于sql语句较多,建议分开,归类存放。

<sqlMap namespace="User">
    <select id="searchUser" resultClass="com.zwb.model.User"
            parameterClass="com.zwb.model.User" remapResults="true">

     SELECT *
        FROM userImformation
        where userName = #userName# and passWord=#passWord#         
    </select>
  </sqlMap>

至此SSI框架的搭建的就完成了,下面运行一下吧~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值