SSH2开发

1. 下面给出整合开发时Struts 2、 Hibernate、Spring需要的JAR。

<1> struts2-core-2.x.x.jar :Struts 2框架的核心类库

xwork-2.x.x.jar :XWork类库,Struts 2在其上构建

ognl-2.6.x.jar :对象图导航语言(Object Graph Navigation Language),struts2框架通过其读写对象的属性

freemarker-2.3.x.jar :Struts 2的UI标签的模板使用FreeMarker编写

commons-fileupload-1.2.x.jar 文件上传组件,2.1.6版本后需要加入此文件

struts2-spring-plugin-2.x.x.jar :用于struts2集成Spring的插件

<2> hibernate核心安装包下的(下载路径:http://www.hibernate.org/,点击“Hibernate Core”右边的“Downloads”):

hibernate3.jar

lib\bytecode\cglib\hibernate-cglib-repack-2.1_3.jar

lib\required\*.jar

<3> hibernate 注解安装包下的(下载路径:www.hibernate.org,点击“Hibernate Annotations”右边的“Downloads”):

hibernate-annotations.jar

lib\ejb3-persistence.jar、hibernate-commons-annotations.jar

<4> Hibernate针对JPA的实现包(下载路径:www.hibernate.org,点击“Hibernate Entitymanager”右边的“Downloads”):

hibernate-entitymanager.jar

lib\test\log4j.jar、slf4j-log4j12.jar

<5> Spring安装包下的

dist\spring.jar

lib\c3p0\c3p0-0.9.1.2.jar   lib\aspectj\aspectjweaver.jar、aspectjrt.jar    lib\cglib\cglib-nodep-2.1_3.jar

lib\j2ee\common-annotations.jar     lib\log4j\log4j-1.2.15.jar      lib\jakarta-commons\commons-logging.jar

<6> MYSQL数据库驱动jar


2. 在源文件夹下新建Spring配置文件beans.xml,在配置文件中配置c3p0数据源:

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

       <property name="driverClass" value="org.gjt.mm.mysql.Driver"/>

       <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/itcast?useUnicode=true&amp;characterEncoding=UTF-8"/>

       <property name="user" value="root"/>

       <property name="password" value="123456"/>

       <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->

       <property name="initialPoolSize" value="1"/>

       <!--连接池中保留的最小连接数。-->

       <property name="minPoolSize" value="1"/>

       <!--连接池中保留的最大连接数。Default: 15 -->

       <property name="maxPoolSize" value="300"/>

       <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->

       <property name="maxIdleTime" value="60"/>    

       <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->

       <property name="acquireIncrement" value="5"/>

       <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->

       <property name="idleConnectionTestPeriod" value="60"/>

</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

 ..... 略 </bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

     <property name="dataSource" ref="dataSource"/>

     <property name="mappingResources">

           <list>

             <value>cn/itcast/bean/Person.hbm.xml</value>

           </list>

     </property>

     <property name="hibernateProperties">

           <value>

               hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

               hibernate.hbm2ddl.auto=update

               hibernate.show_sql=false

               hibernate.format_sql=false

             </value>

    </property>

</bean>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

      <property name="sessionFactory" ref="sessionFactory"/>

</bean>

<tx:annotation-driven transaction-manager="txManager"/>


3. 实体bean配置模版.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.bean">

    <class name="Person" table="person">

        <id name="id" type="integer" >

            <generator class="native"/>

        </id>

        <property name="name" length="10" not-null="true"/>

    </class>

</hibernate-mapping>


4. 在web容器中实例化spring容器和配置struts2

<1> <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->

<context-param>

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

   <param-value>classpath:beans.xml</param-value>

</context-param>

<!-- 对Spring容器进行实例化 -->

<listener>

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

</listener>

<2> 配置struts2

   <filter>

        <filter-name>struts2</filter-name>

        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

   </filter-mapping>

<3> struts2的配置文件模版struts.xml如下。常量struts.objectFactory=spring明确指出将由Spring负责创建Action实例。

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

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

<struts>

<!-- 默认的视图主题 -->

    <constant name="struts.ui.theme" value="simple" />

    <constant name="struts.objectFactory" value="spring" />

      <package name="person" namespace="/person" extends="struts-default">

             <global-results>

                    <result name="message">/WEB-INF/page/message.jsp</result>

             </global-results>

              <action name="action_*" class="personList" method="{1}">

                     <result name="list">/WEB-INF/page/persons.jsp</result>

                     <result name="add">/WEB-INF/page/add_person.jsp</result>

                     <result name="edit">/WEB-INF/page/edit_person.jsp</result>

              </action>

    </package>

</struts>


5. 为了能从spring容器中寻找到Action bean,要求action配置的class属性值和spring中bean的名称相同。如下:

<package name="itcast" namespace="/person" extends="struts-default">

       <action name="list" class="personListAction">

                         .....

       </action>

</package> 

posted @ 2009-09-05 10:07  六不朽  阅读(1082)  评论(0编辑  收藏  举报