关于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>

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读