javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何迭代提供的“项目”在

发布于 2025-01-01 21:17:43 字数 2626 浏览 0 评论 0 原文

我有一个保存结果的 Bean。我需要使用 JSTL 对其进行迭代并呈现结果。这是 bean:

public class DetResults
{
    private List<String> headings;
    private List<Class<?>> types;
    private List<Object[]> data;

    public DetResults() {}

    public List<String> getHeadings() { return this.headings; }
    public String getHeading(int which) { return this.headings.get(which); }

    public List<Class<?>> getTypes() { return this.types; }
    public Class<?> getType(int which) { return this.types.get(which); }

    public List<Object[]> getData( ) { return this.data; }
    public Object[] getDataAtRow( int row ) { return this.data.get(row); }


    public void setHeadings( List<String> val ) { this.headings = val; }
    public void setHeadings( String[] val ) { this.headings = Arrays.asList(val); }
    public void addHeading( String val ) 
    { 
        if( this.headings == null ) this.headings = new ArrayList<String>();
        this.headings.add(val); 
    }

    public void setTypes( List<Class<?>> val ) { this.types = val; }
    public void setTypes( Class<?> val[] ) { this.types = Arrays.asList(val); }
    public void addType( Class<?> val ) 
    {
        if( this.types == null ) this.types = new ArrayList<Class<?>>();
        this.types.add(val); 
    }


    public void setData( List<Object[]> val ) { this.data = val; }

    // allow NPE to get thrown
    public void setDataAtRow( Object[] val, int row ) { this.data.set(row, val); }

    public void appendDataRow( Object[] val ) 
    {
        if( data == null ) data = new ArrayList<Object[]>(); 
        this.data.add(val); 
    }

    public int getColumnCount() { return this.headings!=null?this.headings.size():0; }

}

这是将 bean 设置到 JSP 的处理程序:

DetResults results = detDAO.fetchDetResults(paramBean);
request.setAttribute("results", results);
action.setJspURI(".../.jsp");

我尝试按如下方式显示它:

<c:forEach var="results" items="${results}">
    ${results.heading}
</c:forEach>

但它引发了以下异常:

由以下原因引起:javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何迭代中提供的“项目”

如果我将结果记录在我的处理程序页面上,如下所示:

System.out.println( "\n\nthere are " + results.getColumnCount() + " columns in the result set" );
for( int i=0; i<results.getColumnCount(); i++ )
{
    System.out.println( results.getHeading(i) + " --> " + results.getType(i) );
}

日志记录似乎在服务器上显示良好。

I have a Bean that holds the results. I need to use JSTL to iterate over it and present the results. Here is the bean:

public class DetResults
{
    private List<String> headings;
    private List<Class<?>> types;
    private List<Object[]> data;

    public DetResults() {}

    public List<String> getHeadings() { return this.headings; }
    public String getHeading(int which) { return this.headings.get(which); }

    public List<Class<?>> getTypes() { return this.types; }
    public Class<?> getType(int which) { return this.types.get(which); }

    public List<Object[]> getData( ) { return this.data; }
    public Object[] getDataAtRow( int row ) { return this.data.get(row); }


    public void setHeadings( List<String> val ) { this.headings = val; }
    public void setHeadings( String[] val ) { this.headings = Arrays.asList(val); }
    public void addHeading( String val ) 
    { 
        if( this.headings == null ) this.headings = new ArrayList<String>();
        this.headings.add(val); 
    }

    public void setTypes( List<Class<?>> val ) { this.types = val; }
    public void setTypes( Class<?> val[] ) { this.types = Arrays.asList(val); }
    public void addType( Class<?> val ) 
    {
        if( this.types == null ) this.types = new ArrayList<Class<?>>();
        this.types.add(val); 
    }


    public void setData( List<Object[]> val ) { this.data = val; }

    // allow NPE to get thrown
    public void setDataAtRow( Object[] val, int row ) { this.data.set(row, val); }

    public void appendDataRow( Object[] val ) 
    {
        if( data == null ) data = new ArrayList<Object[]>(); 
        this.data.add(val); 
    }

    public int getColumnCount() { return this.headings!=null?this.headings.size():0; }

}

Here is the handler that will set the bean to the JSP:

DetResults results = detDAO.fetchDetResults(paramBean);
request.setAttribute("results", results);
action.setJspURI(".../.jsp");

I tried to display it as follows:

<c:forEach var="results" items="${results}">
    ${results.heading}
</c:forEach>

But it threw the following exception:

Caused by: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

If I log the results on my handler page like this:

System.out.println( "\n\nthere are " + results.getColumnCount() + " columns in the result set" );
for( int i=0; i<results.getColumnCount(); i++ )
{
    System.out.println( results.getHeading(i) + " --> " + results.getType(i) );
}

The logging seems to show fine on the server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╄→承喏 2025-01-08 21:17:43

引起:javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何迭代中提供的“项目”

< ;c:forEach items> 未引用可迭代的有效对象。该对象应该是一个 Object[] (一个普通数组),一个 集合, 迭代器枚举地图字符串(另请参阅源代码)。任何其他内容都不能通过 进行迭代。您的 DetResults 类不是上述类型的实例,因此它将失败。

您的 DetResults 类看起来不正确。它看起来基本上就像一颗神豆,具有多个单独实体的所有属性的集合。这是不对的。一个 Bean 类最多应该代表一个实体。重写您的 DetResults 类,以便您基本上最终得到一个完整的 javabean 集合:

List<DetResult> results = detDAO.fetchDetResults(paramBean);

以便您可以按如下方式访问它:

<c:forEach items="${results}" var="result">
    ${result.heading}
    <c:forEach items="${result.data}" var="dataItem">
        ${dataItem}
    </c:forEach>
</c:forEach>

请注意,我已经修复了 属性也是如此,将其命名为与作用域中现有对象相同的名称是不正确的。它只会发生冲突。

如果您确实坚持保持 DetResults bean 不变,您可以按如下方式访问它:

<c:forEach begin="0" end="${results.columnCount}" varStatus="loop">
    ${results.headings[loop.index]}
    <c:forEach items="${results.data[loop.index]}" var="dataItem">
        ${dataItem}
    </c:forEach>
 </c:forEach>

另请参阅:

Caused by: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

That will happen when the <c:forEach items> does not refer a valid object over which it can iterate. The object should be an Object[] (a plain array), a Collection, Iterator, Enumeration, Map or String (see also source code). Anything else can't be iterated by <c:forEach>. Your DetResults class is not an instance of either of the aforementioned types, so it will fail.

Your DetResults class doesn't look right. It look basically like one God bean with a collection of all properties of multiple individual entities. This is not right. A bean class should represent at most one entity. Rewrite your DetResults class so that you basically end up with with a fullworthy collection of javabeans:

List<DetResult> results = detDAO.fetchDetResults(paramBean);

so that you can access it as follows:

<c:forEach items="${results}" var="result">
    ${result.heading}
    <c:forEach items="${result.data}" var="dataItem">
        ${dataItem}
    </c:forEach>
</c:forEach>

Note that I have fixed the <c:forEach var> attribute as well, it is not right to give it the same name as an existing object in the scope. It would only clash.

If you really insist to keep your DetResults bean as it is, you could access it as follows:

<c:forEach begin="0" end="${results.columnCount}" varStatus="loop">
    ${results.headings[loop.index]}
    <c:forEach items="${results.data[loop.index]}" var="dataItem">
        ${dataItem}
    </c:forEach>
 </c:forEach>

See also:

回忆躺在深渊里 2025-01-08 21:17:43

您应该能够迭代标题

<tr>
<c:foreach var="heading" items="${results.headings}">
  <th>${heading}</th>
</c:foreach>
</tr>

然后遍历数据......

<c:foreach var="row" items="${results.data}">
  <tr>
  <c:foreach var="i" items="${row}">
    <td>${i}</td>
  </c:foreach>
  </tr>
</c:foreach>

或者类似的东西?

You should be able to iterate over the headings

<tr>
<c:foreach var="heading" items="${results.headings}">
  <th>${heading}</th>
</c:foreach>
</tr>

And then over the data...

<c:foreach var="row" items="${results.data}">
  <tr>
  <c:foreach var="i" items="${row}">
    <td>${i}</td>
  </c:foreach>
  </tr>
</c:foreach>

Or something along those lines?

恋你朝朝暮暮 2025-01-08 21:17:43

检查 servlet 类中的以下代码:

request.setAttribute("productList", products);

并且在 中的 JSP 页面中应匹配相同的代码:

<c:forEach var= "tempProducts" items= "${productList}">

注意:"productList" 名称应在 1 点 1 中完全匹配和 2
那么它就会的。

这对我有用。

Check this below code in servlet class:

request.setAttribute("productList", products);

And the same should match in JSP page in <c:forEach:

<c:forEach var= "tempProducts" items= "${productList}">

NOTE: "productList" name should match exactly in 1 point 1 and 2
then it will.

It worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文