How to get a container-managed DataSource in the Structures action

I am new to using the Struts 2 Framework.

I need to use a DataSource object in the Struts action class. My platform is Tomcat 8 (Servlet 3.1), and I install Resource in context.xml.

I can inject a container-driven DataSource into a servlet using the @Resource annotation.

I tried this way. I create a ServletContextListener and insert a DataSource into this listener. I set this data source for the application area object in the context of the Initialized method.

@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) { }

}

After that, I go to the application area and get this data source from the Struts action methods.

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";
}

My question is:

  • Is it possible to get a DataSource object in an action object of objects?
  • Am I trying to make the correct path in struts?
+4
1

Struts2-CDI Plugin 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> 

2. Tomcat, context.xml web.xml CDI.

2.1 context.xml

<Resource name="BeanManager" auth="Container"
    type="javax.enterprise.inject.spi.BeanManager" 
    factory="org.jboss.weld.resources.ManagerObjectFactory" />

2.2 web.xml

  <resource-env-ref>
    <resource-env-ref-name>BeanManager</resource-env-ref-name>
    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
  </resource-env-ref>

3.

DataSource ServletContext ResourceProducer. , DataSource , .

CDI Struts.

@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");
    }

}

4. Inject DataSource Model Producer

@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);
}

5. Struts 2

@Inject
@DataModel(Employee.class)
private Model<Employee> empModel;

public String welcome() {

    list = empModel.getAll();

    return "welcome";
}
+2

All Articles