130 EL表达式(Expression Language)

1.概念

  • EL使JSP写起来更简单、简洁。
  • 主要用于获取作用域中的数据

2.作用

  • 用于替换 作用域对象.getAttribute(“name”);

3. EL的应用1(获取基本类型、字符串)

  • ${scope.name}获取具体某个作用域中的数据

  • ${name}获取作用域中的数据,逐级查找(pageContext、request、session、application),这种方式需要确定name 的值是唯一的

3.1 EL应用案例
<%
	//存储在request作用域 
	request.setAttribute("name","tom"); 
    request.setAttribute("age",18);
%>

${requestScope.name}  <%--获取request作用域中name对应的值,找到就返回,没找到返回" "--%>
${name} <%--从最小作用域逐级查找name对应的值,找到就返回,没找到返回" " --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式</title>
</head>
<body>
    <%
        request.setAttribute("key1","value1");
        session.setAttribute("key2","value2");
        application.setAttribute("key3","value3");

        request.setAttribute("key666","value6");
        session.setAttribute("key666","value7");
        application.setAttribute("key666","value8");
    %>
    <h1>通过作用域对象获取:</h1>
    <!--通过作用域对象获取数据 -->
    <h1><%=request.getAttribute("key8")%></h1>
    <h1><%=session.getAttribute("key2")%></h1>
    <h1><%=application.getAttribute("key3")%></h1>

    <hr/>
    <h1>通过EL表达式获取数据:</h1>
    <!--通过EL表达式获取数据 -->
    <h1>${requestScope.key666}</h1>
    <h1>${sessionScope.key666}</h1>
    <h1>${applicationScope.key666}</h1>

    <hr/>
    <h1>${key666}</h1>
    <h1>${key666}</h1>
    <h1>${key666}</h1>
</body>
</html>

3.2EL和JSP脚本的区别
  • <%=request.getAttribute() %> 没有找到返回 null

  • ${requestScope.name}没找到返回 " "(空,就是不显示任何东西)

4. EL的应用2(获取引用类型)

  • 使用EL获取作用域中的对象调用属性时,只能访问对象的get方法,必须遵守命名规范定义(实体类要有get方法)
<%
    Emp e = new Emp(); 
    e.setName("gavin"); 
	e.setAge(19);
    request.setAttribute("e" ,e);
%>

${requestScope.e.name} <%--调用getName()方法--%>
<%@ page import="com.wlw.entity.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL获取对象</title>
</head>
<body>
    <%
        User user = new User("gavin","123456");
        request.setAttribute("user",user);
    %>

    <%
        User u = (User)request.getAttribute("user");
        out.println(u.getUsername());
        out.println(u.getPassword());
    %>
    <hr/>
    ${requestScope.user}<br/>
    ${user}<br/>
    ${user.username}<br/><%--调用getUsername()--%>
    ${user.password}<br/>
</body>
</html>

5. EL的应用3(获取数组、集合的元素)

  • EL可以获取Array、List、Map中的元素,Set由于没下标,无法直接访问元素,后续可遍历
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>数组、集合的访问</title>
</head>
<body>
    <%
        int[] array = new int[]{1,2,3,4,5};
        request.setAttribute("array",array);

        List<String> nums = new ArrayList<String>();
        nums.add("A");
        nums.add("B");
        nums.add("C");
        request.setAttribute("nums",nums);

        Map<String,String> maps = new HashMap<String,String>() ;
        maps.put ("CN","中国");
        maps.put ("US", "美国");
        maps.put ("IT", "意大利");
        request.setAttribute("maps", maps) ;


    %>
<!--EL访问数组 -->

    ${array[1]}<br/>
    ${array[2]}<br/>
    ${array[3]}<br/>

    <hr/>
    ${nums[0]}<br/>
    ${nums[1]}<br/>
    ${nums.get(2)}

    <hr/>
    ${requestScope.maps["CN"]}<br/>
    ${maps.US}<br/>
    ${maps["IT"]}

</body>
</html>

6. EL的运算符

操作符描述
.访问一个Bean属性或者一个映射条目
[]访问一个数组或者链表的元素
+
-减或负
*
/ or div
% or mod取模
==or eq测试是否相等
!=or ne测试是否不等
< or lt测试是否小于
>or gt测试是否大于
<=or le测试是否小于等于
>=or ge测试是否大于等于
&&orand测试逻辑与
|| or or测试逻辑或
! or not测试取反
empty测试是否空值
6.1 EL表达式执行运算
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式运算符</title>
</head>
<body>
    <%
        request.setAttribute("nums",1234);
        request.setAttribute("ss","b");
    %>
    <h1>empty运算符</h1>
    <h1>${empty ss}</h1><!--测试ss 是否为空 , null和" " 都是空-->

    <hr/>
    <h1>算术运算符</h1>
    <h1>${nums + 5 } </h1>
    <h1>${nums - 5 } </h1>
    <h1>${nums * 5 } </h1>
    <h1>${nums div 5 } </h1>
    <h1>${nums mod 5 } </h1>

    <hr/>
    <h1>关系运算符</h1>
    <h1>${nums eq 1234}</h1>
    <h1>${nums ne 1234}</h1>
    <h1>${nums gt 1234}</h1>
    <h1>${nums lt 1234}</h1>
    <h1>${nums ge 1234}</h1>
    <h1>${nums le 1234}</h1>

    <h1>逻辑运算符</h1>
    <h1>${nums > 1000 and nums !=1200}</h1>
    <h1>${nums > 1000 or nums == 2000}</h1>
    <h1>${not(num > 1234)}</h1>
</body>
</html>
6.2 empty 关键字
<%
    String s1=" ";
    pageContext.setAttribute("s1",s1);
    String s2=null;
    pageContext.setAttribute("s2",s2);
    String s3="122222";
    pageContext.setAttribute("s3",s3);
    List listl =new ArrayList(); 
	pageContext.setAttribute( "list1", list1);
%>

<!-- empty关键只要内容是空true -->

${empty s1}<br>
${empty s2}<br>
${empty s3}<br>
${empty listl}<br>

7.隐式对象

  • EL表达式语言定义了 11个隐式对象
隐含对象描述
pageScopepage作用域
requestScoperequest作用域
sessionScopesession作用域
applicationScopeapplication 作用域
paramRequest对象的参数,字符串
paramValuesRequest对象的参数,字符串集合
headerHTTP信息头,字符串
headerValuesHTTP信息头,字符串集合
initParam上下文初始化参数
cookieCookie 值
pageContext当前页面的pageContext
7.1获得应用上下文
<%=request.getContextPath() %> 
${pageContext.request.contextPath}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL内置对象</title>
    <link href="${pageContext.request.contextPath}/css/xxx.css">
</head>
<body>

    <%
        String path = request.getContextPath();
    %>
    <%=path%>

    <br/>
    <a href="<%=request.getContextPath()%>/manager/safe/xxxController">Click me</a><br/>
    <a href="${pageContext.request.contextPath}/manager/safe/xxxController">Click target</a>
</body>
</html>

7.2获取Cookie对象
${cookie.username}//获取名为 username 的 cookie 对象 
${cookie.password} //获取名为 password 的cookie 对象 
${cookie.password.value}//获取password 的cookie 的value值
package com.wlw.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "CookieServlet",value = "/cookie")
public class CookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("username","tom");
        Cookie cookie1 = new Cookie("password","123456");

        response.addCookie(cookie);
        response.addCookie(cookie1);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>获取Cookie的EL</title>
</head>
<body>
    <!--之前的做法-->
    <%
        Cookie[] cookies = request.getCookies();
        String username = "";
        String password = "";
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")) {
                    username = cookie.getValue();
                }
                if(cookie.getName().equals("password")){
                    password = cookie.getValue();
                }
            }
        }
    %>
    <%=username%> <br/>
    <%=password%>

    <hr/>
    <!--EL-->
    <h1>${cookie.username.value}</h1>
    <h1>${cookie.password.value}</h1>

    <!--自动登录-->
    <input type="text" name="username" value="<%=username%>"><br/>
    <input type="text" name="password" value="<%=password%>"><br/>
    <input type="text" name="username" value="${cookie.username.value}"><br/>
    <input type="text" name="username" value="${cookie.password.value}"><br/>
</body>
 </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悬浮海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值