• JSP之el表达式


    el表达式

     el: jsp的内置表达式语言,从jsp2.0开始,用来替代<%=..%>

        作用:
            1.获取域中数据 ★
            2.执行运算 ★
            3.获取常见的web对象
            4.调用java的方法
    格式: ${el表达式}
    获取域中数据:★
        注意:★
            若属性名中出现了"."|"+"|"-"等特殊符号,需要使用scope获取
                例如:
                    ${requestScope["arr.age"] }
        获取简单数据
            ${pageScope|requestScope|sessionScope|applicationScope.属性名}
            以后经常使用:
                ${属性名}:依次从pageContext,request,session,application查找指定属性,若查找到返回值,结束该次查找
                    若查找不到,返回""
        获取复杂数据
            获取数组中的数据: ${域中的名称[index]}
            获取list中的数据: ${域中的名称[index]}
            获取map中的数据: ${域中的名称.键名}

    案例1--el获取域中简单数据

      demo1.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el获取域中简单数据</title>
    </head>
    <body>
        <%
            request.setAttribute("rkey", "rvalue");
            session.setAttribute("rkey", "svalue");
            session.setAttribute("skey", "svalue");
            application.setAttribute("akey", "avalue");
        %>
        
        获取request中的数据:
        老方式:<%=request.getAttribute("rkey") %><br>
        el方式:${requestScope.rkey }<br>
        <hr>
        
        获取session中的数据:
        老方式:<%=session.getAttribute("skey") %><br>
        el方式:${sessionScope.skey }<br>
        <hr>
        
        获取application中的数据:
        老方式:<%=application.getAttribute("akey") %><br>
        el方式:${applicationScope.akey }<br>
        
        <hr>
        <hr>
        获取application数据失败:
        老方式失败:<%=application.getAttribute("aakey") %><br>
        el方式失败:${applicationScope.aakey }<br>
        
        <hr style="border-color:red">
        <!-- request和session中都有rkey,查找的时候是先从小域到大域中查起,查找 到了就立即结束,不会再往下面查-->
            便捷获取:
            ${rkey },${skey },${akey },${aakey }
        <hr style="border-color:red">
    </body>
    </html>

       启动tomcat,浏览器中输入以下url,回车,页面显示如下:

    案例2--el获取域中复杂数据

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@ page import="java.util.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el获取域中复杂数据</title>
    </head>
    <body>
        <%
            //往request域中放入数组
            request.setAttribute("arr", new String[]{"aa","bb","cc"});
        
            //往request域中放入list中
            List list = new ArrayList();
            list.add(111);
            list.add(222);
            list.add(333);
            request.setAttribute("list", list);
            
            //往request域中放入map集合中
            Map map = new HashMap();
            map.put("username","hjh");
            map.put("password","12345");
            request.setAttribute("map", map);
            request.setAttribute("user-age", "999");
        %>
        
        获取域中的数组:<br>
        老方式:<%=((String[])request.getAttribute("arr"))[1] %><br>
        el方式:${arr[1] }<br>
        <hr>
        
        获取域中的list:<br>
        老方式:<%=((List)request.getAttribute("list")).get(1) %><br>
        el方式:${list[1] }<br>
        list的长度:${list.size() }
        <hr>
        
        获取域中的map:<br>
        老方式:<%=((Map)request.getAttribute("map")).get("username") %><br>
        el方式:${map.username }<br>
        <hr>
        获取特殊名字的数据 :
        ${requestScope["user-age"] }
    </body>
    </html>

       启动tomcat,浏览器中输入以下url,回车,页面显示如下:  

        javabean导航
            javabean:
                java语言编写的一个可重用的组件,
                狭义上来说就是我们编写的一个普通的java类 例如:User Person 
                javabean规范:
                    1.必须是一个公共的具体的类  public class
                    2.提供私有的字段  private String id;//id称之为字段
                    3.提供公共访问字段的方法 get|set|is方法
                        public String getId(){..}
                        一旦有公共的方法之后,get|set|is之后的内容,将首字母小写,将这个东西称之为bean属性
                            id就是一个bean属性
                    4.提供一个无参的构造器
                    5.一般实现序列化接口  serializable
            
            ${域中javabean名称.bean属性}

    案例--javabean

      user.java

    package com.hjh.javabean;
    
    public class User {
        
        private String id;
        private String username;
        private String password;
        
        public User() {}
        public User(String id,String username,String password) {
            this.id = id;
            this.username = username;
            this.password = password;
        }
        
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return username;
        }
        public void setName(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
        @Override
        public String toString() {
            return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
        }    
    }

       javabean.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@page import="com.hjh.javabean.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>javabean</title>
    </head>
    <body>
        <%
            User user=new User();
            user.setId("001");
            user.setName("hjh");//name为javabean属性,而非username
            user.setPassword("12345");
            
            request.setAttribute("user",user);
        %>
        获取域中javabean的id值:<br>
        老方式:<%=((User)request.getAttribute("user")).getId() %><br>
        el方式:${user.id }<!-- 相当于getXXX() -->
        <hr>
        
        el获取name:<br>
        正确演示:${user.name }<!-- 输出:hjh -->
        
        
    </body>
    </html>

       启动tomcat,浏览器中输入以下url,回车,页面显示如下:

     

      加入以下代码,程序报错,应为user类中并没有提供getUsername()、setUsername()方法:

    错误演示:${user.username}

      错误信息如下:

    el执行运算:
        四则运算 关系(>..) 逻辑(&& ||)
        注意:
            +:只能进行加法运算,字符串形式数字可以进行加法运算.
                数字+数字字符串=数字
                数字字符串+数字字符串 报错
            empty:判断一个容器的长度是否为0(array set list map),还可以判断一个对象是否为空
                ${empty 域中的对象名称}
            三元运算符

      案例1--el加法运算:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el加法运算</title>
    </head>
    <body>
        <%
            request.setAttribute("a", 11);
            request.setAttribute("b", 22);
            request.setAttribute("c", "33");
            request.setAttribute("d", "kkk");
        %>
        数字+数字:${a+b }<br><!-- 33 -->
        数字+数字字符串:${a+c }<br><!-- 44 -->
        数字字符串+数字字符串:${c+c }<br><!-- 66 -->
        <!-- 以下加法报错 -->
        数字+字母字符串:${a+d}<br><!--报错-->
        数字字符串+字母字符串:${c+d }<br><!--报错-->
        字母字符串+字母字符串:${d+d }<br><!--报错-->
    </body>
    </html>

     案例2--el判断长度empty:

       empty1.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@page import="java.util.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>empty判断长度是否为0</title>
    </head>
    <body>
        <%
            List list1=null;
            request.setAttribute("list1", list1);
            
            List list2 = new ArrayList();
            list2.add(1234);
            request.setAttribute("list2", list2);
        %>
        
        request域中lis:1的长度是否为0:${empty list1 }<br><!-- true -->
        request域中list2的长度是否为0:${empty list2 }<!-- false -->
    </body>
    </html>

       启动tomcat,浏览器中输入以下url,回车,页面显示如下:

     

      案例3--el判断对象是否为empty:

       empty2.jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <%@page import="com.hjh.javabean.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>empty判断对象是否为0</title>
    </head>
    <body>
        <%
            User user1 = null;
            request.setAttribute("user1", user1);
        
            User user2 = new User();
            request.setAttribute("user2", user2);
        %>
        
        request域中javabean对象的长度是否为0:${empty user1 }<br><!-- true -->
        request域中javabean对象的长度是否为0:${empty user2 }<!-- false -->
    </body>
    </html>

      启动tomcat,浏览器中输入以下url,回车,页面显示如下:

       案例3--el三元运算符:

       demo3.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el获取域中简单数据</title>
    </head>
    <body>
        <%
            int i=3;
        %>
        <hr>
            ${3>4?"yes":"no" }<br>
            ${i==3?"yes":"no" }
        <hr>
    </body>
    </html>

       启动tomcat,浏览器中输入以下url,回车,页面显示如下:

     el的内置对象(了解):

        11个
            pageScope
            requestScope
            sessionScope
            applicationScope   
            param
            paramValues
            header
            haederValues
            initParam
            cookie★
            pageContext★
        注意:
            除了pagecontext其余对象获取的全是map集合
    了解:和参数相关的el内置对象
            param
            paramValues

     案例:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el内置对象param&paramValues</title>
    </head>
    <body>
        ${param.username }<br>
        ${param }<br>
        ${paramValues }<br>
        ${paramValues.hobby[0] }<br>
        
    </body>
    </html>

       http://localhost:8080/Jsp/el/param?username=hjh&password=12345&hobby=eat&hobby=sport

    了解:和请求头相关的el内置对象
            header
            haederValues

     案例:

       header.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el内置对象header&headerValues</title>
    </head>
    <body>
        header:${header }<br>
        <hr style="border-color:red">
        
        headerValues:${headerValues }<br>
        <hr style="border-color:red">
        
        referer:${header.referer }<br>
        <hr style="border-color:red">
        
        user-agent:${headerValues["user-agent"][0] }    
    </body>
    </html>

       启动tomcat,输入以下url,回车,页面显示如下:

     

    了解:和全局初始化参数相关的el内置对象
            initParam

       web.xml<?xml version="1.0" encoding="UTF-8"?>

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID"
                                                            version="3.1"> <context-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </context-param> </web-app>

       initParam.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el获取域中简单数据</title>
    </head>
    <body>
        ${initParam }
    </body>
    </html>

      启动tomcat,浏览器中输入以下url,回车,页面显示如下:

        cookie内置对象:
            ${cookie} 获取map{key=Cookie}
                例如:创建cookie
                    Cookie c=new Cookie("username","tom");
                通过${cookie}获取相当于    
                    {username=new Cookie("username","tom")}
                相当于map的key是cookie的键
                map的value是当前cookie
                
            若项获取名称username的cookie的value值(获取tom)
                ${cookie.username.value}--javabean导航
            注意:
                java中Cookie的api
                    getName():获取cookie的名称
                    getValue():获取cookie的value值
                我们称name和value是cookie的bean属性
        
            使用cookie内置对象:
                ${cookie.给cookie起名字.value}
                
            例如:
                获取jsession的值
                ${cookie.JSESSIONID.value}

       cookie.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el加法运算</title>
    </head>
    <body>
        ${cookie.JSESSIONID.value }
    </body>
    </html>

      浏览器显示如下:

     

    pageContext:获取不是map集合,相当于jsp的pageContext内置对象
                在jsp页面中获取项目名
                ${pageContext.request.contextPath}

      pageContext.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>el加法运算</title>
    </head>
    <body>
        <a href="${pageContext.request.contextPath }/el/text.jsp">点击跳转到text.jsp</a>
    </body>
    </html>

       启动comcat,访问以下url,显示一个超链接,如下图:

       点击超链接,页面跳转到text.jsp页面,url随之改变:

     

  • 相关阅读:
    团队作业
    第四次作业
    第三次作业
    从电梯问题,看c和c++之间的区别(有点懂了)错觉错觉
    团队作业2
    游戏方案
    电梯调度程序4
    电梯调度程序3
    电梯调度程序2
    电梯调度程序1
  • 原文地址:https://www.cnblogs.com/hejh/p/11063216.html
Copyright © 2020-2023  润新知