JSP 表达式语言2

Expression Language 获取域中存储的值

  1. el表达式只能从域对象中获取值

  2. 语法1:

    • ${域名称.键名}:从指定域中获取指定键的值

      pageScope		--> pageContext
      requestScope 	--> request
      sessionScope 	--> session
      applicationScope --> application(ServletContext)
      
    • 举例:

      <body>
          <%
              // 在域中存储数据
              request.setAttribute("name1", "LeeHua");
              session.setAttribute("name2", "Rainbow");
          %>
      
          <h3>获取域中的数据</h3>
          ${requestScope.name1}
          <br>
          ${sessionScope.name2} 
      </body>
      
    • 浏览器中访问:http://localhost:8080/JspStudy_war_exploded/ExpressionLanguageDemo01.jsp

      ![image-20200607000424239](/Users/liyihua/Library/Application Support/typora-user-images/image-20200607000424239.png)

  3. 语法2:

    • ${键名}:表示依次从最小的域中查找是否有该键对应的值,直到找到为止。

    • 举例:

      <body>
          <%
              // 在域中存储数据
              request.setAttribute("name", "request_LeeHua");
              session.setAttribute("name", "session_Rainbow");
          %>
      
          <h3>获取域中的数据</h3>
          ${name}
      </body>
      

      依次从page --> request --> session --> application 中查找,找到name,就返回其值,没找到就返回null

    • 浏览器中访问:http://localhost:8080/JspStudy_war_exploded/ExpressionLanguageDemo02.jsp

      ![image-20200607001050435](/Users/liyihua/Library/Application Support/typora-user-images/image-20200607001050435.png)


Expression Language 获取域中存储的对象值

获取对象、List集合、Map集合的值。

  1. 获取对象:${域名称.键名.属性名}

    1. 创建一个Users对象

      public class Users {
          private String name;
          private String password;
          private Date date;
      
          public Users() { }
      
          public Users(String name, String password, Date date) {
              this.name = name;
              this.password = password;
              this.date = date;
          }
      
          /** 逻辑视图 @return 对date进行格式化,如果date为null,返回"",如果不为null,返回自定义格式的日期 */
          public String getDateSimple() {
              return date == null ? "" : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
          }
      
          public String getName() { return name; }
      
          public void setName(String name) { this.name = name; }
      
          public String getPassword() { return password; }
      
          public void setPassword(String password) { this.password = password; }
      
          public Date getDate() { return date; }
      
          public void setDate(Date date) { this.date = date; }
      }
      
    2. ExpressionLanguageDemo03.jsp

      <body>
      
          <%
              Users me = new Users();
              me.setName("LeeHua");
              me.setPassword("love");
              me.setDate(new Date());
              request.setAttribute("me", me);
      
              Users she = new Users();
              she.setName("Rainbow");
              she.setPassword("1314");
              she.setDate(new Date());
              request.setAttribute("she", she);
          %>
      
          <h3>在request域中,获取对象中的值</h3>
          ${requestScope.me.name}<%=" "%>
          ${requestScope.me.password}
          ${requestScope.she.name}<%=" "%>
          ${requestScope.she.password}
      
          <h3>在request域中,获取对象中的值</h3>
          ${me.name}<%=" "%>
          ${me.password}
          ${she.name}<%=" "%>
          ${she.password}
      
          <h3>在request域中,获取对象中的值</h3>
          ${me.date}<br>
          ${she.dateSimple}
      
      </body>
      
    3. 浏览器中访问:http://localhost:8080/JspStudy_war_exploded/ExpressionLanguageDemo03.jsp

      20200607014154

  2. 获取List集合:${域名称.键名[索引]}

    1. 这里也是用上面的Users对象

    2. ExpressionLanguageDemo04.jsp

      <body>
          <%
              Users me = new Users();
              me.setName("LeeHua");
              me.setPassword("love");
              me.setDate(new Date());
      
              Users she = new Users();
              she.setName("Rainbow");
              she.setPassword("1314");
              she.setDate(new Date());
          %>
          <%
              List<Users> list = new ArrayList<>();
              list.add(me);
              list.add(she);
              request.setAttribute("list", list);
          %>
      
          <h3>在request域中,获取List中的值</h3>
          ${requestScope.list[0].name}<%=" "%>
          ${requestScope.list[0].password}<%=" "%>
          ${requestScope.list[1].name}<%=" "%>
          ${requestScope.list[1].password}
      
      </body>
      
    3. 浏览器中访问:http://localhost:8080/JspStudy_war_exploded/ExpressionLanguageDemo04.jsp

      20200607102249
  3. 获取Map集合:${域名称.键名.key名称}${域名称.键名["key名称"]}

    1. 这里也是使用上面的Users对象

    2. ExpressionLanguageDemo05.jsp

      <body>
          <%
              Users me = new Users();
              me.setName("LeeHua");
              me.setPassword("love");
              me.setDate(new Date());
      
              Users she = new Users();
              she.setName("Rainbow");
              she.setPassword("1314");
              she.setDate(new Date());
          %>
          <%
              Map<String, Users> map = new HashMap<>();
              map.put("Hua", me);
              map.put("Cai", she);
              request.setAttribute("map", map);
          %>
      
          <h3>在request域中,获取Map中的值</h3>
          ${requestScope.map.Hua.name}<%=" "%>
          ${requestScope.map.Hua.password}<%=" "%>
          ${requestScope.map.Cai.name}<%=" "%>
          ${requestScope.map.Cai.password}
      
          <h3>在request域中,获取Map中的值</h3>
          ${requestScope.map["Hua"].name}<%=" "%>
          ${requestScope.map["Hua"].password}<%=" "%>
          ${requestScope.map["Cai"].name}<%=" "%>
          ${requestScope.map["Cai"].password}
      </body>
      
    3. 浏览器中访问:http://localhost:8080/JspStudy_war_exploded/ExpressionLanguageDemo05.jsp

      20200607105416

empty 运算符

  1. 空运算符 empty ,用于判断字符串、集合、数组对象是否为null或者长度为0

  2. 格式:

    • 判断字符串、集合、数组对象是否为null或者长度为0

      ${empty XXX}
      
    • 判断字符串、集合、数组对象是否不为null或者长度不为0

      ${not empty XXX}
      
  3. 举例:

    1. ExpressionLanguageDemo06.jsp

      <body>
          <%
              String me = "LeeHua";
              String she = "Rainbow";
              List<String> list = new ArrayList<>();
              list.add("love");
              list.add("1314");
              request.setAttribute("name1", me);
              request.setAttribute("name2", she);
              request.setAttribute("list", list);
          %>
      
          <h3>为空判断!!!</h3>
          ${empty name1}<br/>
          ${not empty name2}<br/>
          ${empty list}<br/>
          ${not empty list}
      </body>
      
    2. 浏览器中访问:http://localhost:8080/JspStudy_war_exploded/ExpressionLanguageDemo06.jsp

      20200607114554

隐式对象

参考:隐含对象

pageContext对象

pageContext对象是JSP中pageContext对象的引用。通过pageContext对象,您可以访问request对象。比如,访问request对象传入的查询字符串,就像这样:

${pageContext.request.queryString}
  • 动态获取虚拟目录

    <body>
        <h3>动态获取虚拟目录:</h3>
        ${pageContext.request.contextPath}
    </body>
    

Scope对象

  • pageScope,requestScope,sessionScope,applicationScope变量用来访问存储在各个作用域层次的变量。
  • 举例来说,如果您需要显式访问在applicationScope层的box变量,可以这样来访问:applicationScope.box。

param和paramValues对象

  • param和paramValues对象用来访问参数值,通过使用request.getParameter方法和request.getParameterValues方法。

  • 举例来说,访问一个名为order的参数,可以这样使用表达式:${param.order},或者${param["order"]}

  • 接下来的例子表明了如何访问request中的username参数:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <%
            String title = "Accessing Request Param";
            request.setAttribute("title", title);
        %>
        <title><% out.print(title); %></title>
    </head>
    <body>
        <center><h1><% out.print(title); %></h1></center>
        <div align="center">
            <p>${param["username"]}</p>
            <p>${paramValues}</p>
        </div>
    </body>
    </html>
    
  • param返回一个参数,paramValues返回一个Map<String, String> 集合。

header和headerValues对象

  • header和headerValues对象用来访问信息头,通过使用 request.getHeader方法和request.getHeaders方法。

  • 举例来说,要访问一个名为user-agent的信息头,可以这样使用表达式:${header.user-agent},或者${header["user-agent"]}

  • 接下来的例子表明了如何访问user-agent信息头:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <% String title = "User Agent Example"; %>
        <title><% out.print(title); %></title>
    </head>
    <body>
        <center><h1></h1></center>
        <div align="center">
            <p>${header["user-agant"]}</p>
            <p>${headerValues}</p>
        </div>
    </body>
    </html>
    
  • header对象返回单一值,而headerValues则返回一个字符串数组。


参考文献

  1. JSP 表达式语言| 菜鸟教程
posted @ 2021-03-03 22:32  LeeHua  阅读(122)  评论(0编辑  收藏  举报