JavaWeb之路05--EL表达式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_42267300/article/details/87949736

JSP之EL表达式

EL表达式

JSP中可以使用EL(Expression Language)表达式。EL表达式是用"${}“括起来的脚本,用来更方便地读取对象。EL表达式等同于JSP中”<%=%>“脚本,因此不能写在”<%%>"脚本中。


EL表达式获取域中数据

获取简单的数据

${pageScope|requestScope|sessionScope|applicationScope.属性名}

注意:

1. 能获取到则获取,获取不到返回""
2. 通过快捷获取的方式获取值${域中属性名}:
依次从pageScope|requestScope|sessionScope|applicationScope中查找指定的属性
若找到,立即返回,且结束该次查找
若找不到返回""
3. 若属性名中出现了"+“或”_“或”."等特殊符号时,用这种方法: ${xxxScope[“属性名”]}

获取复杂的数据

${arrayObject[index]}<%--获取数组中的数据--%>
${listObject[index]}<%--获取list中的数据--%>
${mapObject.key}<%--获取map中的数据--%>
${beanObject.attribute}<%--获取javabean中的数据--%>



执行运算

  1. EL表达式支持一些简单的算术运算,例如:加(+)、减(-)、乘(*)、除(/或者div)、取余(%或者mod)等。
  2. EL表达式支持一些简单的关系运算,例如:大于(>或者gt)、小于(<或者lt)、等于(==或者eq)、不等(!=或者ne)、大于等于(≥或者ge)、小于等于(≤或者le)、是否为空(empty)。
  3. EL表达式支持一些简单的逻辑运算,例如:与(&&或者and)、或(||或者or)、非(!或者not)以及括号。
  4. EL表达式也支持条件运算符( ? : )。

注意:字符比较时,EL表达式会调用int compare(char ss)方法进行比较。等于操作时,EL表达式会调用equals()方法进行比较。


隐藏对象

pageScope 四大域对象,会把作用域中的对象映射为一个map对象
requestScope
sessionScope
applicationScope
param 表示一个保存了所有请求参数的Map对象
paramValues 表示一个保存了所有请求参数的Map对象,它对于某个请求参数,返回的是一个string[]
header 表示一个保存了所有http请求头字段的Map对象
headerValuse 表示一个保存了所有http请求头字段的Map对象,它对于某个请求参数,返回的是一个string[]
initParam 表示一个保存了所有web应用初始化参数的map对象
cookie 表示一个保存了所有cookie的Map对象
pageContext 对应于JSP页面中的pageContext对象 ${pageContext.request.contextPath}:在jsp页面中动态的获取项目路


代码示例

el.jsp

<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="bean.Product" %>
<%--
  Created by IntelliJ IDEA.
  Author: XJM
  Date: 2019-02-25
  Time: 20:21
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>el</title>
</head>
<body>
    <%
        String str = "el";
        request.setAttribute("str", str);

        String strs[] = new String[]{"first", "second", "third"};
        request.setAttribute("strs", strs);

        List list = new ArrayList();
        list.add("list data1");
        list.add("list data2");
        list.add("list data3");
        request.setAttribute("list", list);


        Map<String, Integer> map = new HashMap();
        map.put("year", 1998);
        map.put("month", 12);
        map.put("day", 26);
        request.setAttribute("map", map);

        Product product = new Product();//Product类拥有4个属性,并设置了相应的get与set方法
        product.setId(1);
        product.setPdesc("mi 9 骁龙855");
        product.setPname("mi 9");
        product.setPrice(2999.0);
        request.setAttribute("product", product);

        int i = 7;
        request.setAttribute("i", i);

        char ch = '9';
        request.setAttribute("ch", ch);
    %>
    <h1>el表达式</h1>

    老方法:<%=request.getAttribute("str")%><br/>
    el表达式:${str}<hr/>

    老方法:<%=request.getAttribute("strs")%><br/>
    el表达式:${strs}<hr/>

    老方法:<%=((String[])request.getAttribute("strs"))[1]%><br/>
    el表达式:${strs[1]}<hr/>

    老方法:<%=request.getAttribute("list")%><br/>
    el表达式:${list}<hr/>

    老方法:<%=((List)request.getAttribute("list")).get(2)%><br/>
    el表达式:${list[2]}<br/><%--el表达式中,list有两种获取方法,可以根据索引获取--%>
            ${list.get(2)}<hr/><%--也可以通过get方法获取--%>

    老方法:<%=request.getAttribute("map")%><br/>
    el表达式:${map}<hr/>

    老方法:<%=((Map)request.getAttribute("map")).get("year")%>-<%=((Map)request.getAttribute("map")).get("month")%>-<%=((Map)request.getAttribute("map")).get("day")%><br/>
    el表达式:${map.year}-${map.month}-${map.day}<hr/><%--el表达式中map中的值可以省略get,直接通过key获取--%>

    老方法:<%=((Product)request.getAttribute("product")).getId()%>-<%=((Product)request.getAttribute("product")).getPname()%>-<%=((Product)request.getAttribute("product")).getPrice()%>-<%=((Product)request.getAttribute("product")).getPdesc()%><br/>
    el表达式:${product.id}-${product.pname}-${product.price}-${product.pdesc}<hr/><hr/>

    <h1>el运算</h1>
    <h2>算术运算</h2>
    i + ch = ${i + ch}<br/><el表达式中int值与数字类型的char值进行算术运算,会可以转化为数字进行运算>
    5 - 4 = ${5 - 4}<br/>
    29 / 3 = ${29 div 3}<br/>
    31 % 29 = ${31 % 29}<hr/>

    <h2>关系运算</h2>
    i < ch ${i < ch}<br/>
    map为空 ${empty map}<br/>
    100.0 = 100 ${100.0 eq 100}<hr/>

    <h2>逻辑运算</h2>
    7 != 5 || 4 == 4 ${7 != 5 || 4 == 4}<br/>
    1 != 2 && 7 ${1 != 2 && 7}<br/>
    !(1 == 3) ${not (1 eq 3)}<hr/>

    <h2>条件运算</h2>
    10 > 5 ? 10 - 5 : 10 < 5 ${10 gt 5 ? 10 - 5 : 10 lt 5}<hr/>
</body>
</html>

运行结果
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42267300/article/details/87949736