EL表达式的使用-12月23讲课内容

2020-12-23T19:47:00

EL 表达式

EL 的使用

基础使用

案例代码一:获取实体类中的属性值

<%@ page import="com.fc.bean.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式</title>
</head>
<body>
    <%
        // 声明一个对象
        Student student = new Student(1, "易烊千玺", 20, "男", "真帅");

        // 将对象作为值存入pageContext中
        pageContext.setAttribute("student", student);
    %>

    <table align="center" border="1px">
        <tr>
            <th>ID</th><th>姓名</th><th>年龄</th><th>性别</th><th>信息</th>
        </tr>
        <tr>
            <%--通过EL表达式获取对应的值--%>
            <td>${student.id}</td>
            <td>${student.name}</td>
            <td>${student.age}</td>
            <td>${student.gender}</td>
            <td>${student.info}</td>
        </tr>
    </table>
</body>
</html>

【注意】可以使用任意域对象存储数据,一般常用 pageContext

使用 List 和 Map

案例代码一:使用 List

<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="com.fc.bean.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式测试List</title>
</head>
<body>
    <%
        List<Student> list = new ArrayList<>();

        list.add(new Student(1, "易烊千玺", 20, "男", "送你一朵小红花"));
        list.add(new Student(2, "迪丽热巴", 22, "女", "迪丽热巴"));
        list.add(new Student(3, "古力娜扎", 18, "女", "古力娜扎"));

        pageContext.setAttribute("list", list);
    %>

    <table align="center" border="1px">
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
            <th>Gender</th>
            <th>Info</th>
        </tr>

        <tr>
            <td>${list[0].id}</td>
            <td>${list[0].name}</td>
            <td>${list[0].age}</td>
            <td>${list[0].gender}</td>
            <td>${list[0].info}</td>
        </tr>
        <tr>
            <td>${list[1].id}</td>
            <td>${list[1].name}</td>
            <td>${list[1].age}</td>
            <td>${list[1].gender}</td>
            <td>${list[1].info}</td>
        </tr>
        <tr>
            <td>${list.get(2).id}</td>
            <td>${list.get(2).name}</td>
            <td>${list.get(2).age}</td>
            <td>${list.get(2).gender}</td>
            <td>${list.get(2).info}</td>
        </tr>
    </table>
</body>
</html>

案例代码二:使用 Map

<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.fc.bean.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>EL表达式测试Map</title>
</head>
<body>
   <%
       Map<String, Object> map = new HashMap<>();

       map.put("student1", new Student(1, "易烊千玺", 20, "男", "送你一朵小红花"));
       map.put("student2", new Student(2, "迪丽热巴", 21, "女", "古力娜扎"));
       map.put("student3", new Student(3, "欧阳娜娜", 19, "女", "缝纫机乐队"));

       pageContext.setAttribute("map", map);
   %>

   <table align="center" border="1px">
       <caption>测试Map</caption>
       <tr><th>编号</th><th>姓名</th><th>年龄</th><th>性别</th><th>信息</th></tr>
       <tr>
           <td>${map.student1.id}</td>
           <td>${map.student1.name}</td>
           <td>${map.student1.age}</td>
           <td>${map.student1.gender}</td>
           <td>${map.student1.info}</td>
       </tr>
       <tr>
           <td>${map.student2.getId()}</td>
           <td>${map.student2.getName()}</td>
           <td>${map.student2.getAge()}</td>
           <td>${map.student2.getGender()}</td>
           <td>${map.student2.getInfo()}</td>
       </tr>
       <tr>
           <td>${map["student3"].id}</td>
           <td>${map["student3"].name}</td>
           <td>${map["student3"].age}</td>
           <td>${map["student3"].gender}</td>
           <td>${map["student3"].info}</td>
       </tr>
   </table>
</body>
</html>

使用 EL 表达式中的 empty 关键字

empty 和 not empty 可用于判断是否为null,同时可以判断集合形式数据中是否存在元素,用于条件解析

<%@ page import="com.fc.bean.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>测试EL中的empty</title>
</head>
<body>
    <%
        String str1 = null;
        String str2 = "";
        String str3 = new String();

        Student student = null;

        List<Student> list = new ArrayList<>();

        pageContext.setAttribute("str1", str1);
        pageContext.setAttribute("str2", str2);
        pageContext.setAttribute("str3", str3);
        pageContext.setAttribute("student", student);
        pageContext.setAttribute("list", list);
    %>

    ${empty str1}
    ${empty str2}
    ${empty str3}
    ${empty student}
    ${empty list}
</body>
</html>

从 Servlet 获取参数【重点】

案例代码一

@WebServlet("/getParams")
public class TransmitDataServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求编码集
        req.setCharacterEncoding("UTF8");

        // 设置属性键值对
        req.setAttribute("username", "易烊千玺");
        req.setAttribute("password", "四个字");

        // 转发
        req.getRequestDispatcher("jsp/getParam.jsp").forward(req, resp);
    }

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

前端页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>获取后端传递的参数</title>
</head>
<body>
    ${username}
    ${password}
</body>
</html>
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »