<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/struts-spring-hibernate/</link>
<title><![CDATA[手动整合Struts1.3+Spring2.5+Hibernate3]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Tue, 24 Aug 2010 15:30:39 +0000</pubDate> 
<guid>https://www.heckjj.com/struts-spring-hibernate/</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-family: 微软雅黑;">下面来谈下手动整合Struts1.3+Spring2.5+Hibernate3框架的方法，按步骤如下：<br/>1.首先准备好整合用的jar包 <br/>&nbsp;&nbsp;&nbsp;&nbsp; Hibernate的jar包:hibernate3.jar、lib&#92;required下的所有jar包(共6个)、lib&#92;optional&#92;ehche下的ehcache-1.2.3.jar、slf4j-nop-1.5.2.jar共9个包 <br/>&nbsp;&nbsp;&nbsp;&nbsp; Spring的jar包:dist&#92;spring.jar、lib&#92;jakarta-commons下的commons-logging.jar、commons-dbcp.jar、commons-pool.jar、lib&#92;aspectj下的两个jar包、lib&#92;cglib&#92;cglib-nodep-2.1_3.jar、lib&#92;j2ee下的common-annotations.jar、lib&#92;log4j&#92;log4j-1.2.15.jar、dist&#92;modules&#92;spring-webmvc-struts.jar共10个包 <br/>&nbsp;&nbsp;&nbsp;&nbsp; Struts的jar包:lib下面的包除antlr-2.7.6.jar(由于Spring中已经存在一个antlr-2.7.6.jar,所以把Struts中的antlr-2.7.2.jar删除，避免jar冲突)外，全部都要，共19个 <br/>&nbsp;&nbsp;&nbsp;&nbsp; 另外还有数据库驱动包,我用的是MySQL数据库,mysql-connector-java-5.0.5-bin.jar&nbsp;&nbsp;<br/></span><br/><span style="font-family: 微软雅黑;"><br/>2.在创建好的WEB项目中引入Hibernate、Spring、Struts以及数据库驱动等所必须的jar包<br/><br/>3.先整合Spring和Hibernate,在SpringXML配置文件中配置数据源</span><br/><textarea name="code" class="xml" rows="15" cols="100">
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
&nbsp;&nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
&nbsp;&nbsp;xmlns:context="http://www.springframework.org/schema/context"
&nbsp;&nbsp;xmlns:aop="http://www.springframework.org/schema/aop"
&nbsp;&nbsp;xmlns:tx="http://www.springframework.org/schema/tx"
&nbsp;&nbsp;xsi:schemaLocation="http://www.springframework.org/schema/beans
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://www.springframework.org/schema/context
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://www.springframework.org/schema/context/spring-context-2.5.xsd
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://www.springframework.org/schema/aop
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://www.springframework.org/schema/tx
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
&nbsp;&nbsp;<!-- 配置数据源 -->
&nbsp;&nbsp;<bean id="dataSource"
&nbsp;&nbsp;&nbsp;&nbsp;class="org.apache.commons.dbcp.BasicDataSource"
&nbsp;&nbsp;&nbsp;&nbsp;destroy-method="close">
&nbsp;&nbsp;&nbsp;&nbsp;<property name="driverClassName"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value="org.gjt.mm.mysql.Driver" />
&nbsp;&nbsp;&nbsp;&nbsp;<!--&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;是XML支持的&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;符号，在XML文件中一定记得转义-->
&nbsp;&nbsp;&nbsp;&nbsp;<property name="url"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value="jdbc:mysql://localhost:3306/czcspring?useUnicode=true&amp;characterEncoding=UTF-8" />
&nbsp;&nbsp;&nbsp;&nbsp;<property name="username" value="root" />
&nbsp;&nbsp;&nbsp;&nbsp;<property name="password" value="netjava" />
&nbsp;&nbsp;&nbsp;&nbsp;<!--连接池启动时的初始值-->
&nbsp;&nbsp;&nbsp;&nbsp;<property name="initialSize" value="1" />
&nbsp;&nbsp;&nbsp;&nbsp;<!--连接池的最大值-->
&nbsp;&nbsp;&nbsp;&nbsp;<property name="maxActive" value="500" />
&nbsp;&nbsp;&nbsp;&nbsp;<!--最大空闲值，当经过一个高峰时间后，连接池可以慢慢将已经用不到的链接慢慢释放一部分，一直减少到maxIdle为止-->
&nbsp;&nbsp;&nbsp;&nbsp;<property name="maxIdle" value="2" />
&nbsp;&nbsp;&nbsp;&nbsp;<!--最小空闲值，当空闲的链接数少于阀值时，连接池就会预申请去一些链接，以免洪峰来时来不及申请-->
&nbsp;&nbsp;&nbsp;&nbsp;<property name="minIdle" value="1" />
&nbsp;&nbsp;</bean>
&nbsp;&nbsp;<context:annotation-config />
</beans>
</textarea><br/>4.在SpringXML配置文件中配置Hibernate的SessionFacotry<br/><textarea name="code" class="xml" rows="15" cols="100">
<!-- 用于定义SessionFactory对象，对象在容器中是一个单例的形式 -->
&nbsp;&nbsp;<bean id="sessionFactory"
&nbsp;&nbsp;&nbsp;&nbsp;class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
&nbsp;&nbsp;&nbsp;&nbsp;<!-- 数据源属性 -->
&nbsp;&nbsp;&nbsp;&nbsp;<property name="dataSource" ref="dataSource" />
&nbsp;&nbsp;&nbsp;&nbsp;<!-- hibernate实体bean的映射文件,可以配置有多个映射文件 -->
&nbsp;&nbsp;&nbsp;&nbsp;<property name="mappingResources">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<list>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<value>cn/netjava/bean/Userinfo.hbm.xml</value>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</list>
&nbsp;&nbsp;&nbsp;&nbsp;</property>
&nbsp;&nbsp;&nbsp;&nbsp;<!--用来配置hibernate的属性信息-->
&nbsp;&nbsp;&nbsp;&nbsp;<property name="hibernateProperties">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<value>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hibernate.hbm2ddl.auto=update hibernate.show_sql=false
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hibernate.format_sql=false
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</value>
&nbsp;&nbsp;&nbsp;&nbsp;</property>
&nbsp;&nbsp;</bean>
</textarea><br/><br/>5.SpringXML配置文件中配置事务管理 <br/><textarea name="code" class="xml" rows="15" cols="100">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!-- 配置事务管理 -->
&nbsp;&nbsp;<bean id="txManager"
&nbsp;&nbsp;class="org.springframework.orm.hibernate3.HibernateTransactionManager">
&nbsp;&nbsp;&nbsp;&nbsp;<property name="sessionFactory" ref="sessionFactory"></property>
&nbsp;&nbsp;</bean>
&nbsp;&nbsp;<!--采用@Transaction注解方式使用事务 事务管理器由txManager配置好的-->
&nbsp;&nbsp;<tx:annotation-driven transaction-manager="txManager" />
</textarea><br/>6.建立user表的pojo类 <br/><textarea name="code" class="java" rows="15" cols="100">
package cn.netjava.pojo;

public class Userinfo &#123;
&nbsp;&nbsp;private Integer id;
&nbsp;&nbsp;private String name;
&nbsp;&nbsp;private Integer age;

&nbsp;&nbsp;public Userinfo() &#123;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public Userinfo(String name) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.name = name;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public Integer getId() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;return id;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public void setId(Integer id) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.id = id;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public String getName() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;return name;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public void setName(String name) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.name = name;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public Integer getAge() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;return age;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public void setAge(Integer age) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;this.age = age;
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;
&nbsp;&nbsp;public String toString() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;return "id: " + id + "&nbsp;&nbsp;name: " + name + " age: " + age;
&nbsp;&nbsp;&#125;
&#125;</textarea><br/>7.实体Bean配置Userinfo.hbm.xml<br/><textarea name="code" class="xml" rows="15" cols="100">
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
&nbsp;&nbsp;"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
&nbsp;&nbsp;"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
&nbsp;&nbsp;<!-- 这里一定要指明不延迟加载才行 -->
&nbsp;&nbsp;<class name="cn.netjava.pojo.Userinfo" table="user" lazy="false">
&nbsp;&nbsp;&nbsp;&nbsp;<id name="id" column="id">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<generator class="native" />
&nbsp;&nbsp;&nbsp;&nbsp;</id>
&nbsp;&nbsp;&nbsp;&nbsp;<property name="name" column="name" length="20"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;not-null="true">
&nbsp;&nbsp;&nbsp;&nbsp;</property>
&nbsp;&nbsp;&nbsp;&nbsp;<property name="age" column="age" not-null="true"></property>
&nbsp;&nbsp;</class>
</hibernate-mapping>
</textarea><br/>8.创建业务层服务接口UserinfoService <br/><textarea name="code" class="java" rows="15" cols="100">
package cn.netjava.service;

import java.util.List;

import cn.netjava.pojo.Userinfo;

public interface UserinfoService &#123;
&nbsp;&nbsp;/**
&nbsp;&nbsp; * 保存Userinfo
&nbsp;&nbsp; * 
&nbsp;&nbsp; * @param user
&nbsp;&nbsp; */
&nbsp;&nbsp;public void save(Userinfo user);

&nbsp;&nbsp;/**
&nbsp;&nbsp; * 更新Userinfo
&nbsp;&nbsp; * 
&nbsp;&nbsp; * @param user
&nbsp;&nbsp; */
&nbsp;&nbsp;public void update(Userinfo user);

&nbsp;&nbsp;/**
&nbsp;&nbsp; * 获取Userinfo
&nbsp;&nbsp; * 
&nbsp;&nbsp; * @param userId
&nbsp;&nbsp; * @return
&nbsp;&nbsp; */
&nbsp;&nbsp;public Userinfo getUserinfo(Integer userId);

&nbsp;&nbsp;/**
&nbsp;&nbsp; * 获取所有Userinfo
&nbsp;&nbsp; * 
&nbsp;&nbsp; * @return
&nbsp;&nbsp; */
&nbsp;&nbsp;public List<Userinfo> getUserins();

&nbsp;&nbsp;/**
&nbsp;&nbsp; * 删除指定id的Userinfo
&nbsp;&nbsp; * 
&nbsp;&nbsp; * @param userId
&nbsp;&nbsp; */
&nbsp;&nbsp;public void delete(Integer userId);
&#125;
</textarea><br/>9.建立业务层服务类UserinfoServiceBean <br/><textarea name="code" class="java" rows="15" cols="100">
package cn.netjava.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.netjava.pojo.Userinfo;
import cn.netjava.service.UserinfoService;

//spring xml文件中配置事务时采用注解方式进行配置的，所以在此处我们要加上@Transaction
@Transactional
public class UserinfoServiceBean implements UserinfoService &#123;
&nbsp;&nbsp;// 使用SpringIOC依赖注入的方式注入SessionFactory对象
&nbsp;&nbsp;@Resource
&nbsp;&nbsp;private SessionFactory sessionFactory;

&nbsp;&nbsp;public void delete(Integer userId) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;sessionFactory.getCurrentSession()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.delete(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sessionFactory.getCurrentSession().load(Userinfo.class,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userId));
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("用户删除成功");
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
&nbsp;&nbsp;public Userinfo getUserinfo(Integer userId) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;return (Userinfo) sessionFactory.getCurrentSession().load(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Userinfo.class, userId);
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
&nbsp;&nbsp;@SuppressWarnings("unchecked")
&nbsp;&nbsp;// 终止警告
&nbsp;&nbsp;public List<Userinfo> getUserins() &#123;
&nbsp;&nbsp;&nbsp;&nbsp;//这个hql语句里面的Userinfo不是表名，而是pojo类名字
&nbsp;&nbsp;&nbsp;&nbsp;return sessionFactory.getCurrentSession().createQuery("from Userinfo")
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.list();
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public void save(Userinfo user) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;// 调用persist方法进行保存
&nbsp;&nbsp;&nbsp;&nbsp;sessionFactory.getCurrentSession().persist(user);
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("用户保存成功");
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public void update(Userinfo user) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;// 从Spring容器中得到当前被Spring容器管理的Session对象 调用merge方法进行更新。
&nbsp;&nbsp;&nbsp;&nbsp;sessionFactory.getCurrentSession().merge(user);
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("用户信息更新成功");
&nbsp;&nbsp;&#125;
&#125;
</textarea><br/><br/>最后把下面这代码加入到SpringXML配置文件中去<br/><textarea name="code" class="xml" rows="15" cols="100">
<bean id="userinfoService"
&nbsp;&nbsp;&nbsp;&nbsp; class="cn.netjava.service.impl.UserinfoServiceBean">
</bean>
</textarea><br/>10.对业务层UserinfoServiceBean的每个方法进行单元测试 <br/><textarea name="code" class="java" rows="15" cols="100">
package cn.netjava.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.netjava.pojo.Userinfo;
import cn.netjava.service.UserinfoService;

public class TestUtil &#123;
&nbsp;&nbsp;private static UserinfoService userinfoService;

&nbsp;&nbsp;public static void setUpBeforeClass() throws Exception &#123;
&nbsp;&nbsp;&nbsp;&nbsp;ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"spring.xml");
&nbsp;&nbsp;&nbsp;&nbsp;userinfoService = (UserinfoService) applicationContext
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getBean("userinfoService");
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;public static void main(String args[]) throws Exception &#123;
&nbsp;&nbsp;&nbsp;&nbsp;setUpBeforeClass();
&nbsp;&nbsp;&nbsp;&nbsp;List<Userinfo> userlist = userinfoService.getUserins();
&nbsp;&nbsp;&nbsp;&nbsp; for (Userinfo u : userlist) &#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(u.toString());
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;
&#125;
</textarea><br/><br/>测试结果如下:<br/><a href="https://www.heckjj.com/attachment.php?fid=23" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=23" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a>到此已经将Spring与Hibernate整合成功,接下来再整合Struts <br/>11.在Web容器中配置Struts，即在web.xml中配置 <br/><textarea name="code" class="xml" rows="15" cols="100">
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
&nbsp;&nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
&nbsp;&nbsp;xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
&nbsp;&nbsp;http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

&nbsp;&nbsp;<!-- 配置struts -->
&nbsp;&nbsp;<servlet>
&nbsp;&nbsp;&nbsp;&nbsp;<servlet-name>struts</servlet-name>
&nbsp;&nbsp;&nbsp;&nbsp;<servlet-class>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org.apache.struts.action.ActionServlet
&nbsp;&nbsp;&nbsp;&nbsp;</servlet-class>
&nbsp;&nbsp;&nbsp;&nbsp;<init-param>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<param-name>config</param-name>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<param-value>/WEB-INF/struts-config.xml</param-value>
&nbsp;&nbsp;&nbsp;&nbsp;</init-param>
&nbsp;&nbsp;&nbsp;&nbsp;<load-on-startup>0</load-on-startup>
&nbsp;&nbsp;</servlet>

&nbsp;&nbsp;<servlet-mapping>
&nbsp;&nbsp;&nbsp;&nbsp;<servlet-name>struts</servlet-name>
&nbsp;&nbsp;&nbsp;&nbsp;<url-pattern>*.do</url-pattern>
&nbsp;&nbsp;</servlet-mapping>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<welcome-file-list>
&nbsp;&nbsp;&nbsp;&nbsp;<welcome-file>index.jsp</welcome-file>
&nbsp;&nbsp;</welcome-file-list>
</web-app>
</textarea><br/>12.在Web容器中实例化Spring容器,在web.xml中配置如下<br/><textarea name="code" class="xml" rows="15" cols="100">
<!-- 制定Spring配置文件,默认从Web根目录寻找配置文件, -->
&nbsp;&nbsp;<context-param>
&nbsp;&nbsp;&nbsp;&nbsp;<param-name>contextConfigLocation</param-name>
&nbsp;&nbsp;&nbsp;&nbsp;<param-value>classpath:spring.xml</param-value>
&nbsp;&nbsp;</context-param>
&nbsp;&nbsp;<!-- 在Web容器中实例化Spring容器,并将实例放入Application范围内 -->
&nbsp;&nbsp;<listener>
&nbsp;&nbsp;&nbsp;&nbsp;<listener-class>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;org.springframework.web.context.ContextLoaderListener
&nbsp;&nbsp;&nbsp;&nbsp;</listener-class>
&nbsp;&nbsp;</listener>
</textarea><br/>13.建立Struts的Action文件UserinfoAction <br/><textarea name="code" class="java" rows="15" cols="100">
package cn.netjava.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.netjava.pojo.Userinfo;
import cn.netjava.service.UserinfoService;

public class UserinfoAction extends Action &#123;
&nbsp;&nbsp;
&nbsp;&nbsp;public ActionForward execute(ActionMapping mapping, ActionForm form,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletRequest request, HttpServletResponse response)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throws Exception &#123;
&nbsp;&nbsp;&nbsp;&nbsp; // 取得被放置在Application范围里边的Spring容器实例
&nbsp;&nbsp;&nbsp;&nbsp;WebApplicationContext ctx = WebApplicationContextUtils
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getWebApplicationContext(this.servlet.getServletContext());
&nbsp;&nbsp;&nbsp;&nbsp;// 通过Spring容器得到业务bean实例
&nbsp;&nbsp;&nbsp;&nbsp;UserinfoService userinfoService = (UserinfoService) ctx
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.getBean("userinfoService");
&nbsp;&nbsp;&nbsp;&nbsp;List<Userinfo> userinfoList = userinfoService.getUserins();
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("得到总数：" + userinfoList.size());
&nbsp;&nbsp;&nbsp;&nbsp;request.setAttribute("userinfoList", userinfoList);
&nbsp;&nbsp;&nbsp;&nbsp;return mapping.findForward("list");
&nbsp;&nbsp;&#125;
&#125;
</textarea><br/><br/>14.建立Struts配置文件struts-config.xml,把UserinfoAction配置好 <br/><textarea name="code" class="xml" rows="15" cols="100">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>

&nbsp;&nbsp;<action-mappings>
&nbsp;&nbsp;&nbsp;&nbsp;<action path="/userinfo" type="cn.netjava.action.UserinfoAction" scope="request">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<forward name="list" path="/userinfoList.jsp" />
&nbsp;&nbsp;&nbsp;&nbsp;</action>
&nbsp;&nbsp;</action-mappings>
</struts-config>
</textarea><br/>15.创建一个显示查询结果的userinfoList.jsp页面<br/><div class="code"><br/>&lt;%@ page language=&quot;java&quot; import=&quot;java.util.*&quot; pageEncoding=&quot;utf-8&quot;%&gt;<br/>&lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&gt;<br/>&lt;%<br/>&nbsp;&nbsp;String path = request.getContextPath();<br/>&nbsp;&nbsp;String basePath = request.getScheme() + &quot;://&quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ request.getServerName() + &quot;:&quot; + request.getServerPort()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ path + &quot;/&quot;;<br/>%&gt;<br/><br/>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;<br/>&lt;html&gt;<br/>&nbsp;&nbsp;&lt;head&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;用户信息列表&lt;/title&gt;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;pragma&quot; content=&quot;no-cache&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;cache-control&quot; content=&quot;no-cache&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;expires&quot; content=&quot;0&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;keywords&quot; content=&quot;keyword1,keyword2,keyword3&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;meta http-equiv=&quot;description&quot; content=&quot;This is my page&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;!--<br/>&nbsp;&nbsp;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles.css&quot;&gt;<br/>&nbsp;&nbsp;--&gt;<br/><br/>&nbsp;&nbsp;&lt;/head&gt;<br/><br/>&nbsp;&nbsp;&lt;body&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;h1&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test Reslut<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/h1&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:forEach items=&quot;$&#123;userinfoList&#125;&quot; var=&quot;user&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ID=$&#123;user.id&#125;,Name=$&#123;user.name&#125;,Age=$&#123;user.age&#125;&lt;br&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c:forEach&gt;<br/>&nbsp;&nbsp;&lt;/body&gt;<br/>&lt;/html&gt;<br/></div><br/>页面显示结果如下：<br/><a href="https://www.heckjj.com/attachment.php?fid=24" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=24" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>虽然已成功将SSH整合成功了，但有一点缺陷的地方就是在Action每次要取得Spring容器时，都要通过<br/><textarea name="code" class="java" rows="15" cols="100">
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.servlet.getServletContext());
</textarea><br/><br/>去获取Spring容器实例,其实我们可以通过Spring的依赖注入原理省去其中的步骤 <br/><br/>16.使用SpringIOC依赖注入管理Struts的Action <br/>把struts.cfg.xml的内容改为如下形式： <br/><div class="code"><br/>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br/>&lt;!DOCTYPE struts-config PUBLIC &quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//EN&quot; &quot;http://struts.apache.org/dtds/struts-config_1_2.dtd&quot;&gt;<br/>&lt;struts-config&gt;<br/>&nbsp;&nbsp;&lt;!--采用SpringIOC的依赖注入，此处的type可以不配置。<br/>&nbsp;&nbsp;&nbsp;&nbsp;如果配置type，请求处理器会在Spring容器寻找不到的Bean实例情况下，<br/>&nbsp;&nbsp;&nbsp;&nbsp;会交给Struts进行处理，Struts会创建该对象并放入缓存中。<br/>&nbsp;&nbsp;&nbsp;&nbsp;建议：如果使用了Spring的依赖注入，type也就无需要配置了。 <br/>&nbsp;&nbsp;--&gt;<br/>&nbsp;&nbsp;&lt;action-mappings&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;action path=&quot;/userinfo&quot; scope=&quot;request&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;forward name=&quot;list&quot; path=&quot;/userinfoList.jsp&quot; /&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/action&gt;<br/>&nbsp;&nbsp;&lt;/action-mappings&gt;<br/>&nbsp;&nbsp;&lt;!-- 配置spring的请求控制器 --&gt;<br/>&nbsp;&nbsp;&lt;!-- <br/>&nbsp;&nbsp;请求路径是/userinfo.do<br/>&nbsp;&nbsp;首先交给ActionServlet处理，接下来交给Spring的请求处理器来处理<br/>&nbsp;&nbsp;Spring的请求处理器的工作就是：<br/>&nbsp;&nbsp;请求处理器会根据用户请求的路径(/userinfo)，到Spring容器中寻找和名称匹配的Bean，<br/>&nbsp;&nbsp;寻找到之后就采用Bean实例处理用户的请求。<br/>&nbsp;&nbsp;--&gt;<br/>&nbsp;&nbsp;&lt;controller&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;set-property property=&quot;processorClass&quot; value=&quot;org.springframework.web.struts.DelegatingRequestProcessor&quot;&gt;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&lt;/set-property&gt;<br/>&nbsp;&nbsp;&lt;/controller&gt;<br/>&lt;/struts-config&gt;<br/></div><br/><br/>将下面这行代码加到SpringXML配置文件中去 <br/><textarea name="code" class="xml" rows="15" cols="100">
&nbsp;&nbsp;<!-- 配置struts中处理bean的Action-->
&nbsp;&nbsp;<bean name="/userinfo" class="cn.netjava.action.UserinfoAction"></bean>
</textarea><br/>注意:一定要确保action的path属性值与bean的名称相同,尤其别掉了"/" <br/><br/>在UserinfoAction中可以写成如下形式: <br/><textarea name="code" class="java" rows="15" cols="100">
package cn.netjava.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.netjava.pojo.Userinfo;
import cn.netjava.service.UserinfoService;

public class UserinfoAction extends Action &#123;

&nbsp;&nbsp;@Resource
&nbsp;&nbsp;UserinfoService userinfoService;

&nbsp;&nbsp;public ActionForward execute(ActionMapping mapping, ActionForm form,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpServletRequest request, HttpServletResponse response)

&nbsp;&nbsp;&nbsp;&nbsp;List<Userinfo> userinfoList = userinfoService.getUserins();
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("得到总数：" + userinfoList.size());
&nbsp;&nbsp;&nbsp;&nbsp;request.setAttribute("userinfoList", userinfoList);
&nbsp;&nbsp;&nbsp;&nbsp;return mapping.findForward("list");
&nbsp;&nbsp;&#125;
&#125;
</textarea><br/><br/>org.springframework.web.struts.DelegatingRequestProcessor处理流程如下: <br/><a href="https://www.heckjj.com/attachment.php?fid=25" target="_blank"><img src="https://www.heckjj.com/attachment.php?fid=25" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>由于用到的库文件太多，在此我只能把源码和数据库文件传上来,希望对刚接触SSH的人有所帮助。 <br/><a href="attachment.php?fid=26">点击这里下载文件</a><br/>Tags - <a href="https://www.heckjj.com/tags/struts1.3/" rel="tag">struts1.3</a> , <a href="https://www.heckjj.com/tags/spring2.5/" rel="tag">spring2.5</a> , <a href="https://www.heckjj.com/tags/hibernate3/" rel="tag">hibernate3</a> , <a href="https://www.heckjj.com/tags/ssh%25E6%2595%25B4%25E5%2590%2588/" rel="tag">ssh整合</a>
]]>
</description>
</item><item>
<link>https://www.heckjj.com/struts-spring-hibernate/#blogcomment</link>
<title><![CDATA[[评论] 手动整合Struts1.3+Spring2.5+Hibernate3]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/struts-spring-hibernate/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>