0

我正在使用 Spring 来管理 struts 2 动作 bean。我正在尝试从基于 XML 的配置迁移到基于注释的配置。我正在使用 struts2-spring-plugin,以便 struts 获得由 spring 管理的对象的句柄。以下是 myAction 的 applicationContext 文件中的内容。

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema
      /beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org
      /schema/context/spring-context-3.0.xsd">
    <context:annotation-config />
    <bean id="myAction123" class="com.action.MyAction" scope="prototype"></bean>
</beans>

在 struts2-tiles 配置文件中,我定义了请求映射。此映射使用对 spring 创建的 bean 的引用。class属性指的是spring bean定义中定义的id属性。

    <action name="myAction_*" method="{1}" class="myAction123">
        <result name="success" type="tiles">action.request.view</result>
    </action>

Action 类注释如下:

@Controller
public class MyAction {

    @Autowired
    public MyService myService;

    public MyService getMyService() {
        return myService;
    }

    public void setMyService(MyService myService) {
        this.myService = myService;
    }

    public String doSearch() {
        /* ... */
    }
}

请在下面找到堆栈跟踪,当我删除弹簧动作 bean 引用时由 struts 抛出。

09:10:37,852 ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/gas]  - Exception starting filter struts
Action class [volumeRequestAction] not found - action - file:/C:/dev/Workspace/GAS2/_GAS2WebApp/target/_GAS2WebApp-2.0/WEB-INF/classes/struts2-tiles.xml:701:81
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:374)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:329)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:429)
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: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:221)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:302)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:78)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3635)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4222)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
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:120)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
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:700)
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:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
Oct 5, 2012 9:10:37 AM org.apache.catalina.core.StandardContext start

我了解我没有放置正确的注释,这是导致此问题的原因。尽管我已经尝试阅读了很多关于这个的内容,但我仍然对迁移这个特定部分感到困惑。我无法确定我是否会为此使用 struts 2 注释或 spring 注释。任何帮助表示赞赏。谢谢你。

4

1 回答 1

1

您还需要这条线

<context:component-scan base-package="my.package.path" />

在你的 application-context.xml 文件中告诉 Spring 去哪里寻找注释。使用 @Controller 注解没有任何问题。

于 2012-10-05T13:31:40.287 回答