SSH2整合开发 简单示例的详细过程

SSH2整合开发 简单示例的详细过程

 

一、spring hibernate struts2框架的搭建

1、  首先将struts2jar文件,基本的6个,加上一个iojar。一共7个。如果在struts2.2版本,一定要多加一个jar文件,javassist-3.7.ga.jar

2、  修改xml文件,加入struts2的过滤器。

Web.xml配置如下:

    <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>

3、  增加struts.xml

struts.xml配置如下:

<?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.devMode" value="true" />

<struts>

 

4、  加载struts对应spring的插件,struts2-spring-plugin-2.2.1.1.jar

5、  增加hibernate支持。其中支持的jar文件要拷贝到lib目录下。如果出现jar文件重名,请替换或者保存

6、  加入spirng支持 aop core persistence  web 四个大类

7、  讲加入的jar拷贝到lib目录下

8、  Spring的配置文件applicationContext.xml选择放在web-inf目录下

9、  web.xml中配置spring监听器,管理sessionFactory的创建。  

applicationContext.xml配置如下: 

<listener>

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

</listener>                                

10、  加载数据库相关的3jar文件,两个dbcp连接池的,一个数据库驱动jar文件。

11、  applicationContext.xml添加代码,管理hibernate sessionFactory的创建。

applicationContext.xml配置如下:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

       destroy-method="close">

       <property name="driverClassName">

           <value>oracle.jdbc.driver.OracleDriver</value>

       </property>

       <property name="url">

           <value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>

       </property>

       <property name="username">

           <value>scott</value>

       </property>

       <property name="password">

           <value>tiger</value>

       </property>

    </bean>

 

    <bean id="sessionFactory"

       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

       <property name="dataSource">

           <ref local="dataSource" />

       </property>

       <property name="mappingResources">

           <list>

              <value>com/dw/model/Users.hbm.xml</value>

           </list>

       </property>

       <property name="hibernateProperties">

           <props>

              <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>

              <prop key="hibernate.show_sql">true</prop>

              <prop key="hibernate.format_sql">true</prop>

              <prop key="hibernate.current_session_context_class">thread</prop>

           </props>

 

       </property>

    </bean>

 

~~~~~~~~~~~~~~~~~~~~~~启动tomcat 验证配置是否成功~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~如果出现jar文件冲突 请删除重复的一个 保留一个~~~~~~~~~~~

 

二、建议一个数据库网页的CRUD操作模型

1、  有横向开发和纵向开发两种模式 可以选择。下面以纵向开发为例子。

2、  建立数据库表格

3、  逆向工程建立model。包名:com.test.model

4、  建立服务层和服务层实现类。包名分别为:com.test.servicecom.test.service.impl

5、  建立DAODAO的实现类。包名非别为:com.test.DAOcom.test.DAO.impl

~~~~~~~~~~~~打开你的spring explorer进行观察(我称之为豆豆图 哈哈)~~~~~~~~~~

6、  struts2中完成action的编写,注意防止表单重复提交的问题。使用不同的type

~~~~~~~~~~~~~~~~~~~~~~此阶段可以停下来测试一下下~~~~~~~~~~~~~~~~~~~~~

7、  spring中书写各种bean。特别注意beanscope属性。一般与action相关的bean都要考虑是否需要设置为prototype。使用事务代理。

8、  修改页面需要2action来完成 一个是显示,一个是真正的提交

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

三、注意的地方

1、  strutsclass类让 spring来生成 不要顺手写成让struts负责了

2、  注意页面传递对象通过ActionContext.getContext().get("request")拿到request对象

3、  注意防止表单重复提交,合理使用重定向和请求转发

4、  至于设置springactionscope属性,一般action类的要考虑是否应该使用prototype

5、  关于spring中事务的代码举例:

transactionManager bean

   <bean id="transactionManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

       <property name="sessionFactory">

           <ref local="sessionFactory" />

       </property>

    </bean>

目标类:userServiceTarget

    <bean id="userServiceTarget" class="com.dw.service.impl.UserServiceImpl">

       <property name="userDao">

           <ref local="userDao" />

       </property>

    </bean>

代理类:userService

    <bean id="userService"

       class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

       <property name="target">

           <ref local="userServiceTarget" />

       </property>

       <property name="transactionManager">

           <ref local="transactionManager" />

       </property>

       <property name="transactionAttributes">

           <props>

              <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>

              <prop key="*">PROPAGATION_REQUIRED</prop>

           </props>

       </property>

    </bean>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值