Min*_*win 5 java servlets struts2
我是 Struts 2 框架的新手。
我需要在 Struts Action Class 中使用 DataSource 对象。我的平台是 Tomcat 8 (Servlet 3.1),我在 context.xml 中设置了 Resource。
我可以使用 @Resource 注释在 servlet 中注入容器管理的数据源对象。
我试过这种方式。我创建了一个 ServletContextListener 并在这个监听器中注入了 DataSource 。我在 contextInitialized 方法中将此数据源设置为应用程序范围对象。
@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) { }
}
Run Code Online (Sandbox Code Playgroud)
之后,我访问应用程序范围并从 Struts Action 方法中获取此数据源。
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";
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
我通过 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>
Run Code Online (Sandbox Code Playgroud)
2. 由于我使用 Tomcat,我需要将此代码添加到 context.xml 和 web.xml 中才能使用 CDI。
2.1 上下文.xml
<Resource name="BeanManager" auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory" />
Run Code Online (Sandbox Code Playgroud)
2.2 网页.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>
Run Code Online (Sandbox Code Playgroud)
3. 生成数据源
直接将 DataSource 对象和 ServletContext 注入到 ResourceProducer 类中。这样我不需要侦听器类将 DataSource 设置为应用程序范围,也不需要间接访问 servlet 上下文对象。
使用 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");
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
5. 在 Struts 2 Action 类中注入模型
@Inject
@DataModel(Employee.class)
private Model<Employee> empModel;
public String welcome() {
list = empModel.getAll();
return "welcome";
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
644 次 |
| 最近记录: |