Struts无法使用spring生成的bean id引用action bean来进行依赖注入

时间:2014-12-06 18:15:33

标签: spring struts2 spring-bean

我正在尝试将Spring与struts集成为我的控制器。 Spring会根据applicationContext.xml中的配置初始化bean,但是当我将bean id作为struts.xml中的action类引用时,它无法检测到。 这是我的 struts.xml

 <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

    <struts>

        <package name="default" extends="struts-default">

            <action name="getHome" class="baseAction" method="getToHome">
                <result name="success">/jsp/bcHome.jsp</result>
            </action>


        </package>

    </struts>

这是我的 applicationContext.xml

<?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.5.xsd">

        <bean id="baseAction" class="com.localityindia.bc.action.BaseAction">
            <property name="baseService" ref="baseService" />           
        </bean>

        <bean id="baseService" class="com.localityindia.bc.service.BaseServiceImpl">
            <property name="baseDao" ref="baseDao"/>
        </bean>

        <bean id="baseDao" class="com.localityindia.bc.dao.BaseDaoImpl" />

    </beans>

这是我的动作类

import com.localityindia.bc.service.BaseService;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

private BaseService baseService;

public String getToHome(){
    System.out.println("baseService : "+baseService);
    System.out.println("Inside Action... getting home page for you.");
//  baseService.getHomeDetails();
    return SUCCESS;
}

public void setBaseService(BaseService baseService) {
    System.out.println("Setting srevice : "+baseService);
    this.baseService = baseService;
}

}

这是我的输出:

Setting srevice : com.localityindia.bc.service.BaseServiceImpl@1bf72d9
Dec 06, 2014 11:13:20 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 766 ms
Dec 06, 2014 11:13:21 PM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter struts2
Unable to load configuration. - action -                 file:/D:/personal_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/BuyerComp            anion/WEB-INF/classes/struts.xml:10:65
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:450)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFi    lter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4615)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5222)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: Unable to load configuration. - action -        file:/D:/personal_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/BuyerComp    anion/WEB-INF/classes/struts.xml:10:65
at        com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
... 14 more
Caused by: Action class [baseAction] not found - action -     file:/D:/personal_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/BuyerComp    anion/WEB-INF/classes/struts.xml:10:65
at     com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvi    der.java:434)
at     com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider    .java:378)
at     com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvide    r.java:495)
at     com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvi    der.java:286)
at     org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider    .java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:2    34)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
... 16 more

Dec 06, 2014 11:13:21 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error filterStart
Dec 06, 2014 11:13:21 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/BuyerCompanion] startup failed due to previous errors
Dec 06, 2014 11:13:21 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Dec 06, 2014 11:13:21 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing Root WebApplicationContext: startup date [Sat Dec 06 23:13:19 IST 2014]; root of                 
Dec 06, 2014 11:13:21 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-9090"]
Dec 06, 2014 11:13:21 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Dec 06, 2014 11:13:21 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5493 ms

请建议我该怎么做才能让bean id引用与struts.xml中的动作映射一起使用

1 个答案:

答案 0 :(得分:0)

你必须在类路径中拥有Struts 2 Spring插件,因此Spring会实例化Struts 2动作。