EL函数库以及自定义EL函数库

xiaoxiao2021-02-28  24

EL函数库以及自定义EL函数库

一、概述

 

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

EL函数库就是定义一些有返回值的静态方法。然后通过EL语言来调用它们!当然,不只是JSTL可以定义EL函数库,我们也可以自定义EL函数库。EL函数库中包含了很多对字符串的操作方法,以及对集合对象的操作。

二、导入函数库

 

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

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

三、EL库函数

 

 Java Code 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

 

<%@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"69)}<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> -->

四、自定义EL函数库

 

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

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

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

例:

1,Funcations .java

 Java Code 

1 2 3 4 5 6 7 8

 

package cn.wj.el.funcations;public class Funcations {     public static String test() {         return "自定义EL函数库测试";     } }

2,wj.tld(放到classes下)

 

 Java Code 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

 

<?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.wj.cn/jsp/functions</uri>   <function>     <name>test</name>     <function-class>cn.wj.el.funcations.Funcations</function-class>     <function-signature>String test()</function-signature>   </function> </taglib>

3,index.jsp

 

 Java Code 

1 2 3 4 5 6 7 8 9 10

 

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

 

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-2602549.html

最新回复(0)