小谈EL表达式

小谈EL表达式

在jsp中常看到${pageContext.repuest.contextPath},这其实就是一个EL表达式。

特点:

  • ${为始,}为终
  • 中间内容为值,或者说对象的属性
  • 可以使用域运算符,也就是“.”,也可以使用[]
  • 在值有特殊符号,如‘-’‘.’时必须使用[]运算符,且‘.’和’[]’可以同时使用在一个表达式中

例如
${pageContext.repuest.contextPath}等同于${pageContext.repuest[“contextPath”]}

${scope[”my-value”] },写成${scope.my-value}不对

使用[]可以实现动态取值,在[]中传入变量,eg:[变量]
${scope[var] },var为变量,var不同,取到的值不同,如果用’.’无法实现,只能写死,但硬编码需要避免。

范围

  • PageScope
  • RequestScope
  • SessionScope
  • ApplicationScope
    以上范围从大到小,但未指定范围是,由PageScope到ApplicationScope逐级寻找。

隐藏对象

对象含义
pageContext表示此JSP的PageContext
pageScope取得Page范围的属性名称所对应的值
requestScope取得Request范围的属性名称所对应的值
sessionScope取得Session范围的属性名称所对应的值
applicationScope取得Application范围的属性名称所对应的值
param同ServletRequest.getParameter(String name)回传String类型的值
paramValues同ServletRequest.getParameterValues(String name)回传String[]类型的值
headerServletRequest.getHeader(String name)
headerValuesServletRequest.getHeaders(String name)
cookieHttpServletRequest.getCookies()
initParamServletContext.getInitParameter(String name)

为防止硬编码,经常使用一下表达式

表达式含义
${pageContext.request.contextPath}服务的webapplication的名称
${pageContext.request.method}取得HTTP的方法(GET、POST)
${pageContext.request.protocol}取得请求的参数字符串
${pageContext.servletContext.serverInfo}取得主机端的服务信息
${pageContext.session.id}取得session的ID
${pageContext.request.protocol}取得使用的协议(HTTP/1.1、HTTP/1.0)
${pageContext.request.remoteAddr}取得用户的IP地址
${pageContext.request.remoteUser}取得用户名称

关系运算符:

  • == 或 eq
  • != 或 ne
  • < 或 lt
  • > 或 gt
  • <= 或 le
  • >= 或 ge
  • 记忆:equal/ not equal/ greater than/ less than/ less than or equal/ great than or equal/
  • ${3<=5}或${3le5}

逻辑运算符:

  • &&或 and
  • || 或 or
  • !< 或 not
  • ${! A }或${not A}

三元条件表达式:

  • ${ A ? B : C}

Empty 运算符

  • || 或 or
  • 当A为null时,表达式${empty A}返回true。  
  • 当A是空串时,表达式${empty A}返回true。  
  • 当A是数组、List或Map对象时,如果A中没有任何元素,表达式${empty A}返回true。  
  • 其他的情况返回false。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值