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 : 我 的 眼 你 知
原文地址:https://www.cnblogs.com/iscurry/p/11777728.html