EL表达式和JSTL表达式

2019-04-29  本文已影响0人  金石_832e

EL表达式

EL 全名为 Expression Language,它原本是 JSTL 1.0 为方便存取数据所自定义的语言。当时 EL只能在JSTL标签中使用。到了JSP2.0 之后,EL已经正式纳入成为标准规范之一。
举例:java代码中有一个实体类user,他有两个属性username和password。

servlet代码

 request.getSession().setAttribute("user", userinfo);

现在要在页面上显示出user的属性

jsp脚本片段代码

<p>登录成功 : <%= request.getAttribute("username") %>,<%= request.getAttribute("password") %></p>

EL表达式

<p>登录成功 : ${ user.username },${ user.password }</p>

二者对比一下。


如果获取的是请求参数

<h1>TEST2 PAGE</h1>
/**jsp脚本片段**/
<%= request.getParameter("id") %>
<br>
/**el表达式**/
${ param.id }

如果不同域对象设置相同属性,它的默认值会先从 PageContext 范围找,假如找不到,再依序到 Request、Session、Application 范围。假如途中找到 username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时,就回传null。

4a0f35db60850542f386d6cf171df24c_492x178.png

JSTL表达式

JavaServer Pages Standard Tag Library (1.1 ) ,它的中文名称为 JSP 标准标签函数库。JSTL 所提供的标 签函数库主要分为五大类:
(1)核心标签库 (Core tag library)
(2)I18N 格式标签库 (I18N-capable formatting tag library)
(3)SQL 标签库 (SQL tag library)
(4)XML 标签库 (XML tag library)
(5)函数标签库 (Functions tag library) 表 。


使用方法

①先去maven下载两个jar包。
②导入jar包

image
③jsp中导入标签库(我导的是核心库)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

备注:两个最重要,最常用的JSTL表达式if和forEach,判断和遍历。


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
     年龄 :${ age }
    <!-- if(条件){} -->
    <!-- c:if <c:if test="条件"></c:if>-->
    <c:if test="${ age > 18}">
        成年
    </c:if>
    <c:if test="${ age <= 18}">
        未成年
    </c:if>
    <!-- 
        List<String> list;
        for(;;i+=2)
        for(String str :list){}
     -->
    <!-- c:forEach -->
    <!-- List<String> -->
    <c:forEach items="${ strList }" var="str" >
        ${ str }
    </c:forEach>
    <hr>
    <!-- List<Userinfo> -->
    <c:forEach items="${ userList }" var="user" >
        ${ user.username },${ user.password }
    </c:forEach>
    <hr>
    <!-- Map<String,String> -->
    <c:forEach items="${ map }" var="map">
        ${ map.key },${ map.value }
    </c:forEach>
    <hr>
    <!-- Map<String,Userinfo> -->
    <c:forEach items="${ userMap }" var="map">
        ${ map.key },${ map.value.username },${ map.value.password }
    </c:forEach>
    <hr>
    <!-- List<Map<String,Userinfo>> -->
    <c:forEach items="${ listMap }" var="map">
        <c:forEach items="${ map }" var="entry">
            ${ entry.key },${ entry.value.username },${ entry.value.password }
            <br>
            =====================
            <br>
        </c:forEach> 
    </c:forEach>
    <hr>
    <c:forEach items="${ listMap1 }" var="map">
        ${ map.java1 }
    </c:forEach>
    <!-- c:choose -->
    <c:choose>
        <c:when test="${ age <= 18 }">
            未成年
        </c:when>
        <c:when test="${ age > 70 }">
            老年
        </c:when>
        <c:otherwise>
            中年
        </c:otherwise>
    </c:choose>
    <a href="${pageContext.request.contextPath }/jstltest">a</a>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读