JSP EL表达式使用

以下是一些JSP EL基本用法,写出来做个笔记,也可供大家参考交流。

一、EL表达式

EL在jsp中最基本的功能是取代诸如pageContext、request、session、application的对象的getAttribute()操作。

在jsp用运用EL可以简化代码,更加高效。

二、具体语法

Expression Language(EL)最先只能在jstl中用,后来才允许在jsp其他部分使用。

1、用于web层,也就是jsp

2、语法

a)       ${expr}

b)       不允许嵌套

c)       Bean的getter和 . 及 [] 操作

  1. . 操作 : 取得attribute的某个getter方法的返回值。
  2. [] 操作:作用和 . 操作一样,但允许不规范的命名(比如有空格和点)

3、expr可以是:

a)       文字量

b)       Attribute

c)       <jsp:bean>定义的bean:实际也是attribute

d)       参数和cookie

e)       可以含有运算符

4、Implicit(隐含) Objects

a)       pageContext

b)       pageScope

c)       requestScope

d)       sessionScope

e)       applicationScope

f)        param

g)       paramValues

h)       header

i)        headerValues

j)        cookie

k)       initParam

b~e用来限定attribute的范围,如果一个attribute没指定范围,那么有一个默认的由小到大的查找顺序。

5、运算

a)       算术运算

  1. + - * /(或div)  %(或mod)

b)       关系运算符

  1. ==(或 eq)、!=(或 ne)、<(或 lt)、>;(或 gt)、<=(或 le)和 >;=(或 ge)

c)       逻辑运算符

  1. &&(或 and)、||(或 or)和 !(或 not)

d)    空验证运算符  empty 

e)  条件运算 ?:

三、语法举例说明

下面通过具体的例子说明如何使用EL表达式,首先我们定义一个User类

public class User {
    private int id;
    private String n;
    private String password;

    public User() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return n;
    }

    public void setName(String name) {
        this.n = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    public String toString(){
        return "id:"+id+" || name:"+n+" || password:"+password;
    }
}
<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort()+ request.getContextPath() + "/";
    //EL无法直接使用java小脚本当中定义的变量
    pageContext.setAttribute("bp", basePath);
    User user = new User();
    user.setName("zhang3");
    user.setPassword("123");
    request.setAttribute("user4test", user);
%>

a)  ${expr}与算术运算的运用

     ${expr}它的意思是取出某一范围中名称为expr的变量。

 1 <body>
 2     <!-- 先在pageContext里找 user4test,没有再找request  ... -->
 3     ${user4test}
 4     
 5     <%
 6         //Object obj=pageContext.getAttribute("user4test");
 7         //String name=((User)obj).getName();
 8         //out.print(name);
 9     %>
10     <%
11     //或者在jsp表达式中用: ((User)(pageContext.getAttribute("user4test"))).getName()
12     %>
13     <br /> ${user4test.name}<br />
14     
15     
16     <!-- 也可以直接指定scope -->
17     ${requestScope.user4test.name}//这里我们指定用requestScope
18     <br />
19     <!-- 如果找不到呢? -->
20     这里有一个隐形的el:${notExist}<br/>
21     这里有一个隐形的el(by jsp expression):<%=request.getAttribute("notExist") %>
22     <br />
23     
24     
25     <!-- 下面3个是常量 -->
26     ${"我爱中国"}
27     <br /> ${12345}
28     <br /> ${true}
29     <br />
30     
31     
32     <!-- 以下是运算符 -->  
33     <%
34         pageContext.setAttribute("three", new Integer(3));
35     %>
36     ${3==4||3==three}
37     <br /> ${user4test.password=="123"}
38     <br /> ${123=="123"} ${123=="234"}
39     <br />
40 
41     3+4="${3+4}"
42     <br /> ${3==three?"正确":"错误"}
43     <br />
44 </body>

结果如下:

 1 id:0 || name:zhang3 || password:123 
 2 zhang3
 3 zhang3 
 4 这里有一个隐形的el:
 5 这里有一个隐形的el(by jsp expression):null 
 6 我爱中国
 7 12345 
 8 true 
 9 true 
10 true 
11 true false 
12 3+4="7" 
13 正确 

特别需要我们注意的是当属性拥有特殊字符时要使用[]:

1 <body>
2 无效的访问形式:${user.test.name}<br/>
3 
4 ${requestScope["user.test"].name}<br/>
5 
6 注意:不能写成{["user.test"].name},因为[]相当于点,不能写成“.xxxxxx”,第一个点前面没东西了。<br>
7 
8 </body>

结果如下:

1 无效的访问形式:
2 zhang3
3 注意:不能写成${["user.test"].name},因为[]相当于点,不能写成“.xxxxxx”,第一个点前面没东西了。

b)EL中empty的使用

1 <%
2     pageContext.setAttribute("str1","");
3     pageContext.setAttribute("str3",null);
4     pageContext.setAttribute("array",new String[]{});
5     pageContext.setAttribute("array1",new String[]{""});
6 %>
 1 <body>
 2 ${empty str1 }
 3 <br></br>
 4 ${empty str2 }
 5 <br></br>
 6 ${empty str3 }
 7 <br></br>
 8 ${empty array }
 9 <br></br>
10 ${empty array1 }
11 </body>

我们得到结果如下:

1 true 
2 
3  true 
4 
5  true 
6 
7  true 
8 
9  false 

c)EL中List的使用

<%
    List<String> list=new ArrayList<String>();
    list.add("litem1");
    String[] arr=new String[]{"String1"};
    Map<String,String> map=new HashMap<>();
    map.put("key1", "mitem1");
    pageContext.setAttribute("list", list);
    pageContext.setAttribute("map", map);
    pageContext.setAttribute("arr", arr);
    pageContext.setAttribute("k", "key1");
%>
<body>
<!-- 用[]运算,k被作为属性查找 -->
${map[k]}<br/>
<!-- 用.运算,k被作为字符串的key,因为map中没有键为“k”的项,所以此处没有输出 -->
${map.k}<br/>
${map["key1"]}<br/>
<!-- 一般用下面的形式 -->
${map.key1}<br/>
${list[0]}<br/>
${arr[0]}<br/>
</body>

得到结果如下:

1 mitem1
2  
3  mitem1
4  mitem1
5  litem1
6  String1
原文地址:https://www.cnblogs.com/xxxyang/p/11537979.html