开发者

Strange errors for custom tag in JSP file generating dynamic content

开发者 https://www.devze.com 2023-02-21 22:09 出处:网络
So I\'m working on writing a web program in JSP, and Java servlets using the MVC architecture, where I have to list a bunch of items I retrieve from a database on a JSP page, using a custom forEach ta

So I'm working on writing a web program in JSP, and Java servlets using the MVC architecture, where I have to list a bunch of items I retrieve from a database on a JSP page, using a custom forEach tag. I wrote the tag class:

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);
        }
    }
}

The .tld file:

<?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 ent开发者_运维技巧ry is assigned</description>
            <name>var</name>
            <required>true</required>   
        </attribute>
    </tag>
</taglib>

The servlet to do the work:

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);
        }

    }

}

and finally the .jsp file itself:

<%@ 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>

But I keep getting these errors I really don't understand:

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>

I could really use some help deciphering what these errors mean, or what they're even referring to. The line that it mentions doesn't have the characters it's complaining about, and I don't see where the errors could be coming from, and I haven't been able to find any help through googling the error. Any insight would be greatly appreciated.


This may happen because of a wrong declaration of your web.xml descriptor. Try setting the proper version for your Servlet API (probably 2.4) and the correct schema, if that is not properly set.


I'm not sure what exactly went wrong, but it looks like JSP engine erroneously inserted binary representation of type Object[] ([Ljava.lang.Object;) into source code. Try to declare argument of setItems() as Object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号