javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何遍历提供的“items” in< forEach> [英] javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>

查看:981
本文介绍了javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何遍历提供的“items” in< forEach>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拥有结果的Bean。我需要使用JSTL来遍历它并呈现结果。这里是bean:

  public class DetResults 
{
private List< String>标题;
private List< Class<?>>类型;
私人列表< Object []>数据;

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

//允许NPE被抛出
public void setDataAtRow(Object [] val,int row){this.data.set(row,val); ();}
$ b $ 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 =resultsitems =$ {results}> 
$ {results.heading}
< / c:forEach>

但它引发了以下异常:


引起:javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何遍历提供的items in< forEach>


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

  System.out.println(\\\\\\\\\\\\\\\\\\\\ 
for(int i = 0; i< results.getColumnCount(); i ++)
{
System.out.println(results.getHeading(i)+ - >+ results.getType(i));

$ / code>

记录似乎在服务器上显示正常。

解决方案


导致:javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何迭代在< forEach>


中提供了items < c:forEach items> 不会引用可迭代的有效对象。该对象应该是一个 Object [] (一个普通数组),一个 集合 Map Iterator ,<一个href =http://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html =nofollow noreferrer> Enumeration 字符串 (另见源代码)。其他任何东西都不能被< c:forEach> 迭代。您的 DetResults 类不是上述任何一种类型的实例,所以它会失败。



c $ c> DetResults 类看起来不正确。它看起来基本上像一个神豆,它具有多个单独实体的所有属性的集合。这个不对。一个bean类最多只能表示一个实体。重写你的 DetResults 类,这样你就可以最终得到一个完整的javabeans集合:

 列表与LT; 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 你可以像下面这样访问它:
$ b

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



另请参阅:








不相关 c>< c:forEach var> 属性不正确。您不应该将它与作用域中的现有对象同名。它只会冲突。但是,如果你不能解释错误信息,那么这是一个新的问题。


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.

解决方案

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, Map, Iterator, Enumeration 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>

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:


Unrelated to the concrete problem, the <c:forEach var> attribute is not right. You should not give it the same name as an existing object in the scope. It would only clash. But that's subject for a new question if you can't interpret the error message.

这篇关于javax.servlet.ServletException:javax.servlet.jsp.JspTagException:不知道如何遍历提供的“items” in&lt; forEach&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆