JSTL标签库

JSTL标签库

JSTL标签库全称是指 JSP Standard Tag Library
JSP标准标签库。是一个不断完善的开放的源代码的JSP标签库。
EL表达式主要是为了替换jsp中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个jsp页面变得更加简洁。

JSTL由五种不同功能的标签库组成。
在这里插入图片描述
在maven中导入jstl标签库的jar包

set标签

set标签可以往域中保存数据

域对象.setAttribute(key,value);

scope 属性设置保存到哪个域

page 表示pageContext域

request 表示reuqest域

session 表示session域

application域表示ServletContext域

保存之前:${requestScope.abc} <br>
<c:set scope="request" var="abc" value="abcValue"/> <br>
保存之后:${requestScope.abc}

if标签

if标签用来做if判断,test属性表示判断的条件(使用EL表达式输出)

<c:if test="${12==12}">
< h1>12等于12
</c:if>
在java中可以使用if-else但是在if标签里只能使用多个if标签来表示if-else

<c:choose> <c:when> <c:otherwise>标签用来做多路判断
跟java中的switch…case…default

<%
    request.setAttribute("height",178);
%>
<c:choose>
    <c:when test="${requestScope.height>190}">
        <h2>小巨人</h2>
    </c:when>
    <c:when test="${requestScope.height>180}>">
        <h2>很高</h2>
    </c:when>
    <c:when test="${requestScope.height>170}">
        <h2>还可以</h2>
    </c:when>
    <c:otherwise>
        <h3>不到170</h3>
    </c:otherwise>
</c:choose>

choose里面不能使用html标签

forEach标签

作用:遍历输出使用

  1. 遍历1到10,输出
<c:forEach begin="1" end="10" var="i">
    ${i}
</c:forEach>
  1. 遍历Object数组
 <%
request.setAttribute("arr",new String[]{"张三","李四","王五"});
%>
<c:forEach items="${requestScope.arr}" var="item">
    ${item}
</c:forEach>
  1. 遍历List集合——list中存放Person类,有属性:编号,用户名,密码,年龄,电话信息
<%
    ArrayList<Student> list = new ArrayList<>();
    Student stu1 = new Student("张三",18,"男");

    Student stu2 = new Student("王五", 11, "女");

    Student stu3 = new Student("赵四", 22, "男");

    list.add(stu1);
    list.add(stu2);
    list.add(stu3);

    request.setAttribute("list",list);

%>
<c:forEach  items="${requestScope.list}" var="stu">
    ${stu} <br>
</c:forEach>
  1. 遍历Map集合
<%
    HashMap<String, Object> map = new HashMap<>();
    map.put("key1","value1");
    map.put("key2","value2");
    map.put("key3","value3");
    map.put("key4","value4");
    request.setAttribute("map",map);

%>
<c:forEach items="${requestScope.map}" var="entry">
    ${entry} <br>
</c:forEach>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想成为大牛的渣渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值