SSH项目框架,我的学习供大家参考

  这是我自己写的一个框架,因为在写的过程中遇到很多问题,想必很多才接触SSH的朋友们还是会遇到,所以拿出来供大家参考,有什么问题或建议请与我联系!我将认真听取大家的建议!

本项目采用Struts+Spring+Hibernate技术实现。

项目运行软件环境:

eclipse-SDK- 3.3.2 -win32+MyEclipse_6.0.1GA_E3.3.1_Installer+JDK1.6.0以上软件均可在官方网站上下载得到,

项目运行硬件环境:

 

首先我将介绍关于项目环境的搭建,这环境的搭建大家一定要注意,因为HibernateSpring在创建中经常会出现问题,我将截图给大家展示

 

我们现在EclipseFileànewàweb project

首先我们想搭建Spring环境,为了以后不会产生莫名其妙的错误我们把spring中所有的.jar文件全部导入。

当我们把jar都导入到项目中的后,我们再对Hibernate进行添加。

 

Hibernate里面也要把SpringPersistence导入

接着倒入Struts框架这布不需要你做其他任何操作最多就是改改名,其他一切next即可。

 

我现在完成了项目框架的导入,下面我将把我项目会用到的包建好

 

我现在来介绍下每个包的用处:

comm..westsec.ch.realty.common:里面存放公用的类如一些抽象的类,或是bean等等,能够提供公用的文件

comm..westsec.ch.realty.dao :            里面存放相关接口

comm..westsec.ch.realty.dao.impl        里面存放接口的实现

comm..westsec.ch.realty.hibernate里面存放映射的数据库表对象和HibernateSessinFactory

comm..westsec.ch.realty.service   里面存放的也是接口而且和dao 里面的接口完全一样

comm..westsec.ch.realty.service.impl 里面存放的是service接口的实现,为什么这里要要两个接口呢?目的是在为以后做多表操作的时候能够更方便,如:

public String insertTrans(Demo1 demo1, Demo2 demo2) {

       // TODO 自动生成方法存根

       String tmpId1 = demo1DAO.insertDemo1(demo1);

       String tmpId2 = demo2DAO.insertDemo2(demo2);

      

       return tmpId1+tmpId2;

    }

 

这不难理解吧!呵呵

comm..westsec.ch.realty.struts里面存放Struts文字转化文件(支持国际化),或一些用于转换的小工具,

comm..westsec.ch.realty.struts.action.comm里面存放公用的action 方法,如分页或到底实现什么操作的判断等

comm..westsec.ch.realty.struts.action     里面存放每个功能的具体操作

comm..westsec.ch.realty.struts.form.comm里面存放公用的form方法

comm..westsec.ch.realty.struts.form      里面存放每个功能的具体操作

comm..westsec.ch.realty.until           工具包

 

src里面的东西基本上就这些

webroot下面的文件:

css:      存放样式

jsp:      存放全部的JSP文件

images   存放相关图片

js:       存放全部的*.js文件

inc:      存放全部的导入文件

 

下面我将介绍下起项目的基本框架

 

我将以一个 select 例子来对其框架进行分析:
 
首先程序执行:从中提取具体动作
这是对象 form 它是继承与基础的 BaseForm, 里面定义的是操作的是结果集还是单个的对象

public class HouseForm extends BaseForm {
    private List houses = new ArrayList();
    private House house = new House();
    public List getHouses() {
       return houses ;
    }
    public void setHouses(List houses) {
       this . houses = houses;
    }
    public House getHouse() {
       return house ;
    }
    public void setHouse(House house) {
       this . house = house;
    }
   
}

 
Baseform:

 
public abstract class BaseForm  extends ValidatorForm {
    /**
      * 特别指定行为
      */
    private String action ;
    protected Log log = LogFactory.getLog(BaseForm. class );
    public String getAction() {
       return action ;
    }
 
    public void setAction(String action) {
       this . action = action;
    }
 
}

把得到的动作转给 action ,在这里默认的是查询由于是单个的 action 是继承于 baseAction 这个抽象方法的,所以先访问 baseAction, 首先程序执行 ActionForward 方法

public final ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
       this . mapping = mapping;
       this . form = form;
       this . request = request;
       this . response = response;
       messages .clear();
 
       boolean success = false ;
       forward = getDefaultForward();
       try {
           executeCore();        // 执行子类中的此方法
           System. out .println( "**********ChAction***********" );
           success = true ;
       }
       catch (Throwable e) {
           if ( debug ) {
              log .error( "CH Action 出错: " , e);
           }
           else {
              log .error( "CH Action 出错: /r/n" + e);
           }
           if ( operAction != null && operContent != null ) {
              String exMessage = null ;
              if (e.getMessage() == null ) {
                  exMessage = e.toString();
              }
              else {
                  exMessage = e.getMessage();
              }
              StringBuffer buffer = new StringBuffer(exMessage);
               buffer.append( "/r/n" ).append( "------------------------/r/n" ).append( operContent );
              operContent = buffer;
           }
           //addMessages(e);
       }
       finally {
           //log(success);
       }
 
       if (! messages .isEmpty()) {
           saveErrors(request, messages );
       }
       return forward ;
    }

子类 action 首先判断此动作为什么动作

public void executeCore() throws Throwable {
       // TODO   这个是自动生成的方法
       // 时间是 : Apr 1, 2008 -- 2:58:08 PM
       // 修改人 :杜振宇
       HouseForm houseForm = (HouseForm) form ;
      
       if ( request .getParameter( "add" ) != null ) {
           log .info( " 添加 Demo1..." );
           add(houseForm);
           forward = mapping .findForward(IConstants. FORWARD_EDIT );
       }
       else if ( request .getParameter( "edit" ) != null ) {
           log .info( " 编辑 Demo1..." );
           edit(houseForm);
           forward = mapping .findForward(IConstants. FORWARD_EDIT );
       }
       else if ( request .getParameter( "save" ) != null ) {
           log .info( " 保存分公司 ..." );
           forward = mapping .findForward(IConstants. FORWARD_EDIT );
           save(houseForm);
       }
       else if ( request .getParameter( "delete" ) != null ) {
           log .info( " 删除分公司 ..." );
           delete(houseForm);
           query(houseForm);
       }
       else {
          
           log .info( " 分公司列表 ..." );
           query(houseForm);
       }
    }

目前很显然是执行的是 query houseForm

private void query(HouseForm houseForm) throws Exception {
       PageSetBean pageBean= new PageSetBean();
       DatabaseTable dt = new DatabaseTable();
       // 显示从第几条开始取数据第一页是从那开始
       int firstPage = this .firstPage( request ) ;
       pageBean.setItems_per_page(pageBean.getItems_per_page());
       pageBean.setCurrent_page(firstPage);
   
       try {
           List houses = null ;
       setFactoryService(); // 由于这里用了 Spring 管理事务所以就交给它去处理
           System. out .println( "**********HouseAction.java-->query***********" );
           PuppisResult pr = factoryService .getPaged(dt. house .getClass().getSimpleName(), pageBean);
      
           if (pr!= null ){
              request .setAttribute( "pagenum" ,firstPage);
              request .setAttribute( "pr" , pr);
 
           }
           houseForm.setHouses(houses);
       } catch (Exception ex){
           ex.printStackTrace();
       }
 
      
    }

这里是 setFactoyService 方法把信息丢到配置文件中去

public void setFactoryService() {
   
       Object serviceObj = ContextUtil. getContext () .getBean( "factoryService" );
       if (serviceObj!= null ){
           factoryService =(FactoryService)serviceObj;
       }
   
       this . factoryService = factoryService ;
    }

在工具包中找到静态的方法

    private static String[] SPRING_CONFIGS = {
       "applicationContext-dataSource.xml" ,
       "applicationContext-dao.xml" ,
       "applicationContext-service.xml" ,
       "applicationContext-action.xml"
    };
 
    public static ApplicationContext getContext() {
       if ( context == null ) {
           context = new ClassPathXmlApplicationContext( SPRING_CONFIGS );
       }
       return context ;
    }
 

 
 
配置文件中 applicationContext-service.xml 通过 ID 找到所引用的是那个实现

< beans default-lazy-init = "true" >
    < bean id = "factoryService" parent = "txProxyTemplate" >
       < property name = "target" >
           < bean class = "com.westsec.test.service.impl.FactoryServiceImpl" singleton = "false" >
              < property name = "factoryDAO" ref = "factoryDAO" />
           </ bean >
       </ property >
    </ bean >
</ beans >

根据配置文件 applicationContext-dao.xml 去找到 DAO

< beans default-lazy-init = "true" >
    < bean id = "factoryDAO" class = "com.westsec.test.dao.impl.FactoryDAOImpl" >
       < property name = "sessionFactory" >
           < ref bean = "sessionFactory" />
       </ property >
    </ bean >
</ beans >

数据源的配置也是在 applicationContext-dataSource.xml

< beans >
 
    <!-- 配置数据源 -->
    < bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
       < property name = "configLocation" >
           < value > classpath:hibernate.cfg.xml </ value >
       </ property >
    </ bean >
   
    <!-- 配置事务管理 -->
    < bean id = "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager" >
       < property name = "sessionFactory" >
           < ref bean = "sessionFactory" />
       </ property >
    </ bean >
   
    <!-- 定义事务代理模板 -->
    < bean id = "txProxyTemplate" abstract = "true" class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
       < property name = "transactionManager" >
           < ref bean = "transactionManager" />
       </ property >
       < property name = "transactionAttributes" >
           < props >
              < prop key = "insert*" > PROPAGATION_REQUIRED </ prop >
              < prop key = "update*" > PROPAGATION_REQUIRED </ prop >
              < prop key = "delete*" > PROPAGATION_REQUIRED </ prop >
              < prop key = "get*" > PROPAGATION_REQUIRED,readOnly </ prop >
               < prop key = "*" > PROPAGATION_REQUIRED,readOnly </ prop >
           </ props >
       </ property >
    </ bean >

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值