EL & JSTL

EL表达式

标准语法格式:
${域对象别名.关键字}
${requestScope.key}

可用简化版:(定位顺序:pageContext-->request-->session-->application)
简化语法格式:
${关键字}
${key}

==================================================================
相关运算:
${key1 + key2}
${age ge 18 ? "yes" : "no") 等价 ${age > 18 ? "yes" : "no")

==================================================================    
可用内置对象:
1.param
作用:读取请求参数
等同:String value = request.getParameter("请求参数名") out.write(value)
语法格式:
${param.key}

2.paramValues
作用:读取多个请求参数(前端为多选按钮发送参数时)
等同:String array[] = request.getParameter("请求参数名") out.write(array[0])
语法格式:
${paramValues.array[0]}
EL表达式中获取多个参数无法进行遍历,只能一个一个展示,但JSTL实现了循环遍历。
    
3.initParam
作用:读取全局作用域中的共享数据(一般存有如driver等配置数据)    
等同:String value = application.getInitParameter("driver") out.write(value)
语法格式:     
${initParam.共享数据名}

JSTL标签工具类

前提:

  1. 导入 jstl 和 standard 的 jar 包
  2. 在需要使用 JSTL 标签的 jsp 中添加 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
1.set标签
作用:在jsp文件上设置域对象中共享数据
语法:<c:set scope="session/application/request/page" var="key" value="共享数据" />
    
2.if标签
作用:控制哪些内容可写入响应体    
示例:
<c:if test="${sessionScope.age ge 18}">
    欢迎光临
</c:if>

3.choose标签    
作用:多分支选择判断,控制哪些内容可写入响应体
示例:    
<c:choose>    
    <c:when test="EL表达式判断">内容1</c:when>
    <c:when test="EL表达式判断">内容2</c:when>        
    <c:otherwise>内容3</c:otherwise>        
</c:choose>    

4.forEach标签
第一种使用方式:直接循环遍历
var:声明循环变量名
begin:初始化循环变量
end:循环变量可接收的最大值
step:循环变量递增值或递减值
注意:step可省略,默认递增或递减一;数据均是存储到当前页作用域对象中
<select>
    <c:forEach var="i" begin="1" end="5" step="1">
        <option>第${pageScope.i}页</option>
    </c:forEach>
</select>    

第二种使用方式:遍历域对象中List集合对象
item:通过EL表达式获取域对象集合
var:声明循环变量
<c:forEach items="${key}" var="stu">
    <tr>
        <td>${stu.sid}</td>
        <td>${stu.sname}</td>        
    </tr>
</c:forEach>

第三种使用方式:遍历域对象中Map集合对象
注意:遍历map集合,每次从map中得到一个键值对
示例遍历map:
<c:forEach items="${mapKey}" var="key_value">
    <tr>
        <td>${key_value.key}</td>
        <td>${key_value.value.sid}</td>
        <td>${key_value.value.sname}</td>        
    </tr>
</c:forEach>
posted @ 2022-11-01 23:28  luis林  阅读(16)  评论(0编辑  收藏  举报