---
title: "SSH整合示例"
author: "freeglad"
date: "2009-04-16T19:43:00Z"
tags: []
slug: "ssh"
draft: false
lastmod: "2009-04-16T19:43:00Z"
id: 586394
url: "https://freeglad.iteye.com/blog/586394"
---

一个简单的登录的例子，采用SSH进行整合。由于登录的例子挺简单，DAO层，Service层，Struts Action等文件就没有在此详细写出来，下面列出了整合时一些关键的配置：

**Spring配置：**  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans  
xmlns="http://www.springframework.org/schema/beans"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:aop="http://www.springframework.org/schema/aop"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt;

&lt;!-- 配置数据源 --&gt;  
&lt;bean id="dataSource"  
class="org.apache.commons.dbcp.BasicDataSource"&gt;  
&lt;property name="driverClassName"  
value="com.mysql.jdbc.Driver"&gt;  
&lt;/property&gt;  
&lt;property name="url" value="jdbc:mysql://localhost:3306/java"&gt;&lt;/property&gt;  
&lt;property name="username" value="root"&gt;&lt;/property&gt;  
&lt;property name="password" value="root"&gt;&lt;/property&gt;  
&lt;/bean&gt;  
&lt;!-- 配置Hibernate的Session工厂，注入数据源、映射文件 --&gt;  
&lt;bean id="sessionFactory"  
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  
&lt;property name="dataSource"&gt;  
&lt;ref bean="dataSource" /&gt;  
&lt;/property&gt;  
&lt;property name="hibernateProperties"&gt;  
&lt;props&gt;  
&lt;prop key="hibernate.dialect"&gt;  
org.hibernate.dialect.MySQLDialect  
&lt;/prop&gt;  
&lt;/props&gt;  
&lt;/property&gt;  
&lt;property name="mappingResources"&gt;  
&lt;list&gt;  
&lt;value&gt;com/xxh/vos/User.hbm.xml&lt;/value&gt;&lt;/list&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- 事务配置，采用AOP配置 --&gt;  
&lt;tx:advice id="myAdvice"&gt;  
&lt;tx:attributes&gt;  
&lt;tx:method name="login" read-only="true" /&gt;  
&lt;/tx:attributes&gt;  
&lt;/tx:advice&gt;

&lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref bean="sessionFactory"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;aop:config&gt;  
&lt;aop:pointcut id="daoOper" expression="execution(* com.xxh.daos.\*.\*(..))" /&gt;  
&lt;aop:advisor advice-ref="myAdvice" pointcut-ref="daoOper"/&gt;  
&lt;/aop:config&gt;

&lt;!-- DAO配置 --&gt;  
&lt;bean id="userDao" class="com.xxh.daos.UserDao"&gt;  
&lt;property name="sessionFactory"&gt;  
&lt;ref bean="sessionFactory"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- Service配置 --&gt;  
&lt;bean id="userServ" class="com.xxh.serv.UserServ"&gt;  
&lt;property name="userDao"&gt;  
&lt;ref bean="userDao" /&gt;  
&lt;/property&gt;  
&lt;/bean&gt;

&lt;!-- Struts Action配置，由Spring管理Action --&gt;  
&lt;bean name="/login" class="com.xxh.actions.LoginAction" scope="prototype" &gt;  
&lt;property name="userServ"&gt;  
&lt;ref bean="userServ"/&gt;  
&lt;/property&gt;  
&lt;/bean&gt;  
&lt;/beans&gt;

**Struts Action配置struts-config.xml：**  
&lt;action-mappings &gt;  
&lt;action path="/login" type="org.springframework.web.struts.DelegatingActionProxy"&gt;  
&lt;forward name="success" path="/success.jsp" /&gt;  
&lt;forward name="fail" path="/index.jsp" /&gt;  
&lt;/action&gt;  
&lt;/action-mappings&gt;

&lt;!-- 集成Spring插件 --&gt;  
&lt;plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"&gt;  
&lt;set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/&gt;  
&lt;/plug-in&gt;

**Web.xml文件的配置：**  
&lt;servlet&gt;  
&lt;servlet-name&gt;action&lt;/servlet-name&gt;  
&lt;servlet-class&gt;org.apache.struts.action.ActionServlet&lt;/servlet-class&gt;  
&lt;init-param&gt;  
&lt;param-name&gt;config&lt;/param-name&gt;  
&lt;param-value&gt;/WEB-INF/struts-config.xml&lt;/param-value&gt;  
&lt;/init-param&gt;  
&lt;init-param&gt;  
&lt;param-name&gt;debug&lt;/param-name&gt;  
&lt;param-value&gt;3&lt;/param-value&gt;  
&lt;/init-param&gt;  
&lt;init-param&gt;  
&lt;param-name&gt;detail&lt;/param-name&gt;  
&lt;param-value&gt;3&lt;/param-value&gt;  
&lt;/init-param&gt;  
&lt;load-on-startup&gt;0&lt;/load-on-startup&gt;  
&lt;/servlet&gt;  
&lt;servlet-mapping&gt;  
&lt;servlet-name&gt;action&lt;/servlet-name&gt;  
&lt;url-pattern&gt;\*.do&lt;/url-pattern&gt;  
&lt;/servlet-mapping&gt;