Java 如何在Structs操作中获取容器管理的数据源对象

Java 如何在Structs操作中获取容器管理的数据源对象,java,servlets,struts2,Java,Servlets,Struts2,我是新使用Struts 2框架的 我需要在Struts操作类中使用DataSource对象。 我的平台是Tomcat8(Servlet3.1),我在context.xml中设置了资源 我可以使用@Resource注释将容器管理的数据源对象注入servlet中 我试过这样做。 我创建了一个ServletContextListener,并将数据源注入到这个侦听器中。 我在contextInitialized方法中将此数据源设置为application scope对象 @WebListener pub

我是新使用Struts 2框架的

我需要在Struts操作类中使用DataSource对象。 我的平台是Tomcat8(Servlet3.1),我在context.xml中设置了资源

我可以使用@Resource注释将容器管理的数据源对象注入servlet中

我试过这样做。 我创建了一个ServletContextListener,并将数据源注入到这个侦听器中。 我在contextInitialized方法中将此数据源设置为application scope对象

@WebListener
public class ResourceListener implements ServletContextListener {

    @Resource(name="jdbc/skill_db")
    private DataSource ds;

    public ResourceListener() { }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Start");
        sce.getServletContext().setAttribute("Datasource", ds);
        sce.getServletContext().setAttribute("dbConfigStream", sce.getServletContext().getResourceAsStream("/WEB-INF/database.properties"));
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) { }

}
之后,我访问应用程序范围并从Struts操作方法中获取此数据源

public String welcome() {

    Map<String, Object> application = ActionContext.getContext().getApplication();
    DataSource ds = (DataSource) application.get("Datasource");
    InputStream conf = (InputStream) application.get("dbConfigStream");

    Model<Employee> empModel = new BaseModel<Employee>(Employee.class, 
        Employee::convert, ds, conf);
    list = empModel.getAll();

    return "welcome";
}
公共字符串欢迎(){
映射应用程序=ActionContext.getContext().getApplication();
DataSource ds=(DataSource)application.get(“DataSource”);
InputStream conf=(InputStream)application.get(“dbConfigStream”);
模型empModel=新的基本模型(Employee.class,
Employee::convert、ds、conf);
list=empModel.getAll();
返回“欢迎”;
}
我的问题是:

  • 我可以在structs操作对象中获取数据源对象吗
  • 这就是我在struts中尝试的正确方法吗

  • 我通过Struts2 CDI插件尝试了我的需求 通过使用CDI,我可以注入依赖项

    1。我编辑项目的POM如下。

        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-cdi-plugin</artifactId>
            <version>2.3.24</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>2.2.15.Final</version>
        </dependency> 
    
    4。在模型生产者处注入数据源

    @Inject
    @DbResourse
    private DataSource ds;
    @Inject
    @DbConfiguration
    private InputStream dbConfig;
    
    @Produces
    @DataModel(Employee.class)
    public Model<Employee> getEmployeeModel() {
        return new BaseModel<Employee>(Employee.class, Employee::convert, ds, dbConfig);
    }
    
    @Inject
    @数据库资源
    私有数据源ds;
    @注入
    @数据库配置
    私有输入流dbConfig;
    @产生
    @数据模型(Employee.class)
    公共模型getEmployeeModel(){
    返回新的BaseModel(Employee.class、Employee::convert、ds、dbConfig);
    }
    
    5。Struts 2操作类中的注入模型

    @Inject
    @DataModel(Employee.class)
    private Model<Employee> empModel;
    
    public String welcome() {
    
        list = empModel.getAll();
    
        return "welcome";
    }
    
    @Inject
    @数据模型(Employee.class)
    私有模式;
    公共字符串欢迎(){
    list=empModel.getAll();
    返回“欢迎”;
    }
    
    为什么不将其注入到操作中?@AleksandrM我在Structs action类中尝试了资源注入。但它不起作用。当我通过调试模式检查时,数据源对象为空。请查看S2 cdi插件:。@AleksandrM谢谢!我试试看。我还需要在Struts2中使用DI。但我不想在这个项目中使用弹簧或焊接。非常感谢。对于spring,有S2-spring插件,但如果您不想使用spring,那么cdi是一个不错的选择。当你们解决了你们的问题后,你们可以回来,自己回答你们的问题,并接受它来帮助未来的访客。加油!兄弟。。。我为你的成功感到高兴。这可能会对其他像你一样愿意的人有所帮助。
    @ApplicationScoped
    public class ResourceProducer {
    
        @Resource(name="jdbc/skill_db")
        private DataSource datasource;
    
        @Inject
        private ServletContext servletContext;
    
    
        @Produces
        @DbResourse
        public DataSource getDatasource() {
            return datasource;
        }
    
        @Produces
        @DbConfiguration
        public InputStream getConfiguration() {
            return servletContext.getResourceAsStream("/WEB-INF/database.properties");
        }
    
    }
    
    @Inject
    @DbResourse
    private DataSource ds;
    @Inject
    @DbConfiguration
    private InputStream dbConfig;
    
    @Produces
    @DataModel(Employee.class)
    public Model<Employee> getEmployeeModel() {
        return new BaseModel<Employee>(Employee.class, Employee::convert, ds, dbConfig);
    }
    
    @Inject
    @DataModel(Employee.class)
    private Model<Employee> empModel;
    
    public String welcome() {
    
        list = empModel.getAll();
    
        return "welcome";
    }