EL(表达式语言)

来源:互联网 发布:时空数据可视化 编辑:程序博客网 时间:2024/06/04 19:05
1EL概述

1.1EL的作用

JSP2.0要把htmlcss分离、要把htmljavascript分离、要把Java脚本替换成标签。标签的好处是非Java人员都可以使用。

JSP2.0 – 纯标签页面,即:不包含<% … %><%! … %>,以及<%= … %>

ELExpression Language)是一门表达式语言,它对应<%=…%>。我们知道在JSP中,表达式会被输出,所以EL表达式也会被输出。

 

1.2EL的格式

格式:${…}

例如:${1 + 2}

 

1.3 关闭EL

如果希望整个JSP忽略EL表达式,需要在page指令中指定isELIgnored=”true”。

如果希望忽略某个EL表达式,可以在EL表达式之前添加“\”,例如:\${1 + 2}

 

1.4EL运算符

运算符

说明

范例

结果

+

${17+5}

22

-

${17-5}

12

*

${17*5}

85

/div

${17/5}${17 div 5}

3

%mod

取余

${17%5}${17 mod 5}

2

==eq

等于

${5==5}${5 eq 5}

true

!=ne

不等于

${5!=5}${5 ne 5}

false

<lt

小于

${3<5}${3 lt 5}

true

>gt

大于

${3>5}${3 gt 5}

false 

<=le

小于等于

${3<=5}${3 le 5}

true 

>=ge

大于等于

${3>=5}${3 ge 5}

false 

&&and

并且

${true&&false}${true and false}

false 

!not

${!true}${not true}

false

||or

或者

${true||false}${true or false}

true

empty

是否为空

${empty “”},可以判断字符串、数据、集合的长度是否为0,为0返回trueempty还可以与not!一起使用。${not empty “”}

true

 

1.5EL不显示null

  当EL表达式的值为null时,会在页面上显示空白,即什么都不显示。

2EL表达式格式

先来了解一下EL表达式的格式!现在还不能演示它,因为需要学习了EL11个内置对象后才方便显示它。

l 操作List和数组:${list[0]}${arr[0]}

l 操作bean的属性:${person.name}${person[‘name’]},对应person.getName()方法;

l 操作Map的值:${map.key}${map[‘key’]},对应map.get(key)

 

3EL内置对象

EL一共11个内置对象,无需创建即可以使用。这11个内置对象中有10个是Map类型的,最后一个是pageContext对象。

l pageScope

l requestScope

l sessionScope

l applicationScope

l param

l paramValues

l header

l headerValues

l initParam

l cookie

l pageContext

 

3.1 域相关内置对象(重点)

域内置对象一共有四个:

l pageScope${pageScope.name}等同与pageContext.getAttribute(“name”)

l requestScope${requestScope.name}等同与request.getAttribute(“name”)

l sessionScoep: ${sessionScope.name}等同与session.getAttribute(“name”)

l applicationScope${applicationScope.name}等同与application.getAttribute(“name”)

 

如果在域中保存的是JavaBean对象,那么可以使用EL来访问JavaBean属性。因为EL只做读取操作,所以JavaBean一定要提供get方法,而set方法没有要求。

Person.java

public class Person {private String name;private int age;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}

 

 

 

  全域查找:${person}表示依次在pageScoperequesScopetsessionScopeappliationScope四个域中查找名字为person的属性。

 

3.2 请求参数相关内置对象

  paramparamValues这两个内置对象是用来获取请求参数的。

l paramMap<String,String>类型,param对象可以用来获取参数,与request.getParameter()方法相同。

 

 

注意,在使用EL获取参数时,如果参数不存在,返回的是空字符串,而不是null。这一点与使用request.getParameter()方法是不同的。

 

 

l paramValuesparamValuesMap<String, String[]>类型,当一个参数名,对应多个参数值时可以使用它。

 

 

3.3 请求头相关内置对象

headerheaderValues是与请求头相关的内置对象:

l header: Map<String,String>类型,用来获取请求头。

 

l headerValuesheaderValuesMap<String,String[]>类型。当一个请求头名称,对应多个值时,使用该对象,这里就不在赘述。

3.4 应用初始化参数相关内置对象

l initParaminitParamMap<String,String>类型。它对应web.xml文件中的<context-param>参数。

 

 

3.5Cookie相关内置对象

l cookiecookieMap<String,Cookie>类型,其中keyCookie的名字,而值是Cookie对象本身。

 

3.6pageContext对象

pageContextpageContextPageContext类型!可以使用pageContext对象调用getXXX()方法,例如pageContext.getRequest(),可以${pageContext.request}。也就是读取JavaBean属性!!!

 

EL表达式

说明

${pageContext.request.queryString}

pageContext.getRequest().getQueryString();

${pageContext.request.requestURL}

pageContext.getRequest().getRequestURL();

${pageContext.request.contextPath}

pageContext.getRequest().getContextPath();

${pageContext.request.method}

pageContext.getRequest().getMethod();

${pageContext.request.protocol}

pageContext.getRequest().getProtocol();

${pageContext.request.remoteUser}

pageContext.getRequest().getRemoteUser();

${pageContext.request.remoteAddr}

pageContext.getRequest().getRemoteAddr();

${pageContext.session.new}

pageContext.getSession().isNew();

${pageContext.session.id}

pageContext.getSession().getId();

${pageContext.servletContext.serverInfo}

pageContext.getServletContext().getServerInfo();

 

EL函数库

 

1 什么EL函数库

  EL函数库是由第三方对EL的扩展,我们现在学习的EL函数库是由JSTL添加的。JSTL明天再学!

EL函数库就是定义一些有返回值的静态方法。然后通过EL语言来调用它们!当然,不只是JSTL可以定义EL函数库,我们也可以自定义EL函数库。

  EL函数库中包含了很多对字符串的操作方法,以及对集合对象的操作。例如:${fn:length(“abc”)}会输出3,即字符串的长度。

 

2 导入函数库

  因为是第三方的东西,所以需要导入。导入需要使用taglib指令!

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

 

3EL函数库介绍

l String toUpperCase(String input):

l String toLowerCase(String input):

l int indexOf(String input, String substring):

l boolean contains(String input, String substring):

l boolean containsIgnoreCase(String input, String substring):

l boolean startsWith(String input, String substring):

l boolean endsWith(String input, String substring):

l String substring(String input, int beginIndex, int endIndex):

l String substringAfter(String input, String substring):hello-world, “-“

l substringBefore(String input, String substring):hello-world, “-“

l String escapeXml(String input):把字符串的“>”、“<”。。。转义了!

l String trim(String input):

l String replace(String input, String substringBefore, String substringAfter):

l String[] split(String input, String delimiters):

l int length(Object obj):可以获取字符串、数组、各种集合的长度!

l String join(String array[], String separator):

 

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>…String[] strs = {"a", "b","c"};List list = new ArrayList();list.add("a");pageContext.setAttribute("arr", strs);pageContext.setAttribute("list", list);%>${fn:length(arr) }<br/><!--3-->${fn:length(list) }<br/><!--1-->${fn:toLowerCase("Hello") }<br/> <!-- hello -->${fn:toUpperCase("Hello") }<br/> <!-- HELLO -->${fn:contains("abc", "a")}<br/><!-- true -->${fn:containsIgnoreCase("abc", "Ab")}<br/><!-- true -->${fn:contains(arr, "a")}<br/><!-- true -->${fn:containsIgnoreCase(list, "A")}<br/><!-- true -->${fn:endsWith("Hello.java", ".java")}<br/><!-- true -->${fn:startsWith("Hello.java", "Hell")}<br/><!-- true -->${fn:indexOf("Hello-World", "-")}<br/><!-- 5 -->${fn:join(arr, ";")}<br/><!-- a;b;c -->${fn:replace("Hello-World", "-", "+")}<br/><!-- Hello+World -->${fn:join(fn:split("a;b;c;", ";"), "-")}<br/><!-- a-b-c --> ${fn:substring("0123456789", 6, 9)}<br/><!-- 678 -->${fn:substring("0123456789", 5, -1)}<br/><!-- 56789 -->${fn:substringAfter("Hello-World", "-")}<br/><!-- World -->${fn:substringBefore("Hello-World", "-")}<br/><!-- Hello -->${fn:trim("     a b c     ")}<br/><!-- a b c -->${fn:escapeXml("<html></html>")}<br/> <!-- <html></html> -->

 

 

4 自定义EL函数库

l 写一个类,写一个有返回值的静态方法;

l 编写itcast.tld文件,可以参数fn.tld文件来写,把itcast.tld文件放到/WEB-INF目录下;

l 在页面中添加taglib指令,导入自定义标签库。

 

ItcastFuncations.java

package cn.itcast.el.funcations; public class ItcastFuncations {public static String test() {return "传智播客自定义EL函数库测试";}}

 

itcast.tld(放到classes下)

<?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">      <tlib-version>1.0</tlib-version>  <short-name>itcast</short-name>  <uri>http://www.itcast.cn/jsp/functions</uri>   <function>    <name>test</name>    <function-class>cn.itcast.el.funcations.ItcastFuncations</function-class>    <function-signature>String test()</function-signature>  </function></taglib>

 

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="itcast" uri="/WEB-INF/itcast.tld" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <body>   <h1>${itcast:test() }</h1>  </body></html>


0 0