随笔-20  评论-1495  文章-0  trackbacks-0

IoC(Inversion of Control,以下译为控制反转)随着Java社区中轻量级容器(Lightweight Contianer)的推广而越来越为大家耳熟能详。在此,我不想再多费唇舌来解释“什么是控制反转”和“为什么需要控制反转”。因为互联网上已经有非常多的文章对诸如此类的问题作了精彩而准确的回答。大家可以去读一下Rod Johnson和Juergen Hoeller合著的《Expert one-on-one J2EE Development without EJB》或Martin Fowler所写的《Inversion of Control Containers and the Dependency Injection pattern》。

言归正传,本文的目的主要是介绍在Struts 2中实现控制反转。

历史背景

众所周知,Struts 2是以Webwork 2作为基础发展出来。而在Webwork 2.2之前的Webwork版本,其自身有一套控制反转的实现,Webwork 2.2在Spring 框架的如火如荼发展的背景下,决定放弃控制反转功能的开发,转由Spring实现。值得一提的是,Spring确实是一个值得学习的框架,因为有越来越多的开源组件(如iBATIS等)都放弃与Spring重叠的功能的开发。因此,Struts 2推荐大家通过Spring实现控制反转。

具体实现

首先,在开发环境中配置好Struts 2的工程。对这部分仍然有问题的朋友,请参考我的早前的文章。

然后,将所需的Spring的jar包加入到工程的构建环境(Build Path)中,如下图1所示:

图1 所依赖的Spring的jar包
图1 所依赖的Spring的jar包

本文使用的是Spring 2.0,Spring强烈建议大家在使用其jar包时,只引用需要的包,原因是Spring是一个功能非常强大的框架,其中有些功能是您不需要的;而且Spring提倡的是“按需所取”,而不是EJB的“爱我就要爱我的一切”。当然,如果你怕麻烦或者是不清楚每个包的作用,引用一个Spring的总包也未尝不可。

接下来,就要修改WEB-INF\web.xml文件了,内容为:

<? xml version="1.0" encoding="UTF-8" ?>
< web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>

   
< display-name > Struts 2 IoC Demo </ display-name >

   
< filter >
       
< filter-name > struts-cleanup </ filter-name >
       
< filter-class >
            org.apache.struts2.dispatcher.ActionContextCleanUp
       
</ filter-class >
   
</ filter >

   
< filter >
       
< filter-name > struts2 </ filter-name >
       
< filter-class >
            org.apache.struts2.dispatcher.FilterDispatcher
       
</ filter-class >
   
</ filter >

   
< filter-mapping >
       
< filter-name > struts-cleanup </ filter-name >
       
< url-pattern > /* </ url-pattern >
   
</ filter-mapping >

   
< filter-mapping >
       
< filter-name > struts2 </ filter-name >
       
< url-pattern > /* </ url-pattern >
   
</ filter-mapping >

   
< listener >
       
< listener-class >
            org.springframework.web.context.ContextLoaderListener
       
</ listener-class >
   
</ listener >

   
< welcome-file-list >
       
< welcome-file > index.html </ welcome-file >
   
</ welcome-file-list >
</ web-app >
清单1 WEB-INF\web.xml

大家一看便知道,主要是加入Spring的ContextLoaderListener监听器,方便Spring与Web容器交互。

紧接着,修改Struts.properties文件,告知Struts 2运行时使用Spring来创建对象(如Action等),内容如下:

struts.objectFactory = spring
清单2 classes\struts.properties

再下来,遵循Spring的原则——面向接口编程,创建接口ChatService,代码如下:

package tutorial;

import java.util.Set;

public interface ChatService {
   Set
< String > getUserNames();
}
清单3 tutorial.ChatService.java

然后,再创建一个默认实现ChatServiceImpl,代码如下:

package tutorial;

import java.util.HashSet;
import java.util.Set;

public class ChatServiceImpl implements ChatService {

   
public Set < String > getUserNames() {
       Set
< String > users = new HashSet < String > ();
       users.add(
" Max " );
       users.add(
" Scott " );
       users.add(
" Bob " );
       
return users;
   }


}
清单4 tutorial.ChatServiceImpl.java

接下来,就该新建Action了。tutorial.ChatAction.java的代码如下:

package tutorial;

import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;

public class ChatAction extends ActionSupport {
   
private static final long serialVersionUID = 8445871212065L
   
   
private ChatService chatService;
   
private Set < String > userNames;

   
public void setChatService(ChatService chatService) {
       
this .chatService = chatService;
   }

   
   
public Set < String > getUserNames() {
       
return userNames;
   }

   
   @Override
   
public String execute() {
       userNames
= chatService.getUserNames();
       
return SUCCESS;
   }

   
}
清单5 tutorial.ChatAction.java

ChatAction类使用属性(Getter/Setter)注入法取得ChatService对象。

然后,配置Spring的applicationContext.xml(位于WEB-INF下)文件,内容如下:

<? xml version="1.0" encoding="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
   
< bean id ="chatService" class ="tutorial.ChatServiceImpl" />
   
< bean id ="chatAction" class ="tutorial.ChatAction" scope ="prototype" >
       
< property name ="chatService" >
           
< ref local ="chatService" />
       
</ property >
   
</ bean >
</ beans >
清单6 WEB-INF\applicationContext.xml

上述代码有二点值得大家注意的:

  1. Struts 2会为每一个请求创建一个Action对象,所以在定义chatAction时,使用scope="prototype"。这样Spring就会每次都返回一个新的ChatAction对象了;
  2. 因为ChatServiceImpl被配置为默认的scope(也即是singleton,唯一的),所以在实现时应保证其线程安全(关于编写线程安全的代码的讨论已经超出本文的范围,更超出了本人的能力范围,大家可以参考Addison Wesley Professional出版的《Java Concurrency in Practice》)。

接下来,在classes/struts.xml中配置Action,内容如下:

<! DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
   
< include file ="struts-default.xml" />    
    
   
< package name ="Struts2_IoC" extends ="struts-default" >
       
< action name ="Chat" class ="chatAction" >
           
< result > /UserList.jsp </ result >
       
</ action >
   
</ package >    
</ struts >
清单7 classes\struts.xml

这里的Action和平常不同的就是class属性,它对应于Spring所定义的bean的id,而不是它的类全名。

最后,让我们看看/UserList.jsp,内容如下:

<% @ page contentType = " text/html; charset=UTF-8 " %>
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
   
< title > User List </ title >
</ head >

< body >
   
< h2 > User List </ h2 >
   
< ol >
   
< s:iterator value ="userNames" >
       
< li >< s:property /></ li >
   
</ s:iterator >
   
</ ol >
</ body >
</ html >
清单8 /UserList.jsp

大功告成,分布运行应用程序,在浏览器中键入http://localhost:8080/Struts2_IoC/Chat.action,出现如图2所示页面:

图2 /ListUser.jsp
图2 /ListUser.jsp

总结

通过Spring在Struts 2上实现控制反转是强烈推荐的做法,当然您也可以组合其它的实现(如Pico等)。

posted on 2006-12-28 17:37 Max 阅读(16323) 评论(125)  编辑  收藏 所属分类: Struts 2.0系列
评论共2页: 1 2 下一页 

评论:
# re: 在Struts 2中实现IoC 2007-03-28 13:53 | yangdamao
我所發現的唯一區別就是那几個jar文件不一樣,我的是從我同事那里考過來的,沒注意是那個版本,但是您的jar文件要比我原來的那個大十几kB.  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-03-28 13:57 | yangdamao
謝謝您提供的系列資料,受益匪淺!!  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-03-28 16:19 | jintian
為什么我的頁面上只顯示

User List

沒有其它的 1. Bob
等﹗﹗請問是不是那里沒設置??
怎么會出現這種情況﹗﹗
  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-03-28 16:24 | jintian
郁悶﹗﹗原來是Chat.action﹗﹗
哎﹗﹗  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-03-28 18:33 | javafan
邮件已收到!
非常感谢yangdamao和max二位  回复  更多评论
  
# re: 在Struts 2中实现IoC[未登录] 2007-03-29 18:54 | steven
你上面的例子我出404错误找不到地址,< package name ="Struts2_IoC" extends ="struts-default" >为什么是package name ="Struts2_IoC"??请指教,而且地址是http://localhost:8080/Struts2_IoC/Chat.action。请把源码包括工程文件发到我邮箱:chenyufei_icewolf@hotmail.com。谢谢  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-03-29 18:58 | yangdamao
package 的name 可以是任意的,不用必須和 Struts2_IoC 一樣  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-03-29 19:00 | yangdamao
@steven
正好路過,發你郵箱了  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-03-30 16:39 | xiaolan
你好,我已经收的你的代码了.谢谢,项目正要用,真是雪中送炭.感激不尽  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-05 16:34 | panka
Action class [chatAction] not found - action - file:/D:/server/apache-tomcat-5.5.23/webapps/tstruts2ioc/WEB-INF/classes/struts.xml:8:42

我照例子调试好久,一直都是500错误,没有改动啊.
麻烦也给我发一份源码吧,我油箱是mrpanqinag@gmail.com  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-05 16:42 | panka
@panka
不好意思油箱写错了 ,真确的是 mrpanqiang@gmail.com  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-06 18:00 | yangdamao
@panqiang

I have sent to you!!  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-07 23:03 | Max
@yangdamao
I appreciate that.  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-11 22:54 | niehanzi
您好,我按照您的步骤做了一下,总是不成功,您能给我发一份源代码吗?
我的邮箱是:kedahanzi@163.com,万分感谢!  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-13 10:30 | fff
@AlleNny
老大怎么能把借口配到配置文件里面的,接口是不能实现的.  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-18 10:51 | liujian
你好,我也想要一份代码,我怀疑自己的包有问题
我的邮箱是:film911@163.com ,非常感谢  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-18 14:07 | liujian
已经调试成功,原来放错了一个包,谢谢您的分享  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-20 15:05 | look
????  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-20 15:09 | look
谁有源码发给我一份谢谢!cbz_com0826@163.com  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-21 16:24 | look
感谢max发送的源码包!
原来我少导了一个包,struts2-spring-plugin-2.0.6,这个包是不是就是相当也spring中的plugin的作用吧!  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-25 09:35 | ddd
刚看完你的文章, 有几点不太明白。。

Action里面有chatService的setter, 和getUserName(感觉这个是否不需要)

使用注入的方法, 是否Action里面, 就不需要对chatService进行New啥的,

就可以得到chatService的Instance了?  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-25 09:41 | ddd
Dependency Injection 与 IoC又有什么区别呢?  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-04-27 16:28 | Max
@ddd
请参考Martin Fowler所写的《Inversion of Control Containers and the Dependency Injection pattern》  回复  更多评论
  
# re: 在Struts 2中实现IoC[未登录] 2007-04-30 05:15 | free
不需要如此麻烦,当你定义了setter和spring的factory,并不需要任何配置就可以析取出Spring中的bean  回复  更多评论
  
# re: 在Struts 2中实现IoC[未登录] 2007-05-12 19:56 | lucky
404错误,有如下错误提示,大家能帮看看大概是什么地方什么错误吗?谢谢了

2007-5-12 19:40 org.apache.catalina.core.ApplicationContext log
严重: Exception starting filter struts2
Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]

......

2007-5-12 19:40 org.apache.catalina.core.StandardContext star
严重: Error filterStart
2007-5-12 19:40 org.apache.catalina.core.StandardContext start
严重: Context startup failed due to previous errors  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-05-12 22:21 | cookie
请教各位
为什么把包含jar包的文件夹copy到WEB-INF/lib目录下在Eclipse工程中显示出来不出这些jar包来呢
用MyEclipse建的web工程中把包含jar包的文件夹复制到lib下就可以显示这些jar包
那Spring包下lib文件夹下的那么多文件夹怎么才能把他们在工程下显示出来呀?
各位老大都是怎么解决这个问题的呀?  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-05-12 22:48 | cookie
那个web.xml中的struts-cleanup的<filter>是做什么用的呀
以前的教程里没有呀
能解释一下吗?谢谢  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-05-15 09:39 | Max
# re: 在Struts 2中实现IoC 2007-05-15 09:44 | Max
@lucky
应该是没有加struts2-spring-plugin-2.0.6.jar包  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-05-16 17:39 | zlyyi
要是我有多个入注 怎么办  可不可以 指定那个入注呢 谢谢 我也是初学
  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-05-18 09:12 | Max
@zlyyi
“多个入注”是什么意思?你可以看看Spring框架。  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-06-06 15:19 | small.sprite
照着写了,可是运行不了,能发一份源码吗?
117148038@qq.com  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-06-18 17:48 | 想飞就飞
少说了一个包 struts2-spring-plugin-2.0.6.jar
btw:
楼主的代码是不是用什么工具调整过?
< html >
< head >
< title > User List </ title >
</ head >

< > 或者 ( )两边都存在空格,如此工整?

如果是的话,用什么工具调整过代码啊?
推荐一下,俺也用用  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-06-19 00:54 | Max
@想飞就飞
我没有用任何工具,只是使用了Blogjava的“添加新随笔”中的“插入代码”功能。  回复  更多评论
  
# re: 在Struts 2中实现IoC[未登录] 2007-06-19 13:18 | yang
我的服务器 WebLogic 9.2 中总显示:

<2007-6-19 下午12时58分01秒 CST> <Error> <HTTP> <BEA-101165> <Could not load user defined filter in web.xml: org.apache.struts2.dispatcher.FilterDispatcher.
Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:224)
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:195)
at org.apache.struts2.config.BeanSelectionProvider.register(BeanSelectionProvider.java:155)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:131)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
Truncated. see log file for complete stacktrace
>

我没有你们说的 struts2-spring-plugin-2.0.6.jar

能否发一份源码给我,谢谢了!

  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-06-21 20:30 | jezz
@yang
你的问题好像是缺少那个struts2-spring-plugin-2.0.6.jar包
你下载那个struts2的框架时候里面带的example!
就有这个包你自己找找!  回复  更多评论
  
# re: 在Struts 2中实现IoC[未登录] 2007-07-06 01:36 | xy
上面的例子做成功后,在配置中添加了SPRING2.0的AOP,结果报以下的错误:
2007-07-06 01:26:20,218 - Caught OgnlException while setting property 'excludeMethods' on type '$Proxy7'.
ognl.NoSuchPropertyException: $Proxy7.excludeMethods

2007-07-06 01:26:20,218 - Caught OgnlException while setting property 'excludeMethods' on type '$Proxy8'.
ognl.NoSuchPropertyException: $Proxy7.excludeMethods

不知道是什么原因,请帮忙分析一下.  回复  更多评论
  
# re: 在Struts 2中实现IoC[未登录] 2007-07-06 04:06 | xy
在加AOP后总是出现如下异常,不知是何原因:
2007-07-06 04:00:55,968 - ... initialized Struts-Spring integration successfully
2007-07-06 04:01:08,328 - Caught OgnlException while setting property 'excludeMethods' on type '$Proxy7'.
ognl.NoSuchPropertyException: $Proxy7.excludeMethods
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:132)
at com.opensymphony.xwork2.util.OgnlValueStack$ObjectAccessor.setProperty(OgnlValueStack.java:68)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.Ognl.setValue(Ognl.java:476)
at com.opensymphony.xwork2.util.OgnlUtil.setValue(OgnlUtil.java:186)
at com.opensymphony.xwork2.util.OgnlUtil.internalSetProperty(OgnlUtil.java:360)
at com.opensymphony.xwork2.util.OgnlUtil.setProperties(OgnlUtil.java:76)
at com.opensymphony.xwork2.util.OgnlUtil.setProperties(OgnlUtil.java:103)
at com.opensymphony.xwork2.util.OgnlUtil.setProperties(OgnlUtil.java:90)
at com.opensymphony.xwork2.ObjectFactory.buildInterceptor(ObjectFactory.java:183)
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:57)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.lookupInterceptorReference(XmlConfigurationProvider.java:864)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptorStack(XmlConfigurationProvider.java:699)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptorStacks(XmlConfigurationProvider.java:712)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptors(XmlConfigurationProvider.java:733)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:365)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:398)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:455)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:225)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:308)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:79)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3502)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4071)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:755)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:886)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:849)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1079)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1011)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1003)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:437)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:2010)
at org.apache.catalina.startup.Catalina.start(Catalina.java:537)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409)  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-07-06 12:24 | huiyino
我照你写的做了一遍 。。。OK。。。
可是我加了些Bean进去就不行了。。。。。部署的时候出了这错误。。。。
服务器报的错误部署不成功。。。。
麻烦帮忙看看呀 。。。。

2007-7-6 12:12:50 org.apache.struts2.spring.StrutsSpringObjectFactory <init> 信息: ... initialized Struts-Spring integration successfully 2007-7-6 12:12:50 org.apache.catalina.core.StandardContext start 严重: Error filterStart 2007-7-6 12:12:50 org.apache.catalina.core.StandardContext start 严重: Context [/Struts_spring] startup failed due to previous errors 2007-7-6 12:12:50 org.springframework.context.support.AbstractApplicationContext doClose


struts.xml
<action name="login" class="userLogin">
<result>success.jsp</result>
<result type="input">failed.jsp</result>
</action>

applicationContext.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<property name="url"><value>jdbc:mysql://127.0.0.1:3306/usertest</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>123</value></property>
</bean>
<bean id="userBase" class="com.struts.spring.UserBase">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="userLogin" class ="com.struts.spring.UserLogin">
<property name ="userBase" >
<ref local ="userBase" />
</property >
</bean>

我是在搭建环境。。。。   回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-07-09 23:50 | Max
@xy
ognl.NoSuchPropertyException: $Proxy7.excludeMethods 你只贴出错误信息,我很难判断什么错误。
@huiyino
我很难判断什么错误。
Sorry!  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-07-11 10:21 | mashiguang
见意max把需要struts2-spring-plugin-2.0.6.jar这个包也写到文章里,不然会在启动是出错,像我搞了半天才知道原来是缺这个包的缘故.
我开始还以为是spring版本的问题.  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-07-11 22:39 | Max
@mashiguang
对不起,写的时候忘了所这个包的依赖写进去了。  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-06 10:06 | jueqinguzhu
能不能给我发份源码啊 小弟万分感谢
我自己配置了好久都没成功
jueqinguzhu@yahoo.com.cn  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-07 14:26 | jueqinguzhu
MAX先生:
谢谢你的代码,为什么要在ACTION里面加上private static final long serialVersionUID = 8445871212065L 呢?  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-07 14:34 | jueqinguzhu
而且这个serialVersionUID是可以自动生成,还是自己随意写的一个LONG值  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-07 16:03 | shengguolong
404错误,我看了下,工程起不来。。。
能否发一份源码,十分感谢!!!
邮箱:long2317479@gmail.com  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-08 09:31 | pn2007
请发份源码,谢谢! sailsoft@tom.com.  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-08 09:36 | Max
@jueqinguzhu
serialVersionUID用于串行化(Serialize)对象时,加上标识。我是使用Eclipse生成的值。  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-08 14:44 | liy
你好!Max大哥,我运行Struts 2中实现Ioc的例子老是报404错误.
能否发一份你的源码给我比较一下?
我的邮箱是followmephoe@yahoo.com.cn谢谢  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-09 21:46 | shengguolong
我发现我的每个struts2+spring+hibernate都有这个问题,我有把spring-web.jar这个包加进去啊,郁闷了。
能否帮我解答下。。。多谢了!!!
2007-8-9 21:37:42 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.web.context.ContextLoader.<init>(ContextLoader.java:145)
at org.springframework.web.context.ContextLoaderListener.createContextLoader(ContextLoaderListener.java:57)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:48)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4336)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1015)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:448)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:589)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
2007-8-9 21:37:42 org.apache.catalina.core.StandardContext start
严重: Error listenerStart  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-20 13:15 | edzhh
能否发我一份,我的配置后站点总是启动不了,我的邮箱是:edzhh@163.com,
Thank you!
  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-20 16:39 | edzhh
haha,我的问题:
java.lang.SecurityException: class "org.apache.commons.collections.SequencedHashMap"'s signer information does not match signer information of other classes in the same package
解决了,原因是:我的cglib-2.1.3.jar和hibernate3.jar两个文件是在MyEclipse6.0下自动生成的,只要在MyEclipse5.0或5.5下生成拷贝到工程下覆盖掉6.0下生成的就是了.  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-08-27 09:00 | sshzhangwg
大哥,你把你的文章整理下,出本书,可能对整个软件行业的帮助会更大些。  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-09-17 15:19 | sanjin2482
我也发现了问题--可否也给我发份源代码---sanjin2482@sina.com.cn
x谢谢!!  回复  更多评论
  
# re: 在Struts 2中实现IoC[未登录] 2007-09-18 13:35 | yingjie
max兄,看了你的文章十分收益,有个问题想请教一下,就是把struts1.2的项目改成struts2.0,请问要注意哪些方面啊?还有就是action要怎么写合适,没有了ActionForm那么数据存放到action中要怎么存啊?总之就是有很多的问题要请教你,希望你能教我,我的邮箱是yingjie853@163.com,MSN:yingjie_853@hotmail.com
QQ:278537061,希望能和你交流一下
  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-09-30 12:06 | origo
@shengguolong
你的错误是没有添加commons logging的jar  回复  更多评论
  
# 学习过程中也出现这种提示,请问什么原因?[未登录] 2007-09-30 15:26 | Spring
学习过程中也出现这种提示,请问什么原因?
严重: Exception starting filter struts2
Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:223)
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:194)
at org.apache.struts2.config.BeanSelectionProvider.register(BeanSelectionProvider.java:153)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:131)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:452)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3693)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4340)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)  回复  更多评论
  
# 学习过程中也出现这种提示,请问什么原因?[未登录] 2007-09-30 15:33 | Spring
@lucky
我也碰到这种问题?请问你是如何解决的?
严重: Exception starting filter struts2
Cannot locate the chosen ObjectFactory implementation: spring - [unknown location]
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:223)
at org.apache.struts2.config.BeanSelectionProvider.alias(BeanSelectionProvider.java:194)
at org.apache.struts2.config.BeanSelectionProvider.register(BeanSelectionProvider.java:153)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:131)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:395)  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-10-09 15:57 | sanjin
同志们:
我也发现了问题--可否也给我发份源代码---sanjin2482@sina.com.cn  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-11-06 21:18 | Cowboy
想请教一个小问题:
在Web.xml文件里,下面的代码有什么作用?
< filter >
< filter-name > struts-cleanup </ filter-name >
< filter-class >
org.apache.struts2.dispatcher.ActionContextCleanUp
</ filter-class >
</ filter >

< filter-mapping >
< filter-name > struts-cleanup </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >

我只是知道,filter与filter-mapping是通过filter-name来对应起来的。  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-11-23 12:59 | 谢谢大哥指导
谢谢大哥指导谢谢大哥指导谢谢大哥指导
  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-11-26 12:21 | ryou

我也碰到这种问题?请问你是如何解决的?
  回复  更多评论
  
# re: 在Struts 2中实现IoC 2007-11-26 12:22 | ryou
我也碰到这种问题?请问你是如何解决的?
at com.opensymphony.xwork2.ObjectFactory.buildInterceptor(ObjectFactory.java:206)
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:57)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.lookupInterceptorReference(XmlConfigurationProvider.java:905)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptorStack(XmlConfigurationProvider.java:743)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptorStacks(XmlConfigurationProvider.java:756)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadInterceptors(XmlConfigurationProvider.java:777)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:410)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)