我写了一个jsp文件来从mysql服务器获取我的书单。这是我的代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%-- set Data Source --%>
<sql:setDataSource
var="test"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"
user="root"
password="123456"/>
<sql:query dataSource="${test}" var="category" sql="select * from category"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Book List</title>
</head>
<body>
<table>
<c:forEach items="${category.rows}" var="row">
<c:forEach items="${rows}" var="col">
<tr>
<td>${col.value}</td>
<c:set var="column" value="${col.value}" />
<%
String col = (String) pageContext.getAttribute("column").toString();
String strQuery = "select * from bookstore where Category='" + col + "'";
pageContext.setAttribute("strQuery", strQuery); %>
<sql:query var="Book" sql="${strQuery}" />
<c:forEach items="${Book.rows}" var="row1">
<c:forEach items="${row1}" var="col1">
<td>${col1.key}:</td><td>${col1.value}</td>
</c:forEach>
</c:forEach>
</tr>
</c:forEach>
</c:forEach>
</table>
</body>
</html>
当我编译它时,我没有错误。我试着在Tomcat服务器上运行它,我得到了这个:
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.BookList_jsp
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:176)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.ClassNotFoundException: org.apache.jsp.BookList_jsp
java.net.URLClassLoader.findClass(Unknown Source)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:131)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:62)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:171)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
有时,当我多次刷新页面时,我也得到了这个:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [70] in the generated java file: [C:\Users\Hoàng\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\Web_Sample\org\apache\jsp\BookList_jsp.java]
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
An error occurred at line: 21 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
18: </head>
19: <body>
20: <table>
21: <c:forEach items="${category.rows}" var="row">
22: <c:forEach items="${rows}" var="col">
23: <tr>
24: <td>${col.value}</td>
An error occurred at line: 21 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
18: </head>
19: <body>
20: <table>
21: <c:forEach items="${category.rows}" var="row">
22: <c:forEach items="${rows}" var="col">
23: <tr>
24: <td>${col.value}</td>
An error occurred at line: 22 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
19: <body>
20: <table>
21: <c:forEach items="${category.rows}" var="row">
22: <c:forEach items="${rows}" var="col">
23: <tr>
24: <td>${col.value}</td>
25: <c:set var="column" value="${col.value}" />
An error occurred at line: 22 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
19: <body>
20: <table>
21: <c:forEach items="${category.rows}" var="row">
22: <c:forEach items="${rows}" var="col">
23: <tr>
24: <td>${col.value}</td>
25: <c:set var="column" value="${col.value}" />
An error occurred at line: 25 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
22: <c:forEach items="${rows}" var="col">
23: <tr>
24: <td>${col.value}</td>
25: <c:set var="column" value="${col.value}" />
26: <%
27: String col = (String) pageContext.getAttribute("column").toString();
28: String strQuery = "select * from bookstore where Category='" + col + "'";
An error occurred at line: 25 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
22: <c:forEach items="${rows}" var="col">
23: <tr>
24: <td>${col.value}</td>
25: <c:set var="column" value="${col.value}" />
26: <%
27: String col = (String) pageContext.getAttribute("column").toString();
28: String strQuery = "select * from bookstore where Category='" + col + "'";
An error occurred at line: 31 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
28: String strQuery = "select * from bookstore where Category='" + col + "'";
29: pageContext.setAttribute("strQuery", strQuery); %>
30: <sql:query var="Book" sql="${strQuery}" />
31: <c:forEach items="${Book.rows}" var="row1">
32: <c:forEach items="${row1}" var="col1">
33: <td>${col1.key}:</td><td>${col1.value}</td>
34: </c:forEach>
An error occurred at line: 31 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
28: String strQuery = "select * from bookstore where Category='" + col + "'";
29: pageContext.setAttribute("strQuery", strQuery); %>
30: <sql:query var="Book" sql="${strQuery}" />
31: <c:forEach items="${Book.rows}" var="row1">
32: <c:forEach items="${row1}" var="col1">
33: <td>${col1.key}:</td><td>${col1.value}</td>
34: </c:forEach>
An error occurred at line: 32 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
29: pageContext.setAttribute("strQuery", strQuery); %>
30: <sql:query var="Book" sql="${strQuery}" />
31: <c:forEach items="${Book.rows}" var="row1">
32: <c:forEach items="${row1}" var="col1">
33: <td>${col1.key}:</td><td>${col1.value}</td>
34: </c:forEach>
35: </c:forEach>
An error occurred at line: 32 in the jsp file: /BookList.jsp
The method getELContext() is undefined for the type PageContext
29: pageContext.setAttribute("strQuery", strQuery); %>
30: <sql:query var="Book" sql="${strQuery}" />
31: <c:forEach items="${Book.rows}" var="row1">
32: <c:forEach items="${row1}" var="col1">
33: <td>${col1.key}:</td><td>${col1.value}</td>
34: </c:forEach>
35: </c:forEach>
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:198)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:450)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我尝试添加taglibs-standard-impl-1.2.5.jar,jstl-1.2.jar,jsp-api-2.0.jar但没有改变。 你能给我一些建议吗?非常感谢!
答案 0 :(得分:0)
要使其发挥作用,几乎无需进行任何更改。请尝试下面的代码
<table>
<c:forEach items="${category.rows}" var="row">
<c:forEach items="${row}" var="col"> -> Change {rows} to {row}
<tr>
<td>${col.value}</td>
<c:set var="column" value="${col.value}" />
<%
String col = (String) pageContext.getAttribute("column").toString();
String strQuery = "select * from bookstore where Category='" + col + "'"; //Make sure you pass relevant value
pageContext.setAttribute("strQuery", strQuery);
%>
<sql:query dataSource="${test}" var="Book" sql="${strQuery}" /> -> Add datasource
<c:forEach items="${Book.rows}" var="row1">
<c:forEach items="${row1}" var="col1">
<td>${col1.key}:</td><td>${col1.value}</td>
</c:forEach>
</c:forEach>
</tr>
</c:forEach>
</c:forEach>
</table>