EL表达式和JSTL标签实现迭代

EL表达式用于获取数据,在jsp页面中可使用${标识符}的形式,通知JSP引擎调用pageContext.findAttribute()方法
如果没有找到则返回""而不是null,这样少了一个非空判断
 
    <%
        String a = "abc";
        request.setAttribute("a", a);
    %>
    
    ${a}

/*输出abc*/
    <%
        List t1 = new ArrayList();
        t1.add(new StoreName("aa"));
        t1.add(new StoreName("bb"));
        t1.add(new StoreName("cc"));
        request.setAttribute("t1", t1);
    %>
    ${t1[2].name};
${pageContext.request.contextPaht} 获取web资源名称
在写超链接时不能写死了。
<a href=${pageContext.request.contextPath}/index.jsp></a>
JSTL是sun公司开发的一套标签库,使用JSTL可以在页面中实现一些简单的逻辑,从而替换页面中的脚本代码。
在页面中使用JSTL标签需完成以下2个步骤:
1。导入jstl.jar 和standerd.jar这两个JSTL的jar文件。
2。在JSP页面中使用<%@taglib url="" prefix="" %>元素导入标签库。
 
<%
        Map map = new HashMap();
        map.put("a1", "aa");
        map.put("a2", "aa");
        map.put("a3", "aa");
        map.put("a4", "aa");
        request.setAttribute("map", map);
    %>
    <c:forEach var="person" items="${map}">
        ${person.key}:${person.value}
    </c:forEach>
 
原文地址:https://www.cnblogs.com/sjyzz/p/6668300.html