• SSH 框架整合配置


       对于Struts、spring、hibernate大体上去过一遍之后,就是针对这个几个框架的整合了。对ssh框架异常熟悉、能够在1个小时搞定这些无聊的配置的程序猿,请飘过。


    整合的环境

     

    hibernate-distribution-3.6.0.Final-dist

    spring-framework-2.5.6.SEC01-with-dependencies

    struts-2.1.8.1-all

     

        环境这里没有我们值得去考究的,终归是一些工具的使用下载。另外需要说一下的是,很多时候在选择这些框架的时候总会有这样的选

    性综合征,不知道你是不是这样,反正我是。对于这个我的看法是这样的,如果进行项目开发,最好是选择比较稳定的,比如:GAdist这

    样的版本。这样的话,一个很重要的原因是版本稳定一般出现什么重大的漏洞或者兼容性问题;另一个呢是网上的各种社区、论坛的支持比

    较好。换句话说也就是出了问题,我能够尽快在度娘、google的带领下找到问题,不至于拖延项目。我如果是选择来学习的话,可以去尝试

    使用一些最新的。作为一个IT人士,去了解这些新的技术和知识是必不可少的。比如:最近jdk1.8出来了,带来了Lambda表达式、一些接口

    的配置等。个人觉得对于新知识的敏锐的触动是作为IT人必备的技能包。

     

    单独添加strutsspringhibernatejunit并测试

     

    添加struts2

     

    a)jar

    直接在struts_home/app的样例里面有相应的包,直接从这些样例里面拷贝即可。主要的jar如下,对于这些jar包。还是需要去简单熟

    悉一下还是比较好的。commons-fileupload-1.2.1.jar,主要是来支持文件上传的。commons-io-1.3.2.jar,方便文件读取的一个工具类。

    freemarker-2.3.15.jar,能够生成各种文本:HTMLXMLRTFJava源代码等等ognl-2.7.3.jar,这个主要是用来辅助上面一个

    freemarker来工作的,而且在webwork2struts2里面逐渐被用来取代EL表达式来进行数据绑定和界面显示。后面两者都是struts2的核

    心组件,xworkstruts2的基础,struts2是在xwork上来做的。一下是这些所需的组件。

     

    commons-fileupload-1.2.1.jar

    commons-io-1.3.2.jar

    freemarker-2.3.15.jar

    ognl-2.7.3.jar

    struts2-core-2.1.8.1.jar

    xwork-core-2.1.6.jar

     

    b)配置文件

     

    struts.xml


    在项目开发过程中,通常将这些xml配置文件统一放在config这样一个sourcefolder来管理。Struts.xml主要是用来配置过滤资源访问这样一个作用的。

    <?xmlversion="1.0" encoding="UTF-8" ?>
    <!DOCTYPEstruts PUBLIC
        "-//Apache Software Foundation//DTDStruts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>      
       <!-- 配置为开发模式 -->
        <constantname="struts.devMode" value="true" />
       <!-- 配置过滤器的扩展名为action -->
    <constantname="struts.action.extension" value="action" />
              <!-- 配置主题为自定义 -->
    <constantname="struts.ui.theme" value="simple" />
        <package name="default"namespace="/" extends="struts-default">
                      <actionname="test" class="testAction">
                              <!--配置返回结果为success的资源路径-->
                              <resultname="success">test.jsp</result>
                      </action>
        </package>
    </struts>


    web.xml


    不同于struts.xmlweb.xml通常至于WEBINF下面。作为整个web项目的入口,web.xml的配置十分重要。配置strutscorefilter以及后面针对spring的容器对象管理类。

    <?xmlversion="1.0" encoding="UTF-8"?>
    <web-appid="WebApp_9" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>StrutsBlank</display-name>
    <!-- 配置struts2核心过滤器 -->
    <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--配置applicationcontext對象容器 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>


     

    配置hibernate


    a)jar


    主要是核心包还有对于持久化的一些组件支持。比如:c3p0主要是对于数据库连接池的配置和使用,我在之前也有过关于连接池这块的总结。jpa就跟不用讲了,作为J2EE的十三个主要规范之一,定义了关于持久化的一套标准。

    核心包

    Hibernate3.jar

    必须包

    Require

    C3p0

    Jpa

    jdbc驱动包

    ysql-connector-java-5.1.7.zip

    b.xml

    Hibernate.cfg.xml  xx.hbm.xml(每个实体对应一个)log4j.propertiesHibernate.cfg.xml则是针对每个实体的的持久化所做的数据这块的配置,诸如连接池,数据库连接使用的用户名和密码等等。


    Hibernate.cfg.xml


    <!DOCTYPEhibernate-configuration PUBLIC
    "-//Hibernate/HibernateConfiguration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
     
    <hibernate-configuration>
    <session-factory>
    <!-- 数据源配置-->
    <!--方言配置-->
    <!--<property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->
    <!-- 数据库配置-->
    <!--<propertyname="connection.url">jdbc:mysql://localhost:3306/itcastoa1128</property>-->
    <!-- 连接驱动-->
    <!--<propertyname="connection.driver_class">com.jdbc.mysql.Driver</property>-->
    <!-- 数据库用户名-->
    <!--<property name="connection.username">root</property>-->
    <!-- 数据库密码-->
    <!--<property name="connection.password">123456</property>-->
     
    <!-- 其他配置-->
    <propertyname="show_sql">true</property>
    <!-- 每次映射数据库时是更新,而不是删除等 -->
    <propertyname="hbm2ddl.auto">update</property>
    <!-- 导入User映射文件 -->
    <!--<mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>-->
    <mappingresource="com/tgb/domain/User.hbm.xml" />
    </session-factory>
    </hibernate-configuration>
     


    xx.hbm.xml

     

    每个实体都有这样一个实体配置文件,针对类和属性的配置。


    <?xmlversion="1.0"?>
    <!DOCTYPEhibernate-mapping PUBLIC
            "-//Hibernate/Hibernate MappingDTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     
    <hibernate-mappingpackage="org.hibernate.subclassProxyInterface">
    <classname="Person">
    <idname="id" type="long">
                <generatorclass="increment"/>
    </id>
    <discriminatorcolumn="personType" type="string" />
    <propertyname="name" type="string" />
    <subclassname="Doctor" discriminator-value="doctor"proxy="IDoctor" />
    </class>
    </hibernate-mapping>
     


     

    配置spring


    a)jar        


    核心包


    Dist-spring,包含有完整发布的单个jar包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的。

     

    必须包


    commons-logging,针对日志处理的。

    aspectjrt aspectjweaver支持aopjar

    cglib-nodep-2.1_3:配合支持aopjar


    b.applicationContext.xml


    spring之处是通过beanfactory来管理对象以及对象之间的关系的,后来spring2.0的推出,更多的建议以applicationContext容器来管理.是因为applicationContext在国际化方面支持的更好,当然applicationContext也是在beanfactory的基础上去做的。

     


    <!--自动扫描与装配bean-->

    <context:component-scanbase-package="com.cfl.oa"></context:component-scan>

     

    整合struts2spring


    a)web.xml配置


    <!--配置applicationcontext對象容器 -->


    <listener>

    listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpathapplicationContext*.xml</param-value>

    </context-param>


    b)jar


    struts2-spring-plugin-2.1.8.1.jarstrus2spring的一个整合插件。

     

    整合springhibernate


    主要还是针对applicationContext.xml


    <?xmlversion="1.0"?>
    <!DOCTYPEhibernate-mapping PUBLIC
            "-//Hibernate/Hibernate MappingDTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     
    <hibernate-mappingpackage="org.hibernate.subclassProxyInterface">
    <classname="Person">
    <idname="id" type="long">
                <generatorclass="increment"/>
    </id>
    <discriminatorcolumn="personType" type="string" />
    <propertyname="name" type="string" />
    <subclassname="Doctor" discriminator-value="doctor"proxy="IDoctor" />
    </class>
    </hibernate-mapping>
     


     

        至此,配置完成,以上主要还是针对配置这块的一些总结。其实本人还是不喜欢这样的文章的,因为配置这个东西,本身没有一些东西,主要还是

    通过这些配置来理解这个框架的设计和编程的思想,我个人觉得这个才是最为重要的。顺便有一个配置好的并且有测试的搭建好的环境,分享如下

     http://pan.baidu.com/s/1mgIDEGw


  • 相关阅读:
    [Tkinter 教程12] 布局管理 (Pack Place Grid)
    python 文本全选
    CMD窗口恢复默认设置
    Python基础第一天
    Windows:安装Python2.7、3.6与共存,安装pycharm
    Linux安装
    Python input() 函数
    python的continue和pass的区别
    Python 文件读写操作实例详解
    python tkinter教程-事件绑定
  • 原文地址:https://www.cnblogs.com/guziming/p/4232663.html
Copyright © 2020-2023  润新知