Java 如何使用Struts 2在Apache Tomcat的web.xml中创建MySQL数据库连接

Java 如何使用Struts 2在Apache Tomcat的web.xml中创建MySQL数据库连接,java,mysql,tomcat,servlets,struts2,Java,Mysql,Tomcat,Servlets,Struts2,我想在应用程序级别建立数据库连接,所以我想在ApacheTomcat服务器的web.xml文件中创建连接。我正在使用Struts2 MVC框架开发我们的应用程序。实际上,我不想在每个Java文件上创建数据库连接。所以,请给我一个如何在应用程序中建立数据库连接的建议 我正在尝试在web.xml中创建一个连接,但是在connection conn=ds.getConnection()行显示了一个类似java.lang.NullPointerException的错误。所有代码都显示在下面 META-I

我想在应用程序级别建立数据库连接,所以我想在ApacheTomcat服务器的
web.xml
文件中创建连接。我正在使用Struts2 MVC框架开发我们的应用程序。实际上,我不想在每个Java文件上创建数据库连接。所以,请给我一个如何在应用程序中建立数据库连接的建议

我正在尝试在
web.xml
中创建一个连接,但是在
connection conn=ds.getConnection()行显示了一个类似
java.lang.NullPointerException
的错误。所有代码都显示在下面

META-INFO/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>

<Resource name="jdbc/dbmy" auth="Container" type="javax.sql.DataSource"
           maxActive="50" maxIdle="30" maxWait="10000"
           username="root" password="mysql" 
           driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/dbmy"/>
</Context>
我的动作课是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MY</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>

<resource-ref>
<description>MySQL Datasource</description>
<res-ref-name>jdbc/dbmy</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.sql.DataSource;

import com.opensymphony.xwork2.*;


public class GEtResponseObject extends ActionSupport {

/**
 * 
 */
private static final long serialVersionUID = 1L;
ServletContext context =null;
PreparedStatement ps =null;
ResultSet rs =null;


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

public String execute() {       
 try{
     Connection conn = ds.getConnection(); //At this line, A java.lang.NullPointerException error is being occured.
     ps = conn.prepareStatement("select * from dbmy.mytable ");
     rs = ps.executeQuery();
     if (rs.next()) {
        System.out.println(rs.getString("mycolom"));
     }           
 }
 catch(SQLException e)
 {
      e.printStackTrace();       
 }      
  return Action.SUCCESS;
 }
}
启用ActionBean属性毫无意义。如果您需要有关注入资源的更多信息,您应该阅读一篇文章。而是创建一个
ServletContextListener
,并将注释放在那里。关于上下文初始化的事件集上下文属性

public class MyServletContextListener implements ServletContextListener {

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

  @Override
  public void contextInitialized(ServletContextEvent servletContextEvent) {
    System.out.println("contextInitialized");
    ServletContext context = servletContextEvent.getServletContext();
    context.setAttribute("ds",ds);
  }

  @Override
  public void contextDestroyed(ServletContextEvent servletContextEvent) {
    System.out.println("contextDestroyed");

  }
}
web.xml
(应在web-INF中):

<listener>
  <listener-class>com.servlet.MyServletContextListener</listener-class>
</listener>
ds = (DataSource)ServletActionContext.getServletContext().getAttribute("ds");