Итак, я работаю над написанием веб-программы на JSP и Java-сервлетов, использующих архитектуру MVC, где мне нужно перечислить набор элементов, которые я извлекаю из базы данных на странице JSP, используя пользовательский тег forEach.Я написал тег класса:
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>
Сервлет для выполнения работы:
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>
Я мог бы действительно использовать некоторую помощь для расшифровки, что означают эти ошибки, или что онидаже ссылаясь на.В строке, в которой упоминается, нет символов, на которые она жалуется, и я не вижу, откуда могут возникнуть ошибки, и я не смог найти никакой помощи в поиске ошибки.Любая идея будет принята с благодарностью.