zoukankan      html  css  js  c++  java
  • JSTL

     

    JSTL: Java server pages standarded tag library java服务页标准标签库,

    功能 :实现 条件判断,迭代,XML解析,SQL连接与处理。

    使用:一般和 EL输出表达式连用,JSTL逻辑,EL输出

      1. 在WEB-INF下的lib下导入 javax.servlet.jsp.jstl.jar  和 jstl-impl.jar 两个 jar 包 变添加成本项目资源

        

      2. 2. 在jsp页面头部引入资源

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>

      

      3. 1  choose--when--otherwise  类似于java的switch

     1     <c:choose>
     2         <c:when test="${pageScope.name == '张三' && pageScope.password == '123'}">
     3                 登录成功
     4         </c:when>
     5         <c:otherwise>
     6             <%
     7                 request.setAttribute("msg","用户名或密码错误");
     8                 request.getRequestDispatcher("/index.jsp").forward(request,response);
     9             %>
    10         </c:otherwise>
    11     </c:choose>

      

      3.2  if-- 在JSTL中只有if没有else ,若需要实现,那就写两个if

    1 <c:if test="条件表达式">
    2         yes
    3 </c:if>

      3.3.1 forEach   -- 普通for-- 99乘法表

    1 <c:forEach var="i" begin="1" end="9">
    2     <c:forEach var="j" begin="1" end="${i}">
    3         ${j} * ${i} = ${i*j} &nbsp;&nbsp;
    4     </c:forEach>
    5     <br>
    6 </c:forEach>

      

      3.3.2  forEach -- 遍历List集合  -- 相当于 高级for

     1 <%
     2     List<String> list = new ArrayList<>();
     3     list.add("千里冰封");
     4     list.add("万里雪飘");
     5     list.add("望长城内外");
     6     request.setAttribute("list",list);
     7 %>
     8 <c:forEach items="${requestScope.list}" var="li">
     9     ${li}
    10 </c:forEach>
    11 
    12 echo : 千里冰封 万里雪飘 望长城内外

      3.3.3 forEach -- 遍历Map集合

     1 <%
     2     Map<Integer, String> map = new HashMap<>();
     3     map.put(1, "赵钱孙李");
     4     map.put(2, "周吴郑王");
     5     map.put(3, "冯陈褚卫");
     6     map.put(4, "蒋沈韩杨");
     7     pageContext.setAttribute("map",map);
     8 %>
     9 <c:forEach items="${map}" var="m">
    10     <c:out value="${m.key}"></c:out>
    11     <c:out value="${m.value}"></c:out>
    12 </c:forEach>
    13 
    14 echo:  1 赵钱孙李 2 周吴郑王 3 冯陈褚卫 4 蒋沈韩杨

      

      3.3.4 forEach -- 遍历数组

    1 <%
    2     String[] strs = {"","","","","","","","?"};
    3     session.setAttribute("strs",strs);
    4 %>
    5 <c:forEach items="${sessionScope.strs}" var="s" begin="2" end="6">  // 注意 : [begin,end] 下标从0开始, 两个都包括在内
    6     ${s}
    7 </c:forEach>
    8 
    9 echo : 我 的 眼 你 知
  • 相关阅读:
    安装Spring Tool Suite(STS)
    ES6,新增数据结构Set的用法
    在vue-cli生成的项目中使用karma+chrome进行单元测试
    利用babel-cli搭建支持ES6的node环境
    利用es-checker检测当前node对ES6的支持情况
    javascript精度问题与调整
    sessionStorage 、localStorage、cookie
    在vue-cli搭建的项目中在后台mock接口中支持req.body和req.cookies
    wireshark抓取本地数据包
    【linux基础】How to write CMakeLists.txt file?
  • 原文地址:https://www.cnblogs.com/iscurry/p/11777728.html
Copyright © 2011-2022 走看看