2011年6月13日星期一

  ssh整合

1.myEclipse6.6在整合之前,首先统一一下工程编码,全面用utf-8

如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码。然而,Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简体中文操作系统 (Windows XP、Windows 2000简体中文)的缺省编码是GB18030,在此工作空间中建立的工程编码是GB18030,工程中建立的java文件也是GB18030。如果要使 新建立工程、java文件直接使UTF-8则需要做以下工作:

1)、windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右 侧Text file encoding,选择Other,改变为UTF-8,以后新建立工程其属性对话框中的Text file encoding即为UTF-8。

2)、windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Content Types,右侧Context Types树,点开Text,选择Java Source File,在下面的Default encoding输入框中输入UTF-8,点Update,则设置Java文件编码为UTF-8。其他java应用开发相关的文件如: properties、XML等已经由Eclipse缺省指定,分别为ISO8859-1,UTF-8,如开发中确需改变编码格式则可以在此指定。
       
3)、经过上述两步,新建java文件即为UTF-8编码,Eclipse编译、运行、调试都没问题,但是做RCP应用的Product输出时、或者插件 输出时,则总是出错,要么不能编译通过(输出时要重新compile)、要么输出的插件运行时中文显示乱码。此时需要再RCP应用、或插件Plugin工 程的build.properties中增加一行,javacDefaultEncoding.. = UTF-8。让输出时编译知道java源文件时UTF-8编码。这个设置需要保证所有的java源文件时UTF-8编码格式,如果不全是,可以参考 Eclipse帮中(Plug-in Development Environment Guide > Reference > Feature and Plug-in Build configuration),建议全部java源文件是UTF-8编码。

如果插件开发、RCP应用开发原来基于其他编码,如GB18030,想转换为UTF-8,则首先,做以上工作;然后通过查找编码转换工具,如基于 iconv的批量转换工具,将原编码转换为UTF-8编码,注意只转换java源文件,其他类型文件可能已经是比较合适的编码了;将原工程属性中的 Text file encoding,从原编码改为UTF-8即可。

2.新建web工程,选择j2ee5.0,增加struts2能力(已经安装好jdk1.6,tomcat6.0和mysql5,设置mysql5的编程为utf-8)
1)下载struts2.1.8复制其下的例子工程中的lib到当前工程lib下,还要从struts2.1.8的lib下找到struts2-spring-plugin-2.1.8.1.jar,也复制到lib下

struts2-spring-plugin-2.1.8.1.jar

2)在web.xml中增加struts2的支持
       
                struts2
               
                        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
               
       

       
                struts2
                /*
       
3)从struts2.1.8例子工程复制struts.xml到当前工程src下
并且在开头增加以下行
       
       

4)增加src/struts.properties,映射国际化资源文件
内容如下
struts.custom.i18n.resources=globalMessages

增加src/globalMessages_zh_CN.properties和src/globalMessages_en_US.properties等国际化资源文件,在jsp页岩通过%{getText('')}来引用,也可以在继承了ActionSupport的action中通过调用getText()方法


3.增加spring2.5能力
1)选择设置如图

点击next,设置如下:

点击完成。
applicationContext.xml生成到src下,web.xml默认是从web-inf/applicationContext.xml读取的,如果在其他地方,需要手动在web.xml中配置,多个用“,”或回车分隔。
       
                contextConfigLocation
               
                        /WEB-INF/classes/applicationContext.xml
               
       

2)在web.xml中增加spring监听器
       
               
                        org.springframework.web.context.ContextLoaderListener
               
       

3)到此,web.xml文件已经配置完成,增加hibernate能力时不需要修改,内容如下:

http://java.sun.com/xml/ns/javaee"
        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">

        Pro Name
       
       
                contextConfigLocation
               
                        /WEB-INF/classes/applicationContext.xml
               
       
       
       
       
                struts2
               
                        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
               
       

       
                struts2
                /*
       

       
               
                        org.springframework.web.context.ContextLoaderListener
               
       


       
                index.jsp
       

4.增加hibernate3.2能力
1)首先,打开myeclipse hibernate透视图,配置数据源
在db Driver中右键new,如图


next后不作任何更改,直接finish.

2)增加hibernate3.2能力
第一步设置如下:

点击next,设置如下:


继续next,设置如下:


再next,选择在myeclipse hiberbate透视图中配置的数据源,选择名称,设置如下:

next,取消create SessionFactory class,finish,如下:

此时,applicationContext.xml文件如下图:


        xmlns=" http://www.springframework.org/schema/beans"
        xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


       
                class="org.apache.commons.dbcp.BasicDataSource">
               
                        value="com.mysql.jdbc.Driver">
               
               
                        value="jdbc:mysql://localhost:3306/testdatabase">
               
               
               
       
       
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
               
                       
               
               
                       
                               
                                        org.hibernate.dialect.MySQLDialect
                               
                       
               
       


修改applicationContext.xml文件
在 标签里增加xxx.hbm.xml文件映射路径

               
               
               
                       
                                classpath:/com/hecx/bean/*.hbm.xml
                       
               

               

3)删除spring asm-2.2.3.jar,与asm.jar冲突。


4)增加元数据配置。先增加jdbc.properties文件,内容如下:
        driverClassName=com.mysql.jdbc.Driver
        url=jdbc:mysql://localhost:3306/testdatabase
        username=root
        password=root
        maxActive=100
        maxIdle=30
        maxWait=500
        defaultAutoCommit=true

        hibernate.dialect=org.hibernate.dialect.MySQLDialect
        hibernate.show_sql=true

在applicationContext.xml开头增加元数据配置
       
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
               
                       
                                classpath*:jdbc.properties
                               
                       
               
       

将bean id 为dataSource和sessionFactory中的property的value值用"${username}"形式替换,以后更改数据源配置可以在jdbc.properties进行修改

5)hibernate反向生成javaBean和DAO,只限于单表,多表的时候用jdbc处理

在mySql中新建一个表,打开myEclipse hibernate透视图,open connection,选择你建的表,右键选择MyEclipse Reverse Engineering

选择你要生成到的位置,设置要使用的sessionFactory,设置如图:


如果不指标id生成类型,可直接点击完成,否则可以next配置主键生成规则。完成。

更改将生成的文件放置到你需要的包下,更改引入的包路径,修改DAO,增加update()方法,hibernate自动生成原DAO没有此方法。

 

使用jdbc的dao,需要在新建一个接口,并实现它,如IBaseDao->BaseDaoImpl,在applicationContext.xml中配置dao(class用它的实现类),设置配置的dao的数据源为之前在applicationContext.xml中的dataSource.如
  
       
  

 


到此struts2.1.8+spring2.5+hibernate3.2整合差不多完成。

高级篇

5.增加spring事务配置
在applicationContext.xml增加

       
       
           
       
   
                class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
         
         
         
             
                PROPAGATION_REQUIRED  
                PROPAGATION_REQUIRED
                PROPAGATION_REQUIRED
                PROPAGATION_REQUIRED
               
             
       
   
     
       
           
               
                *Service
           
       
         
           
                transactionInterceptor  
           
       
   

 

配置完后,可以在里面增加service的bean配置,也不用继承什么,直接就能进行事务处理。


6.增加spring AOP能力(略,事务控制就用到AOP,其他先不管了)。

没有评论:

发表评论