JSTL--表达式操作
核心标签库:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
1.输入输出操作
c:out 可以对特殊字符进行自动转义
<% request.setAttribute("book", "<<Java in web>>"); %> <!-- output(in IE):book:<> --> book:${requestScope.book } <br/> <!-- output:book:<<Java in web>> --> book:<c:out value="${requestScope.book}"></c:out>
c:set 可以为域赋属性值,其中 value 属性支持 EL 表达式;还可以为域对象中的 JavaBean 的属性赋值,target、value都支持 EL 表达式。
c:set可用的属性有:var、scope、value、target、property。
<% Customer cust = new Customer(); cust.setId("-900"); request.setAttribute("cust", cust); %> <c:set var="newId" scope="request" value="2345"></c:set> ${requestScope.cust.id} newId: ${requestScope.newId } <c:set target="${requestScope.cust }" property="id" value="${requestScope.newId}"></c:set> ${requestScope.cust.id}
c:remove 移除指定域对象的指定属性值
<c:remove var="newId" scope="request"/>
2.流程控制
c:if 没有else,不能实现if...else if...else逻辑,但可以保存当前的判断条件的结果(true或false)。
<c:if test="${param.age > 18 }" var="isAdult" scope="request"> 成年了! </c:if> <br/> ${requestScope.isAdult }
c:choose 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 之后使用。
<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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了