Easily learn EL expressions

 

1. The concept and main functions of EL expressions

(1) Concept

In one sentence: EL (Expression Language) is a mechanism to simplify the acquisition and traversal of jsp page data.

In other words, EL can simplify the writing of expressions in jsp, so what is a jsp expression? It is a sentence of the format "<%=xxx%>" in the jsp page.

What is the function of jsp expressions? It is to get and output data. The EL expression is a simplification of the jsp expression. What can you do with the EL expression is to easily access the data and print it out.

(2) Main functions

  • Access the data stored in pageContext, request, session, and application scope objects in sequence .

  • Get request parameter value;
  • Access the properties of the Bean object;
  • Access the data in the collection;
  • Output simple calculation results.

The prerequisite of EL expression: get data from several jsp built-in objects of pageContext , request , session  and  application.

pageContext: represents valid in the current page;

request: Represents valid in the current request;

session: Represents valid in the current session;

application: Represents valid in the current server.

Scope of action: pageContext <request <session <application

Since the core of EL expressions is to fetch data from these built-in objects , we first need to put the data in the built-in objects. How to put it in?

Set attributes: <% built-in object.setAttribute("key", "value"); %>

Next, explain the main functions of EL expressions one by one.

2. EL expressions access the data of built-in objects

(1) JSP original way

如:<%=request.getAttribute(“ varName”)%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现从内置对象中获取数据并打印</title>
</head>
<body>
<%
    pageContext.setAttribute("name1", "pageContext对象中的属性值:zhangfei");
    request.setAttribute("name2", "request对象中的属性值:guanyu");
    session.setAttribute("name3", "session对象中的属性值:liubei");
    application.setAttribute("name4", "session对象中的属性值:zhaoyun");
%>

<%-- 使用JSP中原始方式获取数据和打印--%>
<%= "name1的数值为:" + pageContext.getAttribute("name1") %><br/>   <%-- zhangfei --%>
<%= "name2的数值为:" + request.getAttribute("name2") %><br/>       <%-- guanyu --%>
<%= "name3的数值为:" + session.getAttribute("name3") %><br/>       <%-- liubei --%>
<%= "name4的数值为:" + application.getAttribute("name4") %><br/>   <%-- zhaoyun --%>

</body>
</html>

running result

 (2) EL expression mode

Such as: ${ varName}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现从内置对象中获取数据并打印</title>
</head>
<body>
<%
    pageContext.setAttribute("name1", "pageContext对象中的属性值:zhangfei");
    request.setAttribute("name2", "request对象中的属性值:guanyu");
    session.setAttribute("name3", "session对象中的属性值:liubei");
    application.setAttribute("name4", "session对象中的属性值:zhaoyun");
%>

<%-- 使用EL表达式实现获取数据和打印 --%>
<h1>name1的数值为:${name1}</h1><br/>
name2的数值为:${name2}<br/>
name3的数值为:${name3}<br/>
name4的数值为:${name4}<br/>

</body>
</html>

Execution effect

 How is it, isn't it simple? Just use the dollar sign ${property name} format.

(3) When the attribute names of the built-in objects are the same, the priority of EL value

When several built-in objects have the same attribute name, which value will the EL expression take? Look at the example:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现从内置对象中获取数据并打印</title>
</head>
<body>
<%
    pageContext.setAttribute("name", "pageContext对象中的属性值:zhangfei");
    request.setAttribute("name", "request对象中的属性值:guanyu");
    session.setAttribute("name", "session对象中的属性值:liubei");
    application.setAttribute("name", "session对象中的属性值:zhaoyun");
%>

<h1>name的数值为:${name}</h1><br/>
</body>
</html>

Execution effect

       As you can see, the value obtained here is the value of pageContext, so pageContext has the highest priority.

The smaller the scope of action, the greater the priority. So the priority order is: pageContext> request> session> application

3. The EL expression accesses the data of the request parameter

  Create the form page, param.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现一个简单页面负责向JSP页面传递参数</title>
</head>
<body>
<form action="el_param.jsp" method="post">
    姓名:<input type="text" name="name"/><br/>
    爱好:<input type="checkbox" name="hobby" value="唱歌"/>唱歌<br/>
         <input type="checkbox" name="hobby" value="跳舞"/>跳舞<br/>
         <input type="checkbox" name="hobby" value="学习"/>学习<br/>
    <input type="submit" value="提交"/><br/>
</form>
</body>
</html>

(1) JSP original way

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现请求参数数值的获取</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
%>
<%-- 使用JSP语法中的原始方式获取请求参数值 --%>
<%= "姓名是:" + request.getParameter("name") %><br/>
<%= "爱好是:" + Arrays.toString(request.getParameterValues("hobby")) %><br/>

</body>
</html>

(2) EL expression mode

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现请求参数数值的获取</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
%>
<%-- 使用EL表达式中的方式获取请求参数值 --%>
姓名是:${param.name}<br/>
爱好是:${paramValues.hobby[0]}<br/>

</body>
</html>

Here, param and paramValues ​​can be regarded as built-in objects of EL expressions.

4. The EL expression accesses the attribute value of the Bean object

Create Bean class: Person.java

public class Person {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

(1) JSP original way

<%@ page import="com.test.demo01.Person" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现Bean对象中属性的获取和打印</title>
</head>
<body>
<%-- 使用JSP语法规则中的原始方式实现对象的创建和设置以及输出 --%>
<%
    Person person = new Person();
    person.setName("zhangfei");
    person.setAge(30);
    pageContext.setAttribute("person", person);
%>

<%= "获取到的姓名为:" + person.getName() %><br/>
<%= "获取到的年龄为:" + person.getAge()  %><br/>

</body>
</html>

(2) EL expression mode

  a, way: $ {object name . attribute name } , for example: $ {} the user.name

 

<%@ page import="com.test.demo01.Person" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现Bean对象中属性的获取和打印</title>
</head>
<body>
<%-- 使用JSP语法规则中的原始方式实现对象的创建和设置以及输出 --%>
<%
    Person person = new Person();
    person.setName("zhangfei");
    person.setAge(30);
    pageContext.setAttribute("person", person);
%>

<%-- 使用EL表达式实现属性的获取和打印,方式1 --%>
获取到的姓名是:${person.name}<br/>
获取到的年龄是:${person.age}<br/>

</body>
</html>

b. Method 2: $ { object name [" attribute name "]} , for example: ${user["name"]}

c. The difference between the two methods

  • When the attribute name to access the table contains a number of special characters, such as: . Or , like reference numerals are not letters or numbers, to be sure to use the [] and
    Not . The way
  • The value can be dynamically obtained using the method of [] , the specific method is as follows:
<% request.setAttribute(“prop”,“age”); %><!-- 相当于表达式中写一个变量 --> ${ user[prop] }

5. EL expressions access the data in the collection

<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.List" %>
<%@ 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>
<%
    // 准备一个List集合并添加数据内容
    List<String> list = new LinkedList<>();
    list.add("two");
    list.add("one");
    list.add("three");
    // 将整个集合放入指定的内置对象中
    pageContext.setAttribute("list", list);

    // 准备一个Map集合并添加数据
    Map<String, Integer> map = new HashMap<>();
    map.put("one", 1);
    map.put("two", 2);
    map.put("th.ree", 3);
    // 将整个集合放入指定的内置对象中
    pageContext.setAttribute("map", map);
%>

<%-- 使用EL表达式实现集合中数据内容的获取 --%>
集合中下标为0的元素是:${list[0]}<br/>    <%-- two --%>
集合中下标为1的元素是:${list[1]}<br/>    <%-- one --%>
集合中下标为2的元素是:${list[2]}<br/>    <%-- three --%>
<hr/>
<%-- 使用EL表达式实现Map集合中数据内容的获取 不支持下标 --%>
整个Map集合中的元素有:${map}<br/>
获取带有特殊字符key对应的数值为:${map["th.ree"]}<br/>   <%-- 3 --%>
</body>
</html>

6. Built-in objects commonly used in EL expressions

7. The use of common operations in EL expressions

<%@ page import="java.util.List" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.Arrays" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>实现常用运算符的使用</title>
</head>
<body>
<%
    // 通过内置对象设置属性的方式来准备操作数
    request.setAttribute("ia", 5);
    request.setAttribute("ib", 2);
    request.setAttribute("b1", true);
    request.setAttribute("b2", false);
%>

<%-- 实现上述所有操作数的获取和打印 --%>
ia的数值为:${ia}<br/>      <%-- 5 --%>
ib的数值为:${ib}<br/>      <%-- 2 --%>
b1的数值为:${b1}<br/>      <%-- true --%>
b2的数值为:${b2}<br/>      <%-- false --%>
<hr/>

<%-- 实现算术运算符的使用 --%>
ia+ib的结果为:${ia+ib}<br/>    <%-- 7 --%>
ia-ib的结果为:${ia-ib}<br/>    <%-- 3 --%>
ia*ib的结果为:${ia*ib}<br/>    <%-- 10 --%>
ia/ib的结果为:${ia/ib}<br/>    <%-- 2.5 --%>
ia%ib的结果为:${ia%ib}<br/>    <%-- 1 --%>
<hr/>

<%-- 实现关系运算符的使用 --%>
ia大于ib的结果为:${ia > ib}<br/>  <%-- true --%>
ia大于等于ib的结果为:${ia >= ib}<br/>  <%-- true --%>
ia小于ib的结果为:${ia < ib}<br/>  <%-- false --%>
ia小于等于ib的结果为:${ia <= ib}<br/>  <%-- false --%>
ia等于ib的结果为:${ia == ib}<br/>  <%-- false --%>
ia不等于ib的结果为:${ia != ib}<br/>  <%-- true --%>
<hr/>

<%-- 实现逻辑运算符的使用 --%>
b1并且b2的结果为:${b1 && b2}<br/>  <%-- false --%>
b1或者b2的结果为:${b1 || b2}<br/>  <%-- true --%>
b1取反的结果为:${ !b1 }<br/>  <%-- false --%>
b2取反的结果为:${ !b2 }<br/>  <%-- true --%>
<hr/>

<%
    String str1 = null;
    String str2 = "";
    String str3 = "hello";

    List<Integer> list1 = new LinkedList<>();
    List<Integer> list2 = Arrays.asList(11, 22, 33, 44, 55);

    request.setAttribute("str1", str1);
    request.setAttribute("str2", str2);
    request.setAttribute("str3", str3);
    request.setAttribute("list1", list1);
    request.setAttribute("list2", list2);

%>
<%-- 实现条件运算符和验证运算符的使用 --%>
ia和ib之间的最大值为:${ia>ib? ia: ib}<br/>
判断是否为空的结果是:${empty str1}<br/>    <%-- true --%>
判断是否为空的结果是:${empty str2}<br/>    <%-- true --%>
判断是否为空的结果是:${empty str3}<br/>    <%-- false --%>
判断是否为空的结果是:${empty list1}<br/>    <%-- true --%>
判断是否为空的结果是:${empty list2}<br/>    <%-- false --%>

</body>
</html>

 

 

 

 

 

Guess you like

Origin blog.csdn.net/u012660464/article/details/109017417