如何使用 JSTL sql 标签

发布于 2024-12-08 22:48:29 字数 2616 浏览 0 评论 0原文

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

<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/cloud" user="root"  password="root"
scope="session" /> 

<sql:query var="qryProvider" >
    SELECT * FROM `provider`;
</sql:query>

<table>
    <c:forEach var="row" items="${qryProvider.rows}">
        <tr>
            <td>${row.display_name}</td>

        </tr>
    </c:forEach>
</table>

我收到以下错误:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /testJSTL.jsp at line 8

5: url="jdbc:mysql://localhost:3306/cloud" user="root"  password="root"
6: scope="session" /> 
7:  
8: <sql:query var="qryProvider" >
9:     SELECT * FROM `provider`;
10: </sql:query>
11:  


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.lang.NullPointerException"
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
    org.apache.jsp.testJSTL_jsp._jspService(testJSTL_jsp.java:94)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我尝试遵循 this tut,但它给了我一个错误如果我写

<sql:query var="qryProvider" dataSource="${dataSource}" >

这个我是新手,有人可以指出我正确的方向吗?

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

<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/cloud" user="root"  password="root"
scope="session" /> 

<sql:query var="qryProvider" >
    SELECT * FROM `provider`;
</sql:query>

<table>
    <c:forEach var="row" items="${qryProvider.rows}">
        <tr>
            <td>${row.display_name}</td>

        </tr>
    </c:forEach>
</table>

I got the following error:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /testJSTL.jsp at line 8

5: url="jdbc:mysql://localhost:3306/cloud" user="root"  password="root"
6: scope="session" /> 
7:  
8: <sql:query var="qryProvider" >
9:     SELECT * FROM `provider`;
10: </sql:query>
11:  


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:456)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Unable to get connection, DataSource invalid: "java.lang.NullPointerException"
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
    org.apache.jsp.testJSTL_jsp._jspService(testJSTL_jsp.java:94)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

I tried to follow this tut, but it gives me an error if I write

<sql:query var="qryProvider" dataSource="${dataSource}" >

I'm new to this, could someone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲歌长辞 2024-12-15 22:48:29

我的建议是完全忘记 标签,并使用纯 Java 进行所有数据库操作(在您首选的 MVC 框架的 servlet 或操作中)。该 servlet 将构建一个 bean 实例列表,准备好由 JSP 显示。使用 RequestDispatcher 将请求从 servlet 分派到 JSP。

甚至官方 Java EE 教程也说:

表 7-7 中列出的用于访问数据库的 JSTL SQL 标记是
专为快速原型设计和简单应用而设计。用于生产
应用程序、数据库操作通常封装在
JavaBeans 组件。

My advice would be to forget about the <sql> tags completely, and to make all your database operations in plain Java (in a servlet or action of your preferred MVC framework). This servlet would build a list of bean instances, ready to be displayed by your JSP. Use RequestDispatcher to dispatch the request to the JSP from the servlet.

Even the official Java EE tutorial says:

The JSTL SQL tags for accessing databases listed in Table 7-7 are
designed for quick prototyping and simple applications. For production
applications, database operations are normally encapsulated in
JavaBeans components.

埋葬我深情 2024-12-15 22:48:29
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>   


<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/cloud" user="root"  password="root"
scope="session" /> 


<sql:query var="qryProvider" dataSource="${dataSource}">

    SELECT * FROM provider;
</sql:query>

<table>
    <c:forEach var="row" items="${qryProvider.rows}">
        <tr>
            <td>${row.display_name}</td>

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


<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/cloud" user="root"  password="root"
scope="session" /> 


<sql:query var="qryProvider" dataSource="${dataSource}">

    SELECT * FROM provider;
</sql:query>

<table>
    <c:forEach var="row" items="${qryProvider.rows}">
        <tr>
            <td>${row.display_name}</td>

        </tr>
    </c:forEach>
</table>
余罪 2024-12-15 22:48:29
SELECT * FROM provider;

替换你的代码并导入sql包

或者
导入sql包和
消除 ;从你的 select 语句中

SELECT * FROM provider;

Replace your code and import sql package

or
import sql package and
Remove ; from your select statement

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文