EL表达式

来源:互联网 发布:淘米网络私有化 编辑:程序博客网 时间:2024/05/17 01:25

一、     EL表达式概述                                                                              

 

El表达式就是为了输出使用。

三大作用域。Request session application(ServletContext)pageContext

二、     EL表达式语法                                                                             

格式:${…}

例如:${2/1} 2/1

 request.setAttritute(“user”,new User()); username/password<%out.print(request.getAttribute(“username”))%><%= request.getAttribute(“username”)%>pageContext request session application${username}${user.username} == user.getUserName()${user[‘username’]}

三、     EL表达式格式                                                                             

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

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

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

四、     EL运算符                                                                                  

五、     EL内置对象                                                                          

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

     pageScope

     requestScope

     sessionScope

     applicationScope

     param;

     paramValues;

     header;

     headerValues;

     initParam;

     cookie;

     pageContext;

5.1           域相关内置对象

域内置对象一共有四个:

     pageScope:${pageScope.name}等同与pageContext.getAttribute(“name”);

     requestScope:${requestScope.name}等同与request.getAttribute(“name”);

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

     applicationScope:${applicationScope.name}等同与application.getAttribute(“name”);

全域查找:${person}表示依次在pageScope、requesScopet、sessionScope、appliationScope四个域中查找名字为person的对象。

5.2           请求参数相关内置对象

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

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

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


5.3           请求头相关的内置对象

header和headerValues是与请求头相关的内置对象:

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

     headerValues:headerValues是Map<String,String[]>类型。当一个请求头名称,对应多个值时,使用该对象。

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

initParam:initParam是Map<String,String>类型。它对应web.xml文件中的<context-param>参数。

5.5           cookie相关内置对象

     cookie:cookie是Map<String,Cookie>类型,其中key是Cookie的名字,而值是Cookie对象本身。

5.6           pageContext内置对象

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


六、     EL函数库                                                                              

6.1           什么是EL函数库

EL函数库是由第三方对EL的扩展,我们现在学习的EL函数库是由JSTL添加的。EL函数库就是定义一些有返回值的静态方法。然后通过EL语言来调用它们!当然,不只是JSTL可以定义EL函数库,我们也可以自定义EL函数库。

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

6.2           导入EL函数库

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

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

6.3           EL函数库介绍

         String toUpperCase(String input):

     String toLowerCase(String input):

     int indexOf(String input, String substring):

     boolean contains(String input, Stringsubstring):

     boolean containsIgnoreCase(String input,String substring):

     booleanstartsWith(String input, String substring):

     boolean endsWith(String input, Stringsubstring):

     String substring(String input, intbeginIndex, int endIndex):

     String substringAfter(String input, Stringsubstring):hello-world, “-“

     substringBefore(Stringinput, String substring):hello-world, “-“

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

     String trim(String input):

     String replace(String input, StringsubstringBefore, String substringAfter):

     String[] split(String input, Stringdelimiters):

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

     String join(String array[], Stringseparator):

 

 

<%@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> -->
0 0
原创粉丝点击