JSTL
一 JSTL_表达式操作
JSTL.jsp
<h4>c:out</h4>
<%
request.setAttribute("book","<<Java>>");
%>
book:${requestScope.book}
<br><br>
book:<c:out value="${requestScope.book2}" default="booktitle"></c:out>
<c:set var="name" value="UJS" 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>
//给JavaBean赋值的方式
<%
Customer cust=new Customer()
cust.setId(10001)
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}
<c:set var="data" value="2019-12-7" scope="session"></c:set>
date:${sessionScope.data}
<br><br>
<c:remove var="data" scope="session"/>
data:--%{sessionScope.data}--
<br><br>
二 JSTL_流程控制操作
<h4>c:if:没有else,但可以把判断结果存储起来,以备之后使用</h4>
<c:set var="age" value="20" scope="session"></c:set>
<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:choose,c:when,c:otherwise:可以实现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 test="${param.age>35">
中年
<c:when test="${param.age>18">
青年
<c:otherwise>
未成年
</c:otherwise>
</c:choose>
三 JSTL_迭代操作
<h4>c:forEach:可以对数组,Collection,Map进行遍历
begin(对于集合begin从0开始算)
</h4>
<c:forEach begin="1" end ="10" step="3" var="i">
${i}--
</c:forEach>
<br><br>
<%
List<Customer> custs=new ArrayList<Customer>();
custs.add(new Customer(1,"AAA"); //index=0
custs.add(new Customer(1,"AAA");
custs.add(new Customer(1,"AAA");
custs.add(new Customer(1,"AAA");
custs.add(new Customer(1,"AAA");
request.setAttribute("custs",custs);
%>
<c:forEach items="${requestScope.custs}" var="custs" begin="1" step="2" end="3">
${cust.id}:${cust.name}<br>
</c:forEach>
<c:forEach items="${requestScope.custs}" var="custs" varStatus="status">
${status.index},${status.count},${status.first},${status.last}
${cust.id}:${cust.name}<br>
</c:forEach>
遍历Map
<!--遍历Map-->
<%
Map<String,Customer> custMap=new HashMap<String,Customer>();
custMap.put("a",new Customer(1,"AAA"));
custMap.put("a",new Customer(1,"AAA"));
custMap.put("a",new Customer(1,"AAA"));
custMap.put("a",new Customer(1,"AAA"));
custMap.put("a",new Customer(1,"AAA"));
request.setAttribute("custMap",custMap);
%>
<c:forEach items="${requestScope.custMap}" var="cust">
${cust.key}-${cust.value.id}-${cust.value.name}<br>
</c:forEach>
遍历字符串数组
<%
String [] names=new String[]{"A","B","C"}
request.setAttribute("names",names);
%>
<br><br>
<c:forEach var="name" items="${names}"-</c:forEach>
<h4>c:forTokens:处理字符串的方法,类似于String的split方法</h4>
四 JSTL_URL操作