Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @自动连线在struts2和x2B上不起作用;春天_Java_Spring_Struts2_Autowired_Struts2 Spring Plugin - Fatal编程技术网

Java @自动连线在struts2和x2B上不起作用;春天

Java @自动连线在struts2和x2B上不起作用;春天,java,spring,struts2,autowired,struts2-spring-plugin,Java,Spring,Struts2,Autowired,Struts2 Spring Plugin,我想制作一个基于struts2和spring的web应用程序。首先,我测试了@Autowired是否对struts2有效。但事实并非如此,数据源为空。我不知道怎样才能修好它。请给我一个信息 HelloWorld.java package example; import java.sql.Connection; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.beans.factory.annot

我想制作一个基于struts2和spring的web应用程序。首先,我测试了@Autowired是否对struts2有效。但事实并非如此,数据源为空。我不知道怎样才能修好它。请给我一个信息

HelloWorld.java

package example;
import java.sql.Connection;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
@Component
public class HelloWorld extends ActionSupport {
    @Autowired
    private BasicDataSource dataSource;
    public String execute() throws Exception {
        Connection con = dataSource.getConnection();
        con.close();
        return SUCCESS;
    }
}
applicationContext.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:mem" />
    <property name="maxActive" value="10" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<context:annotation-config />
<context:component-scan base-package="example" />

web.xml

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

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

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

支柱2
org.apache.struts2.dispatcher.ng.filter.strutspreadexecutefilter
支柱2
/*
org.springframework.web.context.ContextLoaderListener
struts.xml

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="example" namespace="/example" extends="struts-default">
    <action name="HelloWorld" class="example.test.HelloWorld">
        <result>/example/HelloWorld.jsp</result>
    </action>
</package>

/示例/HelloWorld.jsp

要使
@Autowired
在struts应用程序中工作,您需要满足以下条件:

确保类路径中有struts2和spring插件

将以下行放入
strtus.xml

<struts>
  <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
  ... 
</struts>
并在
struts.xml
文件中提供bean id,而不是提供action类

<action name="edit" class="editAction" method="input">
    <result name="input">/edit.jsp</result>
</action>  

如果Spring没有管理对象,
@Autowired
目标只能
null
。查看sprint struts集成桥。现在Struts正在创建对象,而不是Spring;2.实体类别;3.将这些实体或组件注册到
applicationContext.xml
中。请检查!您似乎没有将它们注册到
xml
,请进行注册或使用
软件包扫描
。非常感谢。根据您的回答,我修改了我的代码,并在action类中添加了“setDataSource”,最后我的代码成功了。@Autowired正在使用以下配置为我工作:struts.objectFactory.spring.autoWire=no,位于struts.properties文件的struts.properties.xml中
<action name="edit" class="editAction" method="input">
    <result name="input">/edit.jsp</result>
</action>