SSH2整合开发 简单示例的详细过程
一、spring hibernate struts2框架的搭建
1、 首先将struts2的jar文件,基本的6个,加上一个io的jar。一共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、 加载数据库相关的3个jar文件,两个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.service和com.test.service.impl
5、 建立DAO和DAO的实现类。包名非别为:com.test.DAO和com.test.DAO.impl
~~~~~~~~~~~~打开你的spring explorer进行观察(我称之为豆豆图 哈哈)~~~~~~~~~~
6、 在struts2中完成action的编写,注意防止表单重复提交的问题。使用不同的type
~~~~~~~~~~~~~~~~~~~~~~此阶段可以停下来测试一下下~~~~~~~~~~~~~~~~~~~~~
7、 在spring中书写各种bean。特别注意bean的scope属性。一般与action相关的bean都要考虑是否需要设置为prototype。使用事务代理。
8、 修改页面需要2个action来完成 一个是显示,一个是真正的提交
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~测试~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
三、注意的地方
1、 struts中class类让 spring来生成 不要顺手写成让struts负责了
2、 注意页面传递对象通过ActionContext.getContext().get("request")拿到request对象
3、 注意防止表单重复提交,合理使用重定向和请求转发
4、 至于设置spring的action的scope属性,一般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>