javaWeb 学习日记(11)------ EL 表达式

EL变量

EL存取变量数据的方法很简单,例如:${username}。它的意思是取出某一范围中名称为username的变量。因为我们并没有指定哪一个范围的username,所以它的默认值会先从Page范围找,假如找不到,再依序到Request、Session、 Application 范围。假如途中找到username,就直接回传,找不到,再依序到Request、Session、 Application 范围。假如途中找到username,就直接回传null


EL 表达式中的 11个隐含变量

  1. 与范围有关的隐含对象
    applicationScope
    sessionScope
    requestScope
    pageScope
  2. 与输入有关的隐含变量
    param
    paramValues
  3. 其他隐含变量
    cookie
    header
    headerValues
    initParam
    pageContext



演示

  1. EL 的 . 或 [] 运算符
<%
  Customer customer = new Customer();
  customer.setName("jin");
  customer.setAge(18);
  
  session.setAttribute("customer", customer);
  
  //域中含有特殊字符
  session.setAttribute("com.athome", customer);
 %>
 age:${sessionScope.customer.age }
 <br>
 <!-- 域中含有特殊字符是,[] 会方便很多 -->
 name:${sessionScope["com.athome"].name }
 <br>
  1. EL 的隐含对象
    会按照 page, request ,session ,application 依次找属性中是否有 customer 属性,有则调用 customer.getAge() 方法
 <!-- 会按照 page, request ,session ,application 依次找属性中是否有 customer 属性,有则调用 customer.getAge() 方法 -->
 age:${customer.age}
 <br>
  1. EL 可以进行自动类型转换
<!-- 从 a.jap 中传过来的 score = 88 -->
 <!-- EL 表达式可以进行自动类型转换 -->
 Score1:${param.score + 11 }
 <br>
 Score2:<%= request.getParameter("score") + 11 %>
 <br>
  1. 隐含对象之范围相关的:pageScope, requestScope, sessionScope , applicationScope
<%
  application.setAttribute("time", new Date());
 %>
 Time:${applicationScope.time.time }
 <br>
 <!-- 等价于 -->
 <%-- 
  <%= ((Date)application.getAttribute("time")).getTime()%>
 --%>
  1. 与输入有关的隐含对象:param,patamValues
ScoreByParam:${param.score }
 <br>
 ElementsByParamValues:${paramValues.name[0] }
 <br>
 
 <!-- 6,其他隐含对象: pageContext 等 (cookie,header,initParam 了解即可) -->
 contextPath:${pageContext.request.contextPath }
 <br>
 
 sessionID:${pageContext.session.id }
 <br>
 
 sessionAttributeNames:${pageContext.session.attributeNames }
 <br>
  1. EL 运算符
${param.score > 70?"及格" : "不及格" }
 <br>
 
 <%
  List<String> names = new ArrayList<String>();
  names.add("cc");
  request.setAttribute("names", names);
 %>
 <!-- empty 可以作用于一个集合,若该集合不存在或集合中没有元素,其结果都为 true -->
 name is empty:${empty requestScope.names }
 <br>
  1. EL 自定义函数
    第一步:在一个类中定义静态函数
public class Fun {
 //结合两个字符串
 public static String Add(String str1,String str2) {
  return str1 + str2;
 }
}

第二步:在 tld 文件中

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">

    <!-- 描述tld 文件 -->
  <description>myTag 1.0</description>
  <display-name>myTag core</display-name>
  <tlib-version>1.0</tlib-version>
  
  <!-- 建议使用标签的前缀 -->
  <short-name>athome</short-name>
  <!-- 作为tid 文件的id,用来唯一标识当前的 tld 文件,多个tld 文件的uri 不能重复,通过 jsp 中的tablib 标签的 uri 来引用 -->
  <uri>http://www.athome.com/myTag/core</uri>
  
 <!-- 字符串相加 -->
  <function>
  <!-- 调用函数名 -->
   <name>add</name>
   
   <!-- 类原路径 -->
   <function-class>athome.simpletag.Fun</function-class>
   
   <!-- 函数签名 -->
   <function-signature>java.lang.String Add(java.lang.String,java.lang.String)</function-signature>
  </function>

</taglib>

第三步:在 jsp 文件中引入并使用函数

//第一步:引用 
<%@ taglib uri="http://www.athome.com/myTag/core" prefix="athome" %>
<body>

 //第二步:调用函数
 ${athome:add(param.name1,param.name2)}
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值