Struts2 将StrutsSpringObjectFactory设置为我的当前ObjectFactory

Struts2 将StrutsSpringObjectFactory设置为我的当前ObjectFactory,struts2,opensymphony,Struts2,Opensymphony,我正在从事一个使用openSymphony 2.1.2和struts 2.3.8的项目 然而,在这个代码基础上,我尝试应用一段代码,该代码是为openSymphony和struts的旧版本编写的 这是一段代码: (作为旁注,我需要这些代码,以便能够在基于不同框架的系统中模拟从继承的项目运行struts操作。) 我对以下段落有问题: final StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory("auto", "true"

我正在从事一个使用openSymphony 2.1.2和struts 2.3.8的项目

然而,在这个代码基础上,我尝试应用一段代码,该代码是为openSymphony和struts的旧版本编写的

这是一段代码:

(作为旁注,我需要这些代码,以便能够在基于不同框架的系统中模拟从继承的项目运行struts操作。)

我对以下段落有问题:

final StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory("auto", "true", servletContext);
ObjectFactory.setObjectFactory(ssf);
在第二行,我得到了一个错误:
类型ObjectFactory的方法setObjectFactory(StrutsSpringObjectFactory)未定义

但是,当我查看时,我没有看到其他方法可以让我将特定的
ObjectFactory
(例如
StrutsSpringObjectFactory
)设置为执行操作时使用的
ObjectFactory
。 那么,如何使用新的API设置这样一个特定的
ObjectFactory

更新代码:

static {
    sessionMap = new HashMap<String, Object>();

    // Create fake back end here.
    // ...

    // ===== Struts setup:
    // Create and use a file system resource loader otherwise Tiles will not find
    // our configuration file. The default resource loader is able to find struts.xml
    // if it is in the classpath, but not tiles.xml.
    final FileSystemResourceLoader loader = new FileSystemResourceLoader();

    final String[] config = new String[] { "WEB-INF/classes/struts.xml" };

    servletContext = new MockServletContext(loader);
    final XmlWebApplicationContext appContext = new XmlWebApplicationContext();

    // Link the servlet context and the Spring context.
    appContext.setServletContext(servletContext);
    appContext.setConfigLocations(config);
    appContext.refresh();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
        appContext);

    servletContext.addInitParameter(BasicTilesContainer.DEFINITIONS_CONFIG, "WEB-INF/tiles.xml");

    // Creating the tiles listener statically (not via class loader).
    final StrutsTilesListener tilesListener = new StrutsTilesListener();
    final ServletContextEvent event = new ServletContextEvent(servletContext);
    tilesListener.contextInitialized(event);

    // Use spring as the object factory for Struts
    //this code not working anymore with struts 2.3.2:
    //final StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory("auto", "true", servletContext);
    //ObjectFactory.setObjectFactory(ssf);

    //my change: (original note working with newer version of struts)
    //StrutsSpringObjectFactory.setObjectFactory(ssf);

    //(*) original location.

    // Dispatcher is the guy that actually handles all requests. Pass in
    // an empty. Map as the parameters but if you want to change stuff like
    // what config files to read, you need to specify them here
    // (see Dispatcher's source code)
    dispatcher = new Dispatcher(servletContext, new HashMap<String, String>()); 
    dispatcher.init();
    Dispatcher.setInstance(dispatcher);

    //(*) moved to here:
    //http://stackoverflow.com/questions/15821643/setting-strutsspringobjectfactory-as-my-current-objectfactory?noredirect=1#comment22519072_15821643
    final StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory("auto", "true", "false", servletContext, "false", dispatcher.getContainer());

}
因为,在DataTest中,我尝试执行()DataAction。该操作调用一些混乱的数据库逻辑,并最终返回一些数据。我的服务需要这些数据。我不想试图弄清楚这个动作到底做了什么,而是想简单地执行它()并获取数据。我尝试的方法是运行一个将执行()操作的测试

我的struts.xml不直接在我正在构建的项目中。这是我的情况:让我们称我的新项目为A项目;还有我所依赖的遗留struts项目,项目B。 项目A正在由其他框架(JSF和Spring)构建。 项目B使用struts作为UI。但是它的操作是以如此混乱的方式编写的,以至于很多DB访问逻辑都是直接在操作中编写的。因此,如果我想使用项目B中的逻辑,而不是重写它,一个临时解决方案(我正在尝试实现)将是使用相应的请求数据执行项目B中的操作。 方法是在a的服务类中对B的操作运行测试

由于项目B打包为war文件,我在项目a中与Maven一起重用它的方法是将其定义为项目a中的覆盖。 项目B的类路径中的类和其他资源被打包到一个jar中。然后在项目A中,我依赖于这个jar。 因此struts.xml存在于包含项目B的类和xml的jar中。在编译时,项目A只能在项目A的jar中看到struts.xml

只有当我打包项目A时,war B才被解包,它的内容(包括struts.xml)被复制到项目A的目标文件夹中,然后A+B被打包为war。(在最后一场战争中,struts.xml将同时出现在jarb和最终战争中的WEB-INF/class中


因此,在实际操作中,如果我试图通过右键单击测试并将其“作为junit运行”来运行测试,那么在我的类路径上的一些WEB-INF/类中应该找不到struts.xml——首先,在我的工作区中没有struts.xml,只有在创建的war中。其次,我只尝试从war中运行测试(如上所示)。当时有WEB-INF/classes/struts.xml,为什么找不到它?

您需要包含Struts2 Spring插件(*.jar)

Spring插件通过将Struts对象工厂重写为 增强核心框架对象的创建 创建时,它使用Struts配置中的class属性 对应于Spring配置中的id属性。如果不是 找到时,该类将尝试像往常一样创建,然后由自动连线 对于操作,Spring2的bean作用域特性可以是 用于将操作实例范围限定到会话、应用程序或应用程序 自定义范围,提供高于默认范围的高级自定义 按请求范围划分


您需要包括Struts2 Spring插件(*.jar)

Spring插件通过将Struts对象工厂重写为 增强核心框架对象的创建 创建时,它使用Struts配置中的class属性 对应于Spring配置中的id属性。如果不是 找到时,该类将尝试像往常一样创建,然后由自动连线 对于操作,Spring2的bean作用域特性可以是 用于将操作实例范围限定到会话、应用程序或应用程序 自定义范围,提供高于默认范围的高级自定义 按请求范围划分


只需创建
StrutsSpringObjectFactory的新实例
,但在创建
Dispatcher
之后再创建

final StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory("auto",
      "true", "false", servletContext, "false", dispatcher.getContainer());

但是看看用于测试struts2操作的struts2 junit插件。它有
StrutsSpringJUnit4TestCase
和方便的方法,所以你不需要自己做所有这些事情。

只需创建
StrutsSpringObjectFactory
的新实例,但在创建
Dispatcher
之后再做

final StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory("auto",
      "true", "false", servletContext, "false", dispatcher.getContainer());

但是请看一看用于测试struts2操作的struts2 junit插件。它有
StrutsSpringJUnit4TestCase
和方便的方法,因此您不需要自己完成所有这些工作。

请看一看struts2 junit插件和
StrutsSpringJUnit4TestCase
类。请看一看struts2 junit插件和
StrutsSpringJUnit4TestCase
class。感谢关于插件的提示,我也一定会尝试,因为通过阅读其文档,它似乎与我的案例相匹配。但是,在我的帖子中的链接中,经过您的修复后,它现在可以编译了,但我得到了错误(在webapp中运行测试时):
java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/classes/struts.xml]
。我在部署的war中检查struts.xml是否位于该位置,相对于war的根。前缀斜杠告诉我Spring试图从根搜索。为什么
final StrutsSpringObjectFactory ssf = new StrutsSpringObjectFactory("auto",
      "true", "false", servletContext, "false", dispatcher.getContainer());