• JSP EL表达式


    EL(Expression Language) 是为了使JSP写起来更加简单。表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法,让Jsp的代码更加简化。

    一个简单的表达式语法如下:

    ${expr}
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8" %>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10             <!--     out.print(out==pageContext.getOut()); -->
    11               <%
    12                 pageContext.setAttribute("msg","123");
    13                %>
    14             <!-- EL -->
    15                ${msg}
    16         
    17             <!-- JSP -->
    18               <%
    19                 String msg=(String)pageContext.getAttribute("msg");
    20                     if(msg!=null){
    21                         out.print(msg);
    22                     }
    23                %>
    24 </body>
    25 </html>

    []与.运算符
      . 访问一个Bean属性或者一个映射条目
      [] 访问一个数组或者链表的元素
    EL 提供“.“和“[ ]“两种运算符来存取数据。
    当要存取的属性名称中包含一些特殊字符,如 . 或 - 等并非字母或数字的符号,就一定要使用“[ ]“。例如:
    ${ user. My-Name}应当改为${user["My-Name"]}
    如果要动态取值时,就可以用“[ ]“来做,而“.“无法做到动态取值。例如:
    ${sessionScope.user[data]}中data 是一个变量
    3、变量
    EL存取变量数据的方法很简单,例如:${username}。它的意思是取出某一范围中名称为username的变量。因为我们并没有指定哪一个范围的username,所以它会依序从Page、Request、Session、Application范围查找。
    假如途中找到username,就直接回传,不再继续找下去,但是假如全部的范围都没有找到时,就回传""。

    Scope对象

    pageScope,requestScope,sessionScope,applicationScope变量用来访问存储在各个作用域层次的变量。

    举例来说,如果您需要显式访问在applicationScope层的box变量,可以这样来访问:applicationScope.box。

    属性范围在EL中的名称
    Page PageScope
    Request RequestScope
    Session SessionScope
    Application ApplicationScope
     
     
     
     
     
                                                                                                                                                                                                                       
     
    1  <!-- -------------------------------------------------- -->
    2          <!-- sessionScope -->
    3             ${sessionScope.msg1}
    4             <!--  EL sessionScope等同于JSP getAtrribute("msg") -->
    5         pageContext.setAttribute("msg1",456,pageContext.SESSION_SCOPE);

    EL对象、数组、List和Map的使用

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3     <%@ page import="com.uplooking.controller.Student" %>
     4     <%@ page import="java.util.* " %>
     5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     6 <html>
     7 <head>
     8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     9 <title>Insert title here</title>
    10 </head>
    11 <body>
    12     <%
    13     Student stu=new Student();
    14     stu.setName("老王");
    15     pageContext.setAttribute("stu",stu);
    16     Student stu1=new Student();
    17     stu1.setName("老李");
    18     pageContext.setAttribute("stu1",stu1);
    19     
    20     %>
    21     
    22     <!-- 对象 -->
    23     <!-- stu.name相当于stu.getName -->
    24     ${stu.name}
    25     
    26     <%
    27     int[] i={1,2,4,6};
    28     pageContext.setAttribute("arr",i);
    29     %>
    30     <!-- 数组 -->
    31     ${arr[1]}
    32     
    33     <%
    34     List<Student> stus=new ArrayList();
    35     stus.add(stu);
    36     stus.add(stu1); 
    37     pageContext.setAttribute("stus", stus);
    38     %>
    39     
    40     <!-- List集合 -->
    41     ${stus[1].name}
    42     
    43     <%
    44     Map<String,Student> map=new HashMap();
    45     map.put("10",stu);
    46     map.put("20",stu1);
    47     pageContext.setAttribute("map", map);
    48     %>
    49     
    50     <!-- List集合 -->
    51     ${map["20"].name}
    52     
    53 </body>
    54 </html>
     
     
     
  • 相关阅读:
    Binary Trees
    [POJ] String Matching
    Tree
    Maxmum subsequence sum problem
    poj 2104 划分树
    poj 2486 树形dp
    poj 1848 树形dp
    hdu 4578 线段树
    hdu 4585 set应用
    hdu 2412 树形DP
  • 原文地址:https://www.cnblogs.com/echola/p/9651321.html
Copyright © 2020-2023  润新知