• EL&JSTL:EL表达式总结



    目录:

    (1)EL介绍 

    (2)EL表达式的简单使用 

    (3)EL表达式输出高级 对象属性

    (4)EL表达式简化版

    (5)EL表达式支持运算

    (6)EL表达式其他工具对象

    (7)相对路径 绝对路径

    (8)EL表达式的缺陷


    在未来的实际项目开发中,jsp servlet都是以相结合的方式来开发,servlet主做后端业务逻辑处理,jsp主做前端数据显示。

     jsp数据显示的过程中,我们难免会遇到以java脚本的拼接的方式来结合前后端代码。如果java脚本拼接过多,这就涉及到了代码的可读性低与可维护性低的问题。

    使用el表达式和jstl标签库可以有效的简化jsp的开发,目的就是为了减少jsp开发中的代码量,避免脚本拼接问题,所以eljstl是我们开发jsp必用的操作。

    (1)EL介绍 

     index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 15:51
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--制造测试数据--%>
    10. <%
    11. request.setAttribute("key","abc");
    12. %>
    13. <%--Java命令读取request数据并写入到响应体中--%>
    14. <%
    15. String value= (String)request.getAttribute("key");
    16. out.write(value);
    17. %>

    18. Java命令写入的结果:<%=value%>
    19. EL表达式写入的结果:${requestScope.key}

    结果:

    可以看出使用EL表达式的代码得到了简化,提高了开发效率 

    (2)EL表达式的简单使用 

     index_2.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 16:53
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Titletitle>
    16. <base href="<%=basePath%>">
    17. head>
    18. <body>
    19. <%--制造测试数据--%>
    20. <%
    21. application.setAttribute("sid",10);
    22. session.setAttribute("sname","mike");
    23. request.setAttribute("sex","man");
    24. pageContext.setAttribute("home","BJ");
    25. %>
    26. 学员编号:${applicationScope.sid}<br>
    27. 学员姓名:${sessionScope.sname}<br>
    28. 学员性别:${requestScope.sex}<br>
    29. 学员籍贯:${pageScope.home}
    30. body>
    31. html>

     (3)EL表达式输出高级 对象属性

     创建Student类:

    1. package com.bjpowernode.model;
    2. public class Student {
    3. private Integer sid;
    4. private String sname;
    5. public Student(Integer sid, String sname) {
    6. this.sid = sid;
    7. this.sname = sname;
    8. }
    9. public Integer getSid() {
    10. return sid;
    11. }
    12. public void setSid(Integer sid) {
    13. this.sid = sid;
    14. }
    15. public String getSname() {
    16. return sname;
    17. }
    18. public void setSname(String sname) {
    19. this.sname = sname;
    20. }
    21. }

    index_3.jsp:

    1. <%@ page import="com.bjpowernode.model.Student" %><%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 17:10
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Titletitle>
    16. <base href="<%=basePath%>">
    17. head>
    18. <body>
    19. <%
    20. Student stu=new Student(20,"allen");
    21. session.setAttribute("stuKey",stu);
    22. %>
    23. 学员编号:${sessionScope.stuKey.sid}<br>
    24. 学员姓名:${sessionScope.stuKey.sname}<br>
    25. body>
    26. html>

    (4)EL表达式简化版

    index_4.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 17:23
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. session.setAttribute("key","小美姑娘");
    11. pageContext.setAttribute("key","猪八戒");
    12. %>
    13. session心仪的女孩名字:${key}

     (5)EL表达式支持运算

    数学运算: 

    index_5.jsp:EL表达式会自动的做类型转换

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 17:23
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. pageContext.setAttribute("key1","100");
    11. pageContext.setAttribute("key2","200");
    12. %>
    13. <%--Java--%>
    14. <%
    15. String num1=(String)pageContext.getAttribute("key1");
    16. String num2=(String)pageContext.getAttribute("key2");
    17. int sum=Integer.valueOf(num1)+Integer.valueOf(num2);
    18. %>
    19. Java命令的运行结果:<%=sum%>
    20. 使用EL表达式简化上面的方式:${key1+key2}

     

     关系运算:

    在EL表达式里面不支持if else处理,没有控制语句这个选项,它可以通过三元运算符来处理

    index_5,jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 17:53
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--制造测试数据--%>
    10. <%
    11. request.setAttribute("age",23);
    12. %>
    13. <%--Java命令处理--%>
    14. <%
    15. Integer age=(Integer)request.getAttribute("age");
    16. if (age>18){
    17. out.write("欢迎光临");
    18. }else{
    19. out.write("谢绝入内");
    20. }
    21. %>

    22. EL表达式简化处理:
    23. ${age ge 18?"欢迎光临":"谢绝入内"}

     (6)EL表达式其他工具对象

     

    index_7.jsp:发送请求,地址带的参数,用param接收:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 18:07
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--
    10. 发送请求时带的参数
    11. http://localhost:8080/index_7.jsp?uname=smith&password=123
    12. --%>
    13. 登录名:${param.uname}
    14. 密码:${param.password}

     

    index_8.jsp:接收一组信息

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 18:15
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%--
    10. http://localhost:8080/index_8.jsp?empNo=10&empNo=20
    11. --%>
    12. 第一个职员编号:${paramValues.empNo[0]}
    13. 第二个职员编号:${paramValues.empNo[1]}

     

      

    全局作用域当中存在两种共享数据,一种是由Sevlet写入的共享数据。,一种是由tomcat写入的共享数据 

    在web.xml:中声明一个由tomcat写入的共享数据

    1. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    4. version="4.0">
    5. <context-param>
    6. <param-name>driverparam-name>
    7. <param-value>com.mysql.jdbc.Driverparam-value>
    8. context-param>
    9. web-app>

    index_9.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 18:27
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. 来自web.xml中的共享数据:${initParam.driver}

    pageContext对象: 

     

     

     

    (7)相对路径 绝对路径

     

    这里设置了/myWeb是网站根目录的别名,如果不设置 下方Application Context中 是:/ 

    在代码中访问的网站的根目录web:如果设置了网站的别名,根目录就是/myWeb,没有设置就是 : /

     

    one.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 18:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Titletitle>
    16. <base href="<%=basePath%>">
    17. head>
    18. <body>
    19. one.jsp页面
    20. body>
    21. html>

    index.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 18:40
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    12. %>
    13. <html>
    14. <head>
    15. <title>$Title$title>
    16. <base href="<%=basePath%>">
    17. head>
    18. <body>
    19. index.jsp页面
    20. body>
    21. html>

    two.jsp:

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: DELL
    4. Date: 2022/7/19
    5. Time: 18:37
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <%
    10. String path = request.getContextPath();
    11. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    12. %>
    13. <html>
    14. <head>
    15. <title>Titletitle>
    16. <base href="<%=basePath%>">
    17. head>
    18. <body>
    19. <center>
    20. <h1>相当路径定位h1>
    21. <a href="one.jsp">one.jspa><br>
    22. <a href="../index.jsp">index.jspa>
    23. <h1>绝对路径定位h1>
    24. <a href="/myWeb/jsp/one.jsp">one.jspa><br>
    25. <a href="/myWeb/index.jsp">index.jspa>
    26. center>
    27. body>
    28. html>

     (8)EL表达式的缺陷

  • 相关阅读:
    Python运维之 Flask + 宝塔运行小应用
    [单片机课程设计报告汇总] 单片机设计报告常用硬件元器件描述
    Shell系统学习之Shell条件测试,判断语句和运算符
    git常用命令总结
    【Java技巧】如何在HashMap中插入重复的key?
    陕西Biotin-PEG-NHS ester MW:1k,2k,3.4k,5k,10k,20k
    592. Fraction Addition and Subtraction
    如何选择安全可靠的跨网文件安全交换一体机?
    企业运维实践-Nginx使用geoip2模块并利用MaxMind的GeoIP2数据库实现处理不同国家或城市的访问最佳...
    sheng的学习笔记-AI-高斯混合模型(GMM)
  • 原文地址:https://blog.csdn.net/dengfengling999/article/details/125876153