<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaWo &#187; Spring</title>
	<atom:link href="http://www.javawo.com/category/java/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javawo.com</link>
	<description>JavaWo关注互联网前沿的Blog.</description>
	<lastBuildDate>Thu, 13 May 2010 12:03:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Spring中ref local与ref bean区别</title>
		<link>http://www.javawo.com/spring-ref-local-vs-ref-bean/</link>
		<comments>http://www.javawo.com/spring-ref-local-vs-ref-bean/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 08:25:29 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[ref bean]]></category>
		<category><![CDATA[ref local]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=68</guid>
		<description><![CDATA[
&#60; bean id = &#34;userDAOProxy&#34; class = &#34;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&#34; &#62; 
        &#60; property name = &#34;transactionManager&#34; &#62; 
            &#60; ref bean = &#34;transactionManager&#34; /&#62; 
        &#60;/ property &#62; 
   [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; bean id = <span style="color: #ff0000;">&quot;userDAOProxy&quot;</span> class = <span style="color: #ff0000;">&quot;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
        <span style="color: #009900;">&lt; property name = <span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
            <span style="color: #009900;">&lt; ref bean = <span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
        <span style="color: #009900;">&lt;/ property <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
        <span style="color: #009900;">&lt; property name = <span style="color: #ff0000;">&quot;target&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
            <span style="color: #009900;">&lt; ref local = <span style="color: #ff0000;">&quot;UserDAO&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
        <span style="color: #009900;">&lt;/ property <span style="color: #000000; font-weight: bold;">&gt;</span></span> 
    <span style="color: #009900;">&lt;/ bean <span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></div></div>

<p>1 、用 local 属性指定目标 bean 可以利用 xml 解析器的能力在同一个文件中验证 xml id 引用 . 如果在同一个文件中没有匹 配的元素 ,xml 解析器就会产生一个 error, 所以如果目标 bean 在同一个 xml 文件中 , 那么用 local 形式是最好的选择 .</p>
<p>2 、可以这么说，<br />
<strong>bean是寻找全局中的 bean; </strong><br />
<strong>local是寻找本 xml 文件中的 bean </strong></p>
<p>3 、 <ref> 提供了如下几方面的属性 :<br />
1)bean: 在当前 Spring XML 配置文件中，或者在同一 BeanFactory(ApplicationContext) 中的其他 JavaBean 中 .</p>
<p>2)local: 在当前 Spring XML 配置文件中 . 其依赖的 JavaBean 必须存在于当前 Spring XML 培植文件中 . 如果借助 于 Spring IDE, 则在编译期可以对其依赖的 JavaBean 进行验证。基于 local 方式，开发者能够使用到 XML 本身提供 的优势，而进行验证。 </p>
<p>3)parent: 用于指定其依赖的父 JavaBean 定义。 </p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/spring-ref-local-vs-ref-bean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>spring整合struts</title>
		<link>http://www.javawo.com/spring-struts-integration/</link>
		<comments>http://www.javawo.com/spring-struts-integration/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 14:10:44 +0000</pubDate>
		<dc:creator>JavaWo</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[Struts]]></category>
		<category><![CDATA[整合]]></category>

		<guid isPermaLink="false">http://www.javawo.com/?p=62</guid>
		<description><![CDATA[与 Struts 相似，Spring 可以作为一个 MVC 实现。这两种框架都具有自己的优点和缺点，尽管大部分人同意 Struts 在 MVC 方面仍然是最好的。很多开发团队已经学会在时间紧迫的时候利用 Struts 作为构造高品质软件的基础。Struts 具有如此大的推动力，以至于开发团队宁愿整合 Spring 框架的特性，而不愿意转换成 Spring MVC。没必要进行转换对您来说是一个好消息。Spring 架构允许您将 Struts 作为 Web 框架连接到基于 Spring 的业务和持久层。最后的结果就是现在一切条件都具备了。
在接下来您将会了解到三种将 Struts MVC 整合到 Spring 框架的方法。我将揭示每种方法的缺陷并且对比它们的优点。 一旦您了解到所有三种方法的作用，我将会向您展示一个令人兴奋的应用程序，这个程序使用的是这三种方法中我最喜欢的一种。
使用 Spring 的 ActionSupport 类整合 Structs 
使用 Spring 的 DelegatingRequestProcessor 覆盖 Struts 的 RequestProcessor 
将 Struts Action 管理委托给 Spring 框架 
1. 使用 Spring 的 ActionSupport
手动创建一个 Spring [...]]]></description>
			<content:encoded><![CDATA[<p>与 Struts 相似，Spring 可以作为一个 MVC 实现。这两种框架都具有自己的优点和缺点，尽管大部分人同意 Struts 在 MVC 方面仍然是最好的。很多开发团队已经学会在时间紧迫的时候利用 Struts 作为构造高品质软件的基础。Struts 具有如此大的推动力，以至于开发团队宁愿整合 Spring 框架的特性，而不愿意转换成 Spring MVC。没必要进行转换对您来说是一个好消息。Spring 架构允许您将 Struts 作为 Web 框架连接到基于 Spring 的业务和持久层。最后的结果就是现在一切条件都具备了。</p>
<p>在接下来您将会了解到三种将 Struts MVC 整合到 Spring 框架的方法。我将揭示每种方法的缺陷并且对比它们的优点。 一旦您了解到所有三种方法的作用，我将会向您展示一个令人兴奋的应用程序，这个程序使用的是这三种方法中我最喜欢的一种。</p>
<p><strong>使用 Spring 的 ActionSupport 类整合 Structs </strong><br />
<strong>使用 Spring 的 DelegatingRequestProcessor 覆盖 Struts 的 RequestProcessor </strong><br />
<strong>将 Struts Action 管理委托给 Spring 框架 </strong></p>
<p><strong>1. 使用 Spring 的 ActionSupport</strong><br />
手动创建一个 Spring 环境是一种整合 Struts 和 Spring 的最直观的方式。为了使它变得更简单，Spring 提供了一些帮助。为了方便地获得 Spring 环境，org.springframework.web.struts.ActionSupport 类提供了一个 getWebApplicationContext() 方法。您所做的只是从 Spring 的 ActionSupport 而不是 Struts Action 类扩展您的动作</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">ca.nexcel.books.actions</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionError</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionErrors</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.DynaActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.context.ApplicationContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.web.struts.ActionSupport</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ca.nexcel.books.beans.Book</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ca.nexcel.books.business.BookService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchSubmit <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #009900;">&#123;</span>   <span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000000; font-weight: bold;">public</span> ActionForward execute<span style="color: #009900;">&#40;</span>
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException <span style="color: #009900;">&#123;</span>
    DynaActionForm searchForm <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaActionForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> isbn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> searchForm.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;isbn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//the old fashion way</span>
    <span style="color: #666666; font-style: italic;">//BookService bookService = new BookServiceImpl();</span>
&nbsp;
    ApplicationContext ctx <span style="color: #339933;">=</span> 
      getWebApplicationContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
    BookService bookService <span style="color: #339933;">=</span> 
      <span style="color: #009900;">&#40;</span>BookService<span style="color: #009900;">&#41;</span> ctx.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bookService&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>
&nbsp;
  <span style="color: #003399;">Book</span> book <span style="color: #339933;">=</span> bookService.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>isbn.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> book<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ActionErrors errors <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActionErrors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      errors.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ActionErrors.<span style="color: #006633;">GLOBAL_ERROR</span>,<span style="color: #000000; font-weight: bold;">new</span> ActionError
        <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message.notfound&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      saveErrors<span style="color: #009900;">&#40;</span>request, errors<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;failure&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
    request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;book&quot;</span>, book<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>让我们快速思考一下这里到底发生了什么。在 (1) 处，我通过从 Spring 的 ActionSupport 类而不是 Struts 的 Action 类进行扩展，创建了一个新的 Action。在 (2) 处，我使用 getWebApplicationContext() 方法获得一个 ApplicationContext。为了获得业务服务，我使用在 (2) 处获得的环境在 (3) 处查找一个 Spring bean。</p>
<p>这种技术很简单并且易于理解。不幸的是，它将 Struts 动作与 Spring 框架耦合在一起，对Struts的侵入性非常大。如果您想替换掉 Spring，那么您必须重写代码。并且，由于 Struts 动作不在 Spring 的控制之下，所以它不能获得 Spring AOP 的优势。当使用多重独立的 Spring 环境时，这种技术可能有用，但是在大多数情况下，这种方法不如另外两种方法合适。</p>
<p><strong>2. 覆盖 RequestProcessor</strong><br />
将 Spring 从 Struts 动作中分离是一个更巧妙的设计选择。分离的一种方法是使用<strong>org.springframework.web.struts.DelegatingRequestProcessor </strong>类来覆盖 Struts 的 RequestProcessor 处理程序</p>
<p>Struts配置文件：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE struts-config PUBLIC</span>
<span style="color: #00bbdd;">          &quot;-//Apache Software Foundation//DTD Struts Configuration 1.1//EN&quot;</span>
<span style="color: #00bbdd;">          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchForm&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.DynaValidatorForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;isbn&quot;</span>    <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;global-forwards</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.action.ActionForward&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;welcome&quot;</span>                <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/welcome.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchEntry&quot;</span>            <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchEntry.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchSubmit&quot;</span>           <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchSubmit.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/global-forwards<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/welcome&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/welcome.htm&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchEntry&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/search.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchSubmit&quot;</span> </span>
<span style="color: #009900;">               <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.actions.SearchSubmit&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">input</span>=<span style="color: #ff0000;">&quot;/searchEntry.do&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">               <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;success&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/detail.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;failure&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/search.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message-resources</span> <span style="color: #000066;">parameter</span>=<span style="color: #ff0000;">&quot;ApplicationResources&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controller</span> <span style="color: #000066;">processorClass</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.</span>
<span style="color: #009900;">   DelegatingRequestProcessor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> |(1)
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.ValidatorPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;pathnames&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.ContextLoaderPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;csntextConfigLocation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/beans.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>我利用了 <controller> 标记来用 DelegatingRequestProcessor 覆盖默认的 Struts RequestProcessor。下一步是在我的 Spring 配置文件中注册该动作</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; </span>
<span style="color: #00bbdd;">  &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.business.BookServiceImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;/searchSubmit&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.actions.SearchSubmit&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> |(1)
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>注意：在 (1) 处，我使用名称属性注册了一个 bean，以匹配 struts-config 动作映射名称。SearchSubmit 动作揭示了一个 JavaBean 属性，允许 Spring 在运行时填充属性</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">ca.nexcel.books.actions</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletRequest</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.Action</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionError</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionErrors</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionForward</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.ActionMapping</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.struts.action.DynaActionForm</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ca.nexcel.books.beans.Book</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ca.nexcel.books.business.BookService</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SearchSubmit <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Action</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> BookService bookService<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> BookService getBookService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> bookService<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBookService<span style="color: #009900;">&#40;</span>BookService bookService<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #339933;">|</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bookService</span> <span style="color: #339933;">=</span> bookService<span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span> 
  <span style="color: #000000; font-weight: bold;">public</span> ActionForward execute<span style="color: #009900;">&#40;</span>
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response<span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span>, ServletException <span style="color: #009900;">&#123;</span>
    DynaActionForm searchForm <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>DynaActionForm<span style="color: #009900;">&#41;</span> form<span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> isbn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span> searchForm.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;isbn&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #003399;">Book</span> book <span style="color: #339933;">=</span> getBookService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>isbn.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #339933;">|</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> book<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ActionErrors errors <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ActionErrors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      errors.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>ActionErrors.<span style="color: #006633;">GLOBAL_ERROR</span>,<span style="color: #000000; font-weight: bold;">new</span> ActionError<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;message.notfound&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      saveErrors<span style="color: #009900;">&#40;</span>request, errors<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;failure&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
      request.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;book&quot;</span>, book<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> mapping.<span style="color: #006633;">findForward</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;success&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>在 (1) 处，我创建了一个 JavaBean 属性。DelegatingRequestProcessor自动地配置这种属性。这种设计使 Struts 动作并不知道它正被 Spring 管理，并且使您能够利用 Sping 的动作管理框架的所有优点。由于您的 Struts 动作注意不到 Spring 的存在，所以您不需要重写您的 Struts 代码就可以使用其他控制反转容器来替换掉 Spring。</p>
<p>DelegatingRequestProcessor 方法的确比第一种方法好，但是仍然存在一些问题。如果您从写了Struts的 RequestProcessor，则需要手动整合 Spring 的 DelegatingRequestProcessor。添加的代码会造成维护的麻烦并且将来会降低您的应用程序的灵活性。此外，还有过一些使用一系列命令来代替 Struts RequestProcessor 的传闻。 这种改变将会对这种解决方法的使用寿命造成负面的影响。</p>
<p><strong>3. 将动作管理委托给 Spring 的 DelegatingActionProxy类</strong></p>
<p>一个更好的解决方法是将 Strut 动作管理委托给 Spring。您可以通过在 struts-config 动作映射中注册一个代理来实现。代理负责在 Spring 环境中查找 Struts 动作。由于动作在 Spring 的控制之下，所以它可以填充动作的 JavaBean 属性，并为应用诸如 Spring 的 AOP 拦截器之类的特性带来了可能。 </p>
<p>struts-config 内容：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE struts-config PUBLIC</span>
<span style="color: #00bbdd;">          &quot;-//Apache Software Foundation//DTD Struts Configuration 1.1//EN&quot;</span>
<span style="color: #00bbdd;">          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchForm&quot;</span> </span>
<span style="color: #009900;">      <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.DynaValidatorForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form-property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;isbn&quot;</span>    <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;java.lang.String&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form-beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;global-forwards</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.apache.struts.action.ActionForward&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;welcome&quot;</span>                <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/welcome.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchEntry&quot;</span>            <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchEntry.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span>   <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchSubmit&quot;</span>           <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchSubmit.do&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/global-forwards<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/welcome&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/welcome.htm&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchEntry&quot;</span> <span style="color: #000066;">forward</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/search.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span>    <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/searchSubmit&quot;</span> </span>
<span style="color: #009900;">             <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.DelegatingActionProxy&quot;</span> |<span style="color: #66cc66;">&#40;</span>1<span style="color: #66cc66;">&#41;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">input</span>=<span style="color: #ff0000;">&quot;/searchEntry.do&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">validate</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;searchForm&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;success&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/detail.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;forward</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;failure&quot;</span> <span style="color: #000066;">path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/pages/search.jsp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action-mappings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;message-resources</span> <span style="color: #000066;">parameter</span>=<span style="color: #ff0000;">&quot;ApplicationResources&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.apache.struts.validator.ValidatorPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;pathnames&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.ContextLoaderPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;contextConfigLocation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/beans.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>一个典型的 struts-config.xml 文件，只有一个小小的差别。它注册 Spring 代理类的名称，而不是声明动作的类名，如（1）处所示。<strong>DelegatingActionProxy</strong> 类使用动作映射名称查找 Spring 环境中的动作。这就是我们使用 ContextLoaderPlugIn 声明的环境。</p>
<p>将一个 Struts 动作注册为一个 Spring bean 是非常直观的。我利用动作映射使用 <bean> 标记的名称属性（在这个例子中是 &#8220;/searchSubmit&#8221;）简单地创建了一个 bean。这个动作的 JavaBean 属性像任何 Spring bean 一样被填充：</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN//EN&quot; </span>
<span style="color: #00bbdd;"> &quot;http://www.springframework.org/dtd/spring-beans.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.business.BookServiceImpl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;/searchSubmit&quot;</span>   </span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.nexcel.books.actions.SearchSubmit&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;bookService&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>动作委托解决方法是这三种方法中最好的。Struts 动作不了解 Spring，不对代码作任何改变就可用于非 Spring 应用程序中。RequestProcessor 的改变不会影响它，并且它可以利用 Spring AOP 特性的优点。 </p>
<p>动作委托的优点不止如此。一旦让 Spring 控制您的 Struts 动作，您就可以使用 Spring 给动作补充更强的活力。例如，没有 Spring 的话，所有的 Struts 动作都必须是线程安全的。如果您设置 <bean> 标记的 singleton 属性为“false”，那么不管用何种方法，您的应用程序都将在每一个请求上有一个新生成的动作对象。您可能不需要这种特性，但是把它放在您的工具箱中也很好。您也可以利用 Spring 的生命周期方法。例如，当实例化 Struts 动作时，<bean> 标记的 init-method 属性被用于运行一个方法。类似地，在从容器中删除 bean 之前，destroy-method 属性执行一个方法。这些方法是管理昂贵对象的好办法，它们以一种与 Servlet 生命周期相同的方式进行管理。</p>
<p>另外在说下Spring容器初始化问题<br />
初始化Spring容器也有2种方式：<br />
一种是在web.xml中配置ContextLoaderListener</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>contextConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		/WEB-INF/acegi-config.xml,
		/WEB-INF/applicationContext.xml,
		/WEB-INF/action-servlet.xml,
		/WEB-INF/daoContext.xml,
		/WEB-INF/serviceContext.xml,	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		org.springframework.web.context.ContextLoaderListener
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>另一种是在struts-config.xml中配置plug-in</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plug-in</span> <span style="color: #000066;">className</span>=<span style="color: #ff0000;">&quot;org.springframework.web.struts.ContextLoaderPlugIn&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>   
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;set-property</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;contextConfigLocation&quot;</span>  <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-INF/daoContext.xml,/WEB-INF/serviceContext.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>   
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plug-in<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>这中间存在OpenSessionInView模式的失效的问题,推荐使用第一种方法。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javawo.com/spring-struts-integration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
