首页 > 其他分享 >JSTL的常用标签choose和foreach

JSTL的常用标签choose和foreach

时间:2022-12-23 20:11:43浏览次数:31  
标签:count index 标签 JSTL foreach choose

JSTL的常用标签choose

c:choose标签:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>choose标签</title>
</head>
<body>

    <%--
        完成一个数字编号对应星期几案例
            1.域中存储数字
            2.使用choose标签去除数字    相当于switch声明
            3.使用when标签做数字判断     相当于case
            4.otherwise标签做其他情况的声明, 相当于default
    --%>

    <%
        request.setAttribute("number", 5);
    %>

    <c:choose>
        <c:when test="${number == 1}">星期一</c:when>
        <c:when test="${number == 2}">星期二</c:when>
        <c:when test="${number == 3}">星期三</c:when>
        <c:when test="${number == 4}">星期四</c:when>
        <c:when test="${number == 5}">星期五</c:when>
        <c:when test="${number == 6}">星期六</c:when>
        <c:when test="${number == 7}">星期日</c:when>

        <c:otherwise>数字输入有误</c:otherwise>
    </c:choose>

</body>
</html>

 

 

JSTL的常用标签foreach

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>foreach标签</title>
</head>
<body>

    <%--
          foreach:相当于java代码的for循环语句
            1.完成重复的操作
                for(int i = 0; i < 10; i ++){
                    执行操作
                }
                  属性:
                    begin:开始值
                    end:结束值
                    var:临时变量
                    step:步长 2
                    varStatus:循环状态对象
                        index:容器中元素的索引从0开始
                        count:循环次数从1开始
            2.遍历容器
                List<User> list;
                for(User user : list){

                }
                  属性:
                    items:容器对象
                    var:容器中元素的临时变量
                    varStatus:循环状态对象
                        index:容器中元素的索引从0开始
                        count:循环次数从1开始

    --%>

<%--    foreach使用步骤--%>
    <c:forEach begin="1" end="10" var="i" step="2" varStatus="s">
        ${i} <h3>${s.index}</h3> <h4>${s.count}</h4><br>
    </c:forEach>

    <hr>

<%--    遍历容器--%>
    <%
        List list = new ArrayList();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");

        request.setAttribute("list", list);
    %>

    <c:forEach items="${list}" var="str" varStatus="s">

            ${s.index} ${s.count} ${str}<br>

    </c:forEach>

</body>
</html>

 

标签:count,index,标签,JSTL,foreach,choose
From: https://www.cnblogs.com/qihaokuan/p/17001512.html

相关文章

  • JSTL概述以及JSTL中的if标签
    JSTL概述1.概念:JavaServer Pages TagLibraryJSP标准标签库是由Apache组织提供的开源的免费的jsp标签2.作用:用于简化和替换jsp页面上的java代码3.使用步骤:......
  • Bash Shell自定义助手函数git-submodule-foreach:遍历对每个子模块仓库执行自定义的函
    BashShell自定义助手函数git-submodule-foreach:遍历对每个子模块仓库执行自定义的函数或命令序列...概述:在一个大型项目下,我们通常通过GitSubmodule(子模块)机制引入了其......
  • IfcBooleanChoose
    IfcBooleanChoose功能定义如果为TRUE,则返回第一个选项,否则返回第二个选项。 注:根据ISO/IEC10303-42:1992的定义此函数根据布尔输入参数的值返回两个选项之一。这两......
  • C#迭代器foreach
    自己建立的类中,可以通过迭代器列出所有的成员  需用到  IEnumerable建立  Students 类,通过迭代列出所有的student的id和name classStudent{......
  • Map中forEach方法介绍
    HashMap继承于AbstractMap,实现了Map、Cloneable、java.io.Serializable接口。AbstractMap实现了Map接口。简单例子:  Map接口里面有一个forEach方法(java8)  ......
  • JSTL-常用标签-if、choose、foreach
    JSTL-常用标签-ifif:相当于java代码的if语句1.属性:test必须属性,接受boolean表达式如果表达式为true,则显示if标签体内容,如果为false,则不显示标签......
  • JSTL练习
    JSTL练习需求:在request域中有一个存有User对象的List集合。需要使用jstl+el将list集合数据展示到jsp页面的表格table中<%@pageimport="cn.itcast.domain.User"%><%@......
  • EL-empty运算符&隐式对象pageContext、JSTL概述
    EL-empty运算符空运算符:empty功能:用于判断字符串、集合、数组对象是否为null或者长度是否为0${emptylist}:判断字符串、集合、数组对象是否为null或者长度为0......
  • TypeError: forEach is not a function in JavaScript
     以下代码: constparent=this.el.parentElementconsole.log(parent.children)parent.children.forEach(child=>{console.log(child)}) 运行后出现以下错......
  • jsp页面通过JSTL表达式获取session中存储的对象的属性
    1.将user对象存入session  request.getSession().setAttribute("user",userSession);2.User类 publicclassUser{privateStringuserId;privateStringuser......