在JSP中不使用Wildfly DataSource

时间:2018-06-25 03:01:50

标签: java jsp wildfly datasource

我正在使用Wildfly 13。 使用管理控制台添加了连接到DB2数据库的数据源。 这是standalone-fa-full.xml的一部分

<subsystem xmlns="urn:jboss:domain:datasources:5.0">
            <datasources>
                <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="ExampleDS" enabled="true" use-java-context="true" statistics-enabled="true">
                    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                    <driver>h2</driver>
                    <security>
                        <user-name>sa</user-name>
                        <password>sa</password>
                    </security>
                </datasource>
                <datasource jndi-name="java:/QuizDS" pool-name="QuizDS" enabled="true" statistics-enabled="true">
                    <connection-url>jdbc:db2://ip-address:18012/ogcscdb1</connection-url>
                    <driver-class>com.ibm.db2.jcc.DB2Driver</driver-class>
                    <driver>db2jcc4.jar</driver>
                    <pool>
                        <min-pool-size>0</min-pool-size>
                        <max-pool-size>50</max-pool-size>
                    </pool>
                    <security>
                        <user-name>Testing</user-name>
                        <password>Testing</password>
                    </security>
                    <validation>
                        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.db2.DB2ValidConnectionChecker"/>
                        <background-validation>true</background-validation>
                        <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.db2.DB2StaleConnectionChecker"/>
                        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.db2.DB2ExceptionSorter"/>
                    </validation>
                </datasource>
                <drivers>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                    </driver>
                </drivers>
            </datasources>
        </subsystem>

我试图编写一个简单的JSP程序来加载数据库。这是JSP文件

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/QuizDS">
select name from sysibm.systables;
</sql:query>

<html>
  <head>
    <title>DB Test</title>
  </head>
  <body>

  <h2>Results</h2>

<c:forEach var="row" items="${rs.rows}">
     ${row.name}<br/>
</c:forEach>

  </body>
</html>

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Testing2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/QuizDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

</web-app>

我尝试通过管理控制台部署我的war文件。部署成功,但是当我加载JSP页面时,显示以下错误:

2018-06-25 11:04:31,950 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /Testing2/: javax.servlet.ServletException: javax.servlet.jsp.JspException: 
select name from sysibm.systables;
: Schema "SYSIBM" not found; SQL statement:

select name from sysibm.systables;
 [90079-193]
    at io.undertow.jsp@2.0.3.Final//org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:886)
    at io.undertow.jsp@2.0.3.Final//org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:818)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:129)
    at io.undertow.jsp@2.0.3.Final//org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

但是在管理控制台中,测试连接成功。

请问是否有人知道为什么会这样?

1 个答案:

答案 0 :(得分:0)

最后,我通过在WEB-INF下添加jboss-web.xml来解决该问题

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <resource-ref>
        <res-ref-name>jdbc/QuizDS</res-ref-name>
        <jndi-name>java:/QuizDS</jndi-name>
    </resource-ref>
</jboss-web>