所以我正在使用MVC架构在JSP和Java servlet中编写Web程序,我必须使用自定义的forEach标记列出我从JSP页面上的数据库中检索的一堆项目。我写了标签类:
package ps6;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForEachTag extends SimpleTagSupport {
private Object[] items;
private String attributeName;
public void setItems(Object[] items) {
this.items = items;
}
public void setVar(String attributeName) {
this.attributeName = attributeName;
}
public void doTag() throws JspException, IOException {
for (int i=0; i < items.length; i++) {
getJspContext().setAttribute(attributeName, items[i]);
getJspBody().invoke(null);
}
}
}
.tld文件:
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>auction</short-name>
<tag>
<description>Outputs a list or something</description>
<name>forEach</name>
<tag-class>ps6.ForEachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<description>The array of elements</description>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<description>The name of the variable to which each entry is assigned</description>
<name>var</name>
<required>true</required>
</attribute>
</tag>
</taglib>
要完成工作的servlet:
package ps6;
import java.io.*;
import java.sql.Connection;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ViewAuctions
*/
@WebServlet("/ViewAuctions")
public class ViewAuctions extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ViewAuctions() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[][] items = getItems();
request.setAttribute("items", items);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/viewAuctions.jsp");
dispatcher.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
private String[][] getItems() {
try {
Connection connection = DB.openConnection();
Statement statement = connection.createStatement();
String query = "SELECT * FROM items WHERE open='1';";
ResultSet results = statement.executeQuery(query);
results.last();
int numItems = results.getRow();
results.first();
int numCols = 5;
String[][] items = new String[numItems][numCols];
for (int r=0; r < numItems; r++) {
items[r][0] = results.getString("itemNum");
items[r][1] = results.getString("username");
items[r][2] = results.getString("title");
items[r][3] = results.getString("description");
items[r][4] = results.getString("highBid");
results.next();
}
return items;
}
catch (SQLException sqle) {
throw new RuntimeException("Error accessing database: " + sqle);
}
}
}
最后是.jsp文件本身:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>See what's for sale</title>
</head>
<body>
<%@ taglib uri="/WEB-INF/auction.tld" prefix="auction" %>
<h1>Open Auctions:</h1>
<table border="1">
<tr><th>Item No.</th><th>Auction Owner</th><th>Title</th><th>Description</th><th>Highest Bid</th></tr>
<auction:forEach items="${items}" var="row">
<tr><auction:forEach items="${row}" var="data">
<td>${data}</td>
</auction:forEach>
</tr>
</auction:forEach>
</table>
但我一直得到这些错误,我真的不明白:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 16 in the jsp file: /WEB-INF/viewAuctions.jsp
Syntax error on token "[", delete this token
13:
14: <table border="1">
15: <tr><th>Item No.</th><th>Auction Owner</th><th>Title</th><th>Description</th><th>Highest Bid</th></tr>
16: <auction:forEach items="${items}" var="row">
17: <tr><auction:forEach items="${row}" var="data">
18: <td>${data}</td>
19: </auction:forEach>
An error occurred at line: 16 in the jsp file: /WEB-INF/viewAuctions.jsp
Ljava.lang.Object cannot be resolved to a type
13:
14: <table border="1">
15: <tr><th>Item No.</th><th>Auction Owner</th><th>Title</th><th>Description</th><th>Highest Bid</th></tr>
16: <auction:forEach items="${items}" var="row">
17: <tr><auction:forEach items="${row}" var="data">
18: <td>${data}</td>
19: </auction:forEach>
An error occurred at line: 16 in the jsp file: /WEB-INF/viewAuctions.jsp
Syntax error on token ";", delete this token
13:
14: <table border="1">
15: <tr><th>Item No.</th><th>Auction Owner</th><th>Title</th><th>Description</th><th>Highest Bid</th></tr>
16: <auction:forEach items="${items}" var="row">
17: <tr><auction:forEach items="${row}" var="data">
18: <td>${data}</td>
19: </auction:forEach>
An error occurred at line: 16 in the jsp file: /WEB-INF/viewAuctions.jsp
The method getValueFromPropertyEditorManager(Class<?>, String, String) in the type JspRuntimeLibrary is not applicable for the arguments ()
13:
14: <table border="1">
15: <tr><th>Item No.</th><th>Auction Owner</th><th>Title</th><th>Description</th><th>Highest Bid</th></tr>
16: <auction:forEach items="${items}" var="row">
17: <tr><auction:forEach items="${row}" var="data">
18: <td>${data}</td>
19: </auction:forEach>
An error occurred at line: 16 in the jsp file: /WEB-INF/viewAuctions.jsp
Syntax error, insert ")" to complete Expression
13:
14: <table border="1">
15: <tr><th>Item No.</th><th>Auction Owner</th><th>Title</th><th>Description</th><th>Highest Bid</th></tr>
16: <auction:forEach items="${items}" var="row">
17: <tr><auction:forEach items="${row}" var="data">
18: <td>${data}</td>
19: </auction:forEach>
我真的可以使用一些帮助来破译这些错误的含义,或者他们甚至指的是什么。它提到的那一行没有它抱怨的字符,我也看不出错误的来源,我也无法通过谷歌搜索错误找到任何帮助。任何见解将不胜感激。
答案 0 :(得分:1)
由于您的web.xml描述符声明错误,可能会发生这种情况。尝试为Servlet API设置正确的版本(可能是2.4)和正确的架构,如果没有正确设置的话。
答案 1 :(得分:0)
我不确定到底出了什么问题,但看起来JSP引擎错误地将类型Object[]
([Ljava.lang.Object;
)的二进制表示插入到源代码中。尝试将setItems()
的参数声明为Object
。