【问题标题】:Invoke while loop with expression language使用表达式语言调用 while 循环
【发布时间】:2017-10-09 10:59:15
【问题描述】:

我的 jsp 文件中有以下 while 循环:

<%
    List sorts = (List)request.getAttribute("sorts");
    Iterator it = sorts.iterator();
    while(it.hasNext()) {
        out.print(it.next());
}
%>

如何在此处使用表达式语言避免 JSP 文件中的 Java 代码?

我尝试将it.next()) 参数保存在变量中,但没有成功。

【问题讨论】:

    标签: java html jsp el


    【解决方案1】:

    表达式语言中不能使用while循环。可以使用foreach循环代替while循环。

    foreach 循环的语法如下-

    <c:forEach var="name of scoped variable"
           items="Colleciton,List or Array" >
    

    其中c是前缀

    var 是从集合中存储数据的变量名。

    items 是集合或列表。

    【讨论】:

    • 你知道 forEach 循环是自增循环。不需要使用作用域变量的名称定义下一个和访问值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2018-03-10
    • 2015-02-13
    • 2013-03-22
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多