如何使用 JSTL sql 标签
<%@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的建议是完全忘记
标签,并使用纯 Java 进行所有数据库操作(在您首选的 MVC 框架的 servlet 或操作中)。该 servlet 将构建一个 bean 实例列表,准备好由 JSP 显示。使用RequestDispatcher
将请求从 servlet 分派到 JSP。甚至官方 Java EE 教程也说:
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. UseRequestDispatcher
to dispatch the request to the JSP from the servlet.Even the official Java EE tutorial says:
替换你的代码并导入sql包
或者
导入sql包和
消除 ;从你的 select 语句中
Replace your code and import sql package
or
import sql package and
Remove ; from your select statement