EL表达式

The primary feature of JSP 2.0 is its support for an expression language(EL). An expression language makes it possible to easily access application data stored in javabeans components.

 

Active and Deactive EL:

<%@page isELIgnored=”false | true”%>

 

 

EL可以被用在哪里?

l         静态文本

${“static text……”}

 

l         In any standard or custom tag attribute that can accept an expression

<some:tag value=”abc ${a1} ${a2}” />

 

 

 .     []  访问对象的属性

${user.id} ${user[‘id’]} 效果是一样的,都是调用getId()方法。这里注意,访问的是属性property而非字段field,所以,user并不一定要有名为id的字段,但一定要有名为getId()的方法用以供调用。

如果user是一个javabean,那么用.[]就是去调用其property的访问方法。

如果user是一个List, 那么id就会被强制转成int,然后实行user.get(id),从而返回了List中的元素。若产生IndexOutOfBoundsException,将返回null,若是别的异常,将被抛出。

      如果user是一个Map,那么id被当作key,然后执行user.get(id),返回key所对应的值。若 user.containsKey(id)false,则返回null.

 

 

EL除了可以从不同的访问范围内拿我们设定的对象,还可以访问隐性对象 Implicit Object. 隐性对象是指,由web containerapplication加载的时候而设定的。JSP定义了一些列Implicit Object,如:

pageContext

   -servletContext

   -session

   -request

      -response

等等。我们可以使用EL来直接访问,${pageContext.request.localName}

${pageContext.session.creationTime}

 

 

 

 

 

 

Table 12-2 Example Expressions 

EL Expression

Result

${1 > (4/2)}

false

${4.0 >= 3}

true

${100.0 == 100}

true

${(10*10) ne 100}

false

${'a' < 'b'}

true

${'hip' gt 'hit'}

false

${4 > 3}

true

${1.2E4 + 1.4}

12001.4

${3 div 4}

0.75

${10 mod 4}

2

${empty param.Add}

True if the request parameter named Add is null or an empty string

${pageContext.request.contextPath}

The context path

${sessionScope.cart.numberOfItems}

The value of the numberOfItems property of the session-scoped attribute named cart

${param['mycom.productId']}

The value of the request parameter named mycom.productId

${header["host"]}

The host

${departments[deptName]}

The value of the entry named deptName in the departments map

${requestScope['javax.servlet.
forward.servlet_path']}

The value of the request-scoped attribute named javax.servlet.
forward.servlet_path

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值