EL表达式基础语法总结

一、概念

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

二、作用

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

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

${scope.name}获取具体某个作用域中的数据
${name}获取作用域中的数据,逐级查找(pageContext、request、session、application)
例题
在这里插入图片描述

如果

request.setAttribute("key666")
session.setAttribut("key666")
application.setAttribute("key666")

全部都命名为key666,用EL表达式$ {key666}获取会获取到查询到的第一个(顺序是先查request.setAttribute(“key666”)、session.setAttribute(“key666”)、application.setAttribute(“key666”))

四、为什么要用EL表达式,EL表达式和JSP脚本的区别

假设在页面中key8没有定义
用EL表达式输出这个不存在的key8

<h1>$ {requestScope.key8}<h1>

输出结果是“”,就是什么也没有
但是如果通过作用域对象获取

<h1><%=request.getAttribute("key8")%></h1>

在浏览器会输出一个null
如果是一个表格里面的数据
我们更希望是“ ”,而不是null
所以建议通过EL表达式获取数据

五、EL的应用

5.1 获取引用类型

使用EL获取作用域中的对象调用属性时,只能访问对象的get方法,必须遵守命名规范定义
在这里插入图片描述
输出结果为-----
在这里插入图片描述

5.2 获取数组、集合的元素

例题

<%@ page import="com.qf.project.entity.Admin" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    int[] array = {1,3,5,6};
    request.setAttribute("arr",array);

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

    Map<String,String> maps = new HashMap<>();
    maps.put("CN","中国");
    maps.put("RA","俄罗斯");
    maps.put("PN","巴基斯坦");
    request.setAttribute("country",maps);
%>
${arr}<br>
${arr[0]}<br/>
${arr[2]}<br/>
<hr>
${list[0]}<br/>
${list[1]}<br/>
${list[2]}<br/>
${list.get(1)}<br/>
<hr>
${country["CN"]}<br/>
${country["RA"]}<br/>
${country["PN"]}<br/>
${country.PN}<br>
</body>
</html>

EL表达式是不支持循环的,所以在访问集合的元素时,是一个一个访问
在这里插入图片描述

六、EL表达式运算符

操作符描述
.访问一个Bean属性或者一个映射条目
[]访问一个数组或者链表的元素
+
-
*
/ or div
% or mod取模
== or eq测试是否相等
!= or ne测试是否不等
< or lt测试是否小于
> or gt测试是否大于
<= or le测试是否小于等于
>= or ge测试是否大于等于
&& or and测试逻辑与
|| or or测试逻辑非
! or not测试取反
empty测试是否空值
例题
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setAttribute("nums", 420);
%>
<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>
<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 !=200}</h1>
<h1>${nums > 1000 || nums ==2000}</h1>
<h1>${!(nums>1234)}</h1>
<h1>empty运算符</h1>
<h1>${empty nums}</h1>
</body>
</html>

七、获取应用上下文

例题:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL内置对象</title>
</head>
<body>
    <%
        String path = request.getContextPath();
    %>
    <%=path%>
<hr/>
    <a href="<%=path%>">Click me</a>
    <a href="<%=request.getContextPath()+"/xx/xx/xxx.jsp"%>">Click me</a>
    <a href="${pageContext.request.contextPath}/xx/xx/xxx.jsp">Click me</a>
</body>
</html>

在浏览器中查看代码
在这里插入图片描述

八、获取Cookie的EL

例题
先定义一个cookie,


@WebServlet(name = "CookieServlet",value = "/cookie")
public class CookieServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("name1", "libai");
        Cookie cookie1 = new Cookie("name2", "xiaobai");
        response.addCookie(cookie);
        response.addCookie(cookie1);
    }

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

8.1不用EL要写的代码很多,如下

在jsp页面调用该cookie

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>获取Cookie的EL</title>
</head>
<body>
    <%
        Cookie[] cookies = request.getCookies();
        String username1 = "";
        String username2 = "";
        if(cookies!=null){
            for (Cookie c:cookies
                 ) {
                if(c.getName().equals("name1")){
                    username1 = c.getValue();
                }if(c.getName().equals("name2")){
                    username2 = c.getValue();
                }
            }
        }
    %>
<%=username1%><br/>
<%=username2%>
</body>
</html>

先访问cookie再访问object.jsp,运行结果如图
在这里插入图片描述

8.2用EL表达式

只需要两行代码

<h1>${cookie.name1.value}</h1>
<h1>${cookie.name2.value}</h1>

该EL表达式在input标签中可以简化代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>获取Cookie的EL</title>
</head>
<body>
   <%
       Cookie[] cookies = request.getCookies();
       String username1 = "";
       String username2 = "";
       if(cookies!=null){
           for (Cookie c:cookies
                ) {
               if(c.getName().equals("name1")){
                   username1 = c.getValue();
               }if(c.getName().equals("name2")){
                   username2 = c.getValue();
               }
           }
       }
   %>
<hr/>
<br/>
   <input type="text" name="name1" value="<%=username1%>">
</body>
</html>

可以替换成

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>获取Cookie的EL</title>
</head>
<body>
<input type="text" name="name1" value="${cookie.name1.value}">
</body>
</html>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

素心如月桠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值