关于EL表达式的那些事儿

1.EL表达式概述

  表达式语言(Expression Language),或称EL表达式,简称EL,是java中的一种特殊的通用变成语言,借鉴于JavaScrpit和APath。主要作用是在Java Web应用程序嵌入到网页(如 jsp中) 用以访问页面上的上下文以及不同作用域对象,缺德对象属性的值,或执行简单的运算或判断操作。EL在得到某个数据时,会自动进行数据类型的转换。

2.EL表达式的使用案例

<%@ page import="com.q.Student" %><%--
  Created by IntelliJ IDEA.
  User: 刘朝阳
  Date: 2020/5/23
  Time: 14:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>06-域对象</title>
</head>
<body>


<%
     pageContext.setAttribute("msg","PageContext Message");
     request.setAttribute("msg","Requset Message");
     session.setAttribute("msg","Session Messafe");
     application.setAttribute("msg","Application Message");


    Student stu = new Student(1, "小李", 16);

    request.setAttribute("stu",stu);

%>


<%--

数据检索顺序是从Page》Request》Session》Application
--%>


<h1>${msg}</h1>

<h2>${stu.id}</h2>
<h2>${stu.name}</h2>
<h2>${stu.age}</h2>
</body>



</html>

 

 

<%@ page import="java.util.ArrayList" %>
<%@ page import="com.q.Student" %><%--
  Created by IntelliJ IDEA.
  User: 刘朝阳
  Date: 2020/5/23
  Time: 15:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    ArrayList<Student> list = new ArrayList<>();

    list.add(new Student(1,"2",3));
    list.add(new Student(11,"21",31));
    list.add(new Student(21,"22",32));
    request.setAttribute("list",list);



%>


<table width="400px" align="center" border="1px">

    <tr>

        <th>ID</th>
        <th>name</th>
        <th>age</th>
    </tr>

    <tr>
        <td>${list[0].id}</td>
        <td>${list[0].name}</td>
        <td>${list[0].age}</td>


    </tr>

    <tr>
        <td>${list[1].id}</td>
        <td>${list[1].name}</td>
        <td>${list[1].age}</td>


    </tr>

    <tr>
        <td>${list[2].id}</td>
        <td>${list[2].name}</td>
        <td>${list[2].age}</td>


    </tr>



</table>
</body>
</html>

 

<%@ page import="java.util.ArrayList" %>
<%@ page import="com.q.Student" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: 刘朝阳
  Date: 2020/5/23
  Time: 15:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    ArrayList<Student> list = new ArrayList<>();

    list.add(new Student(1,"2",3));
    list.add(new Student(11,"21",31));
    list.add(new Student(21,"22",32));
    request.setAttribute("list",list);

   HashMap<String,String >map=new HashMap<>();

   map.put("一","2");
   map.put("二","12");
   map.put("三","22");

   application.setAttribute("map",map);



%>


<table width="400px" align="center" border="1px">

    <tr>

        <th>ID</th>
        <th>name</th>
        <th>age</th>
    </tr>

    <tr>
        <td>${list[0].id}</td>
        <td>${list[0].name}</td>
        <td>${list[0].age}</td>


    </tr>

    <tr>
        <td>${list[1].id}</td>
        <td>${list[1].name}</td>
        <td>${list[1].age}</td>


    </tr>

    <tr>
        <td>${list[2].id}</td>
        <td>${list[2].name}</td>
        <td>${list[2].age}</td>


    </tr>



</table>


<hr>

<h1>${map.一}</h1>
<h1>${map.二}</h1>
<h1>${map.三}</h1>

<hr>

<h1>${map['一']}</h1>
<h1>${map['二']}</h1>

<h1>${map['三 ']}</h1>


</body>
</html>

 

在Servlet中进行引用

package com.q;

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

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<Object> list = new ArrayList<>();

        list.add(new Student(1,"2",3));
        list.add(new Student(11,"21",31));
        list.add(new Student(21,"22",32));

        //list存储到request域对象中
        req.setAttribute("list",list);

        //转发到JSP页面中

        req.getRequestDispatcher("03-el.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

 

<%--
  Created by IntelliJ IDEA.
  User: 刘朝阳
  Date: 2020/5/23
  Time: 15:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<table width="400px" align="center" border="1px">

    <tr>

        <th>ID</th>
        <th>name</th>
        <th>age</th>
    </tr>

    <tr>
        <td>${list[0].id}</td>
        <td>${list[0].name}</td>
        <td>${list[0].age}</td>


    </tr>

    <tr>
        <td>${list[1].id}</td>
        <td>${list[1].name}</td>
        <td>${list[1].age}</td>


    </tr>

    <tr>
        <td>${list[2].id}</td>
        <td>${list[2].name}</td>
        <td>${list[2].age}</td>


    </tr>



</table>


</body>
</html>

 

<%@ page import="com.q.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 刘朝阳
  Date: 2020/5/23
  Time: 15:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%

    Student stu =null;

    request.setAttribute("stu",stu);

    request.setAttribute("list",new ArrayList<Student>());

    List<Student> list1 = null;
    request.setAttribute("list1",list1);

%>

<h3>${empty stu}</h3>

<%-- list.size()==0--%>
<h3>${empty list}</h3>
<h3>${empty list1}</h3>

<h3>${not empty list1}</h3>


</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值