---
title: "SSH2整合"
author: "resembling"
date: "2010-08-09T17:36:17Z"
tags: []
categories: ["编程语言"]
slug: "ssh2"
draft: false
lastmod: "2010-08-09T17:38:08Z"
id: 733159
url: "https://resembling.iteye.com/blog/733159"
---

我这里所用的是struts-2.0.11.2,hibernate3.3.1,spring2.5.5不包版本之间可能有些包不同。

step1:首先新建一个web project项目，项目名为ssh2。

step2:首先把ssh2整合的环境搭建好再来写代码。

把所需要的JAR包复制到WebRoot/WEB-INF/lib目录下

struts2所需要的包:

struts2-core-2.0.11.jar

xwork-2.0.4.jar

ognl-2.6.11.jar

freemarker-2.3.8.jar

commons-logging-1.0.4.jar

struts2-spring-plugin-2.0.11.2.jar（这个是struts2和spring整合的包）

hibernate3.3所需要的包：

hibernate3.jar（这个是hibernate的核心包）

以及hibernate3/lib目录下所有的包复制进来，lib目录下有三个目录required，optional，bytecode其中required是hibernate必须的包，防为防止漏包，我们把另二个目录下的所有包也复制进来

Spring2.5所需要的包:

spring-framework-2.5.5\\dist目录下的spring.jar这个是spring的核心包

spring-framework-2.5.5\\lib\\aspectj\\aspectjrt.jar

spring-framework-2.5.5\\lib\\aspectj\\aspectjweaver.jar

spring-framework-2.5.5\\lib\\c3p0\\c3p0-0.9.1.2.jar

spring-framework-2.5.5\\lib\\log4j\\log4j-1.2.15.jar

spring-framework-2.5.5\\lib\\jakarta-commons\\commons-dbcp.jar

spring-framework-2.5.5\\lib\\jakarta-commons\\commons-pool.jar

spring-framework-2.5.5\\lib\\jakarta-commons\\commons-collections.jar

spring-framework-2.5.5\\lib\\jakarta-commons\\commons-logging.jar

spring-framework-2.5.5\\lib\\jakarta-commons\\commons-collections.jar

 最后别忘了把mysql的数据库驱动包引进来，这里用的是MYSQL数据库

不知道写的时候有没有写漏，可能有一些包有重复，造成有冲突，解决方法，可以先把tomcat停止，然后在进到webapps/工程所有目录下的WEB-INF/lib下查看所有引入进来的JAR包，看到有复制的就删掉，只留下最新的。  
step3:最后配置好的web.xml如下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app version="2.5"  
 xmlns="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee) "  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance) "  
 xsi:schemaLocation="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)  
 [http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd](http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd) "&gt;  
  &lt;!-- 配置Spring --&gt;  
  &lt;context-param&gt;  
   &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
   &lt;param-value&gt;  
    classpath:application-\*.xml  
   &lt;/param-value&gt;  
  &lt;/context-param&gt;  
  &lt;!-- 配置Struts2 --&gt;  
  &lt;filter&gt;  
  &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
  &lt;filter-class&gt;  
   org.apache.struts2.dispatcher.FilterDispatcher  
  &lt;/filter-class&gt;  
 &lt;/filter&gt;  
 &lt;filter-mapping&gt;  
  &lt;filter-name&gt;struts2&lt;/filter-name&gt;  
  &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
 &lt;/filter-mapping&gt;  
  &lt;!-- 配置Spring的监听 --&gt;  
  &lt;listener&gt;  
   &lt;listener-class&gt;  
    org.springframework.web.context.ContextLoaderListener  
   &lt;/listener-class&gt;  
  &lt;/listener&gt;  
  &lt;!-- 配置Spring的过滤器，解决乱码问题 --&gt;  
  &lt;filter&gt;  
   &lt;filter-name&gt;encoding&lt;/filter-name&gt;  
   &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;  
   &lt;init-param&gt;  
    &lt;param-name&gt;encoding&lt;/param-name&gt;  
    &lt;param-value&gt;UTF-8&lt;/param-value&gt;  
   &lt;/init-param&gt;  
  &lt;/filter&gt;  
  &lt;filter-mapping&gt;  
   &lt;filter-name&gt;encoding&lt;/filter-name&gt;  
   &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
  &lt;/filter-mapping&gt;  
  &lt;welcome-file-list&gt;  
    &lt;welcome-file&gt;reg.jsp&lt;/welcome-file&gt;  
  &lt;/welcome-file-list&gt;  
&lt;/web-app&gt;

src下struts.xml的最终代码如下：

&lt;?xml version="1.0" encoding="UTF-8" ?&gt;  
&lt;!DOCTYPE struts PUBLIC  
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
    "[http://struts.apache.org/dtds/struts-2.0.dtd](http://struts.apache.org/dtds/struts-2.0.dtd) "&gt;  
     
     
&lt;struts&gt;  
 &lt;package name="default" extends="struts-default"&gt;  
  &lt;action name="reg" class="reg"&gt;  
   &lt;result name="success"&gt;/regSuc.jsp&lt;/result&gt;  
   &lt;result name="error"&gt;/regFail.jsp&lt;/result&gt;  
  &lt;/action&gt;  
 &lt;/package&gt;  
&lt;/struts&gt;  
在src下把hibernate相应的配置文件复制进来，hibernate.cfg.xml最终代码如下：

&lt;?xml version='1.0' encoding='UTF-8'?&gt;  
&lt;!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "[http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd](http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd) "&gt;

&lt;!-- Generated by MyEclipse Hibernate Tools.                   --&gt;  
&lt;hibernate-configuration&gt;

 &lt;session-factory&gt;  
  &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;  
  &lt;property name="show\_sql"&gt;true&lt;/property&gt;  
  &lt;mapping resource="vo/User.hbm.xml" /&gt;  
 &lt;/session-factory&gt;  
&lt;/hibernate-configuration&gt;

把hibernate缓存的配置也复制进来ehcache.xml代码如下：

&lt;ehcache&gt;  
    &lt;diskStore path="java.io.tmpdir"/&gt;  
    &lt;defaultCache  
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"  
        /&gt;  
    &lt;cache name="sampleCache1"  
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="300"  
        timeToLiveSeconds="600"  
        overflowToDisk="true"  
        /&gt;  
    &lt;cache name="sampleCache2"  
        maxElementsInMemory="1000"  
        eternal="true"  
        timeToIdleSeconds="0"  
        timeToLiveSeconds="0"  
        overflowToDisk="false"  
        /&gt; --&gt;

&lt;/ehcache&gt;  
把hibernate下的日志文件配置复制过来log4j.properties：

\# For JBoss: Avoid to setup Log4J outside $JBOSS\_HOME/server/default/deploy/log4j.xml!  
\# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.  
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%d %p \[%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender  
log4j.appender.logfile.File=${petstore.root}/WEB-INF/petstore.log  
log4j.appender.logfile.MaxFileSize=512KB  
\# Keep three backup files.  
log4j.appender.logfile.MaxBackupIndex=3  
\# Pattern to output: date priority \[category] - message

把spring的配置文件复制过来applicationContext.xml

这里我们把他改名为：applicaton-beans.xml最后代码为：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) "  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance) "  
 xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) "  
 xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) "  
 xsi:schemaLocation="  
   [http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.5.xsd](http://www.springframework.org/schema/beans/spring-beans-2.5.xsd)  
   [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) [http://www.springframework.org/schema/aop/spring-aop-2.5.xsd](http://www.springframework.org/schema/aop/spring-aop-2.5.xsd)  
   [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-2.5.xsd](http://www.springframework.org/schema/tx/spring-tx-2.5.xsd) "&gt;  
 &lt;bean name="userDaoImpl" class="dao.impl.UserDaoImpl"  
  scope="prototype"&gt;  
  &lt;property name="sessionFactory" ref="sf"&gt;&lt;/property&gt;  
 &lt;/bean&gt;  
 &lt;bean name="reg" class="action.UserAction" scope="prototype"&gt;  
  &lt;property name="userDaoImpl" ref="userDaoImpl"&gt;&lt;/property&gt;  
 &lt;/bean&gt;  
&lt;/beans&gt;  
再建立一个事务控制的application-transaction.xml代码最终如下：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) "  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance) "  
 xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) "  
 xmlns:tx="[http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) "  
 xsi:schemaLocation="  
   [http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.5.xsd](http://www.springframework.org/schema/beans/spring-beans-2.5.xsd)  
   [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) [http://www.springframework.org/schema/aop/spring-aop-2.5.xsd](http://www.springframework.org/schema/aop/spring-aop-2.5.xsd)  
   [http://www.springframework.org/schema/tx](http://www.springframework.org/schema/tx) [http://www.springframework.org/schema/tx/spring-tx-2.5.xsd](http://www.springframework.org/schema/tx/spring-tx-2.5.xsd) "&gt;

 &lt;bean id="propertyConfigurer"  
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;  
  &lt;property name="locations"&gt;  
   &lt;list&gt;  
    &lt;value&gt;classpath:jdbc.properties&lt;/value&gt;  
   &lt;/list&gt;  
  &lt;/property&gt;  
 &lt;/bean&gt;

 &lt;!-- 配置数据源 --&gt;  
 &lt;bean id="dataSource"  
  class="org.apache.commons.dbcp.BasicDataSource"&gt;  
  &lt;property name="driverClassName"  
   value="${jdbc.driverClassName}" /&gt;  
  &lt;property name="url" value="${jdbc.url}" /&gt;  
  &lt;property name="username" value="${jdbc.username}" /&gt;  
  &lt;property name="password" value="${jdbc.password}" /&gt;  
 &lt;/bean&gt;

 &lt;!-- 配置SessionFactory --&gt;  
 &lt;bean id="sf"  
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  
  &lt;property name="dataSource" ref="dataSource" /&gt;  
  &lt;property name="configLocation"  
   value="classpath:hibernate.cfg.xml" /&gt;  
 &lt;/bean&gt;

 &lt;aop:config&gt;  
  &lt;aop:advisor pointcut="execution(* \*..PetStoreFacade.\*(..))"  
   advice-ref="txAdvice" /&gt;  
 &lt;/aop:config&gt;  
 &lt;tx:advice id="txAdvice" transaction-manager="txManager"&gt;  
  &lt;tx:attributes&gt;  
   &lt;tx:method name="insert\*" /&gt;  
   &lt;tx:method name="update\*" /&gt;  
   &lt;tx:method name="delete\*" /&gt;  
   &lt;tx:method name="modify\*" /&gt;  
   &lt;tx:method name="\*" read-only="true" /&gt;  
  &lt;/tx:attributes&gt;  
 &lt;/tx:advice&gt;  
 &lt;bean id="txManager"  
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
  &lt;property name="dataSource" ref="dataSource" /&gt;  
  &lt;/bean&gt;  
&lt;/beans&gt;

到此，所有的配置都整合好了，下面开始写代码

step4:建立相应的jsp页面

注册页面reg.jsp代码如下：

&lt;%@ page language="java" pageEncoding="UTF-8"%&gt;  
&lt;%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
  &lt;head&gt;  
    &lt;base href="&lt;%=basePath%&gt;"&gt;  
     
    &lt;title&gt;SSH2用户注册&lt;/title&gt;  
     
 &lt;meta http-equiv="pragma" content="no-cache"&gt;  
 &lt;meta http-equiv="cache-control" content="no-cache"&gt;  
 &lt;meta http-equiv="expires" content="0"&gt;     
 &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;  
 &lt;meta http-equiv="description" content="This is my page"&gt;  
 &lt;!--  
 &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;  
 --&gt;

  &lt;/head&gt;  
   
  &lt;body&gt;  
    &lt;h2&gt;用户注册&lt;/h2&gt;  
    &lt;form action="reg.action " method="post"&gt;  
     用户名：&lt;input type="text" name="user.username " /&gt;&lt;br /&gt;  
     密&nbsp;&nbsp;码：&lt;input type="password" name="user.password "&gt;&lt;br /&gt;  
     &lt;input type="submit" value="注册" /&gt;&lt;input type="reset" value="重置" /&gt;  
    &lt;/form&gt;  
  &lt;/body&gt;  
&lt;/html&gt;  
注册成功显示页面regSuc.jsp代码如下：

&lt;%@ page language="java" pageEncoding="UTF-8"%&gt;  
&lt;%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
  &lt;head&gt;  
    &lt;base href="&lt;%=basePath%&gt;"&gt;  
     
    &lt;title&gt;注册成功&lt;/title&gt;  
     
 &lt;meta http-equiv="pragma" content="no-cache"&gt;  
 &lt;meta http-equiv="cache-control" content="no-cache"&gt;  
 &lt;meta http-equiv="expires" content="0"&gt;     
 &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;  
 &lt;meta http-equiv="description" content="This is my page"&gt;  
 &lt;!--  
 &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;  
 --&gt;

  &lt;/head&gt;  
   
  &lt;body&gt;  
    恭喜你注册成功了 &lt;br&gt;  
  &lt;/body&gt;  
&lt;/html&gt;  
注册失败regFail.jsp代码如下：

&lt;%@ page language="java" pageEncoding="UTF-8"%&gt;  
&lt;%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%&gt;

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;  
&lt;html&gt;  
  &lt;head&gt;  
    &lt;base href="&lt;%=basePath%&gt;"&gt;  
     
    &lt;title&gt;注册失败&lt;/title&gt;  
     
 &lt;meta http-equiv="pragma" content="no-cache"&gt;  
 &lt;meta http-equiv="cache-control" content="no-cache"&gt;  
 &lt;meta http-equiv="expires" content="0"&gt;     
 &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;  
 &lt;meta http-equiv="description" content="This is my page"&gt;  
 &lt;!--  
 &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;  
 --&gt;

  &lt;/head&gt;  
   
  &lt;body&gt;  
   对不起注册失败 &lt;br&gt;  
  &lt;/body&gt;  
&lt;/html&gt;

在src下新建一个vo包，在vo包下新建一个User类，User.java代码如下：

package vo;

public class User {  
 private int id;  
 private String username;  
 private String password;  
 public String getUsername() {  
  return username;  
 }  
 public void setUsername(String username) {  
  this.username = username;  
 }  
 public String getPassword() {  
  return password;  
 }  
 public void setPassword(String password) {  
  this.password = password;  
 }  
 public int getId() {  
  return id;  
 }  
 public void setId(int id) {  
  this.id = id;  
 }  
   
}  
在vo包下新建hibernate的映射文件User.hbm.xml

&lt;?xml version="1.0"?&gt;  
&lt;!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "[http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd](http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd) "&gt;  
&lt;hibernate-mapping package="vo"&gt;  
 &lt;class name="User" table="t\_user"&gt;  
  &lt;id name="id"&gt;  
   &lt;generator class="native"&gt;&lt;/generator&gt;  
  &lt;/id&gt;  
  &lt;property name="username" column="username" not-null="true"/&gt;  
  &lt;property name="password" column="password" not-null="true"/&gt;  
 &lt;/class&gt;  
&lt;/hibernate-mapping&gt;

step5:编写struts2的action类，UserAction.java代码如下：

package action;

import vo.User;  
import com.opensymphony.xwork2.ActionSupport;

import dao.UserDao;

public class UserAction extends ActionSupport {

   
 private static final long serialVersionUID = -8534550171421612227L;  
 private User user;  
 private UserDao userDaoImpl;  
 public User getUser() {  
  return user;  
 }  
 public void setUser(User user) {  
  this.user = user;  
 }  
 public static long getSerialVersionUID() {  
  return serialVersionUID;  
 }  
 @Override  
 public String execute() throws Exception {  
  boolean flag = false;  
  flag = userDaoImpl.insertUser(user);  
  if(flag){  
   return SUCCESS;  
  }else{  
   return ERROR;  
  }  
 }  
 public UserDao getUserDaoImpl() {  
  return userDaoImpl;  
 }  
 public void setUserDaoImpl(UserDao userDaoImpl) {  
  this.userDaoImpl = userDaoImpl;  
 }  
}  
在src下新建一个dao包，在dao下新建一个UserDao.java

package dao;

import vo.User;

public interface UserDao {  
 public boolean insertUser(User user);  
}  
在src/dao下新建一个impl包在下面新建一个UserDao的实现类UserDaoImpl.java

package dao.impl;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import vo.User;  
import dao.UserDao;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

 public boolean insertUser(User user) {  
  boolean b = false;  
  this.getHibernateTemplate().save(user);  
  b = true;  
  return b;  
 }  
}

现在全部代码也写好了，下面看一下整个工程的目录结构：

 [![SSH2整合成功 - xiehx163 - xiehx163的博客](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_1.jpg "SSH2整合成功 - xiehx163 - xiehx163的博客")](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_1.jpg)

 所有JAR包如下图所示：

[![SSH2整合成功 - xiehx163 - xiehx163的博客](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_2.jpg "SSH2整合成功 - xiehx163 - xiehx163的博客")](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_2.jpg)

下面我们部署好测试一下，测试前得先把数据库建好，数据库名字和jdbc.properties里的jdbc.url里的数据库名字一样jdbc.url=jdbc:mysql://localhost:3306/ssh2，所以这里我们新建一个ssh2的数据库，

启动服务器进行测试：

[http://localhost:8088/ssh2/](http://localhost:8088/ssh2/)

[![SSH2整合成功 - xiehx163 - xiehx163的博客](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_3.jpg "SSH2整合成功 - xiehx163 - xiehx163的博客")](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_3.jpg)

[![SSH2整合成功 - xiehx163 - xiehx163的博客](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_4.jpg "SSH2整合成功 - xiehx163 - xiehx163的博客")](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_4.jpg)

 

[![第一次SSH2整合 - xiehx163 - xiehx163的博客](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_5.jpg "第一次SSH2整合 - xiehx163 - xiehx163的博客")](http://image4.360doc.com/DownloadImg/2009/8/20/219800_5073601_5.jpg)

我们进入到MYSQL，里面也有我们插入的数据。

到此全部整合成功了