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>
复制代码

 

posted @   少司命  阅读(237)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示