JSTL while循环(无脚本) [英] JSTL while loop (without scriptlets)

查看:190
本文介绍了JSTL while循环(无脚本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用JSP在不使用脚本的情况下创建类似while循环的结构?

Is there a way to create a while loop like structure with JSP, without using a scriptlet?

我问,因为我有一个类似链表的结构(特别是打印异常的原因链),因此AFAIK没有可用于forEach的迭代器接口.

I ask as I have a linked list-like structure (specifically, printing the cause chain for exceptions) which AFAIK does not have an iterator interface to use forEach on.

推荐答案

您可以通过遍历列表来实现它

You could do it by iterating over a list

<c:forEach var="entry" items="${requestScope['myErrorList']}">
${entry.message}<br/>
</c:forEach>

您可以使用类似下面的方法将异常及其原因转换为列表,稍后可以使用 forEach

You could have a method like the following to transform an exception and its causes into list that later could be shown using a forEach

public static List<Throwable> getExceptionList(Exception ex) {
  List<Throwable> causeList = new ArrayList<Throwable>();
  causeList.add(ex);
  Throwable cause = null;
  while ((cause = ex.getCause()) != null && !causeList.contains(cause)) {
    causeList.add(cause);
  }
  return causeList;
}

例如:

try {
  ...
} catch (... ex) {
  request.setAttribute("myErrorList", getExceptionList(ex));
}

这篇关于JSTL while循环(无脚本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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