jstl

jstl 标签使用

1)*. c:out 主要用于对特殊字符进行转换. 真正进行输出时, 建议使用 c:out, 而不是使用 EL
2)*. c:set: 可以为域赋属性值。 而对域对象中的 JavaBean 的属性赋值用的并不多.
3). c:remove: 移除指定域对象的指定属性值(较少使用, 即便移除也是在 Servlet 中完成)

4)*. c:if: 在页面上对现实的内容进行过滤, 把结果存储到域对象的属性中. 但不灵活, 会被其他的自定义标签所取代.
5). c:choose, c:when, c:otherwise: 作用同上, 但麻烦, 不灵活.

6)*. c:forEach: 对集合进行遍历的. 常用!
7). c:forTokens: 处理字符串, 类似于 String 累的 split() 方法(知道即可)

8). c:import: 导入页面到当前页面的. (了解)
9). c:redirect: 当前页面进行重定向的. (使用较少)
10)*. c:url: 产生一个 URL 的, 可以进行 URL 重写, 变量值编码, 较为常用.


使用jstl时首先先要导入相关的jar包

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

c:forEach:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.atguigu.javaweb.Customer"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'foreach.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
    <h4>c:forEach: 可以对数组, Collection, Map 进行遍历, begin(对于集合 begin 从 0 开始算), end, step</h4>
    <c:forEach begin="1" end="10" step="3" var="i">
        ${ i} --
    </c:forEach>
    <hr>
    <% 
        List<Customer> custs = new ArrayList<Customer>();
        custs.add(new Customer(1, "AAA")); //index: 0 
        custs.add(new Customer(2, "BBB")); //1
        custs.add(new Customer(3, "CCC")); 
        custs.add(new Customer(4, "DDD")); //3
        custs.add(new Customer(5, "EEE"));
        custs.add(new Customer(6, "FFF")); //5

        request.setAttribute("custs", custs);
    %>
    <!-- 遍历 Collection, 遍历数组同 Collection -->
    <c:forEach items="${requestScope.custs }" var="cust"
        varStatus="status">
        ${status.index}, ${status.count}, ${status.first}, ${status.last}: ${cust.id }: ${cust.name }<br>
    </c:forEach>
    <hr>
    <!-- 遍历 Map -->
    <% 
        Map<String, Customer> custMap = new HashMap<String, Customer>();
        custMap.put("a", new Customer(1, "AAA")); //index: 0 
        custMap.put("b", new Customer(2, "BBB")); //index: 0 
        custMap.put("c", new Customer(3, "CCC")); //index: 0 
        custMap.put("d", new Customer(4, "DDD")); //index: 0 
        custMap.put("e", new Customer(5, "EEE")); //index: 0 
        custMap.put("f", new Customer(6, "FFF")); //index: 0 

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

    <br><br>
    <c:forEach items="${requestScope.custMap }" var="cust">
        ${cust.key } - ${cust.value.id } - ${cust.value.name }<br>
    </c:forEach>
    <hr>
    <% 
        String [] names = new String[]{"A", "B", "C"};
        request.setAttribute("names", names);
    %>
    <br><br>
    <c:forEach var="name" items="${names }">${name }-</c:forEach>

    <br><br>
    <c:forEach items="${pageContext.session.attributeNames }" var="attrName">
        ${attrName }-
    </c:forEach>
    <hr>
    <h4>c:forTokens: 处理字符串的, 类似于 String 的 split() 方法</h4>
    <c:set value="a,b,c.d.e.f;g;h;j" var="test" scope="request"></c:set>
    <c:forTokens items="${requestScope.test }" delims="." var="s">
        ${s }<br>
    </c:forTokens>
  </body>
</html>

这里写图片描述

<h4>
        c:choose, c:when, c:otherwise: 可以实现 if...else if...else if...else 的效果. 但较为麻烦
        其中: c:choose 以 c:when, c:otherwise 的父标签出现.
        c:when, c:otherwise 不能脱离 c:choose 单独使用.
        c:otherwise 必须在 c:when 之后使用。 
    </h4>
    <c:choose>      
        <c:when test="${param.age > 60 }">
            老年
        </c:when>
        <c:when test="${param.age > 35 }">
            中年
        </c:when>
        <c:when test="${param.age > 18 }">
            青年
        </c:when>
        <c:otherwise>
            未成年. 
        </c:otherwise>
    </c:choose>

    <c:set value="20" var="age" scope="request"></c:set>

    <h4>c:if: 不能实现 else 操作, 但可以把结果储存起来。 </h4>
    <c:if test="${requestScope.age > 18 }">成年了!</c:if>
    <br><br>

    <c:if test="${param.age > 18 }" var="isAdult" scope="request"></c:if>
    isAdult: <c:out value="${requestScope.isAdult }"></c:out>


    <h4>c:remove: 移除指定域对象的指定属性值</h4>
    <c:set value="1997-09-1" var="date" scope="session"></c:set>
    date: ${sessionScope.date }
    <br><br>

    <c:remove var="date" scope="session"/>
    date: --${sessionScope.date }--
    <br><br>

    <h4>c:set: 可以为域赋属性值, 其中 value 属性支持 EL 表达式; 还可以为域对象中的 JavaBean 的属性赋值, target, value都支持 EL 表达式</h4>

    <c:set var="name" value="ATGUIGU" scope="page"></c:set>
    <%-- 
        pageContext.setAttribute("name", "atguigu");
    --%>
    name: ${pageScope.name }   

    <br><br>
    <c:set var="subject" value="${param.subject }" scope="session"></c:set>
    subject: ${sessionScope.subject }

    <br><br>
    <% 
        Customer cust = new Customer();
        cust.setId(1001);
        request.setAttribute("cust", cust);
    %>
    ID: ${requestScope.cust.id }
    <c:set target="${requestScope.cust }" property="id" value="${param.id }"></c:set>
    <br>
    ID: ${requestScope.cust.id }

    <br><br>

    <h4>c:out: 可以对特殊字符进行转换. </h4>

    <% 
        request.setAttribute("book", "<<Java>>");
    %>
    book: ${requestScope.book }
    <br><br>
    book: <c:out value="${requestScope.book }" default="booktitle"></c:out>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值