Java web----EL函数库

来源:互联网 发布:做班服的软件 编辑:程序博客网 时间:2024/06/05 17:32

1 What is 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" %>

3 EL函数库介绍

  • String toUpperCase(String input):
  • String toLowerCase(String input):
  • int indexOf(String input, String substring):
  • boolean contains(String input, String substring):
  • boolean containsIgnoreCase(String input, String substring):
  • boolean startsWith(String input, String substring):
  • boolean endsWith(String input, String substring):
  • String substring(String input, int beginIndex, int endIndex):
  • String substringAfter(String input, String substring):hello-world, “-“
  • substringBefore(String input, String substring):hello-world, “-“
  • String escapeXml(String input):把字符串的“>”、“<”。。。转义了!
  • String trim(String input):
  • String replace(String input, String substringBefore, StringsubstringAfter):
  • String[] split(String input, String delimiters):
  • int length(Object obj):可以获取字符串、数组、各种集合的长度!
  • String join(String array[], String separator):

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="com.cug.bean01.Person" %><%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'person.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>   <%   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:substring("0123456789",6,9) }<br/><!-- 678 -->   ${fn:substring("0123456789",5,-1) }<br/><!-- 56789 -->   ${fn:substringAfter("hello-world","-") }<br/><!-- 56789 -->   ${fn:substringBefore("hello-world","-") }<br/><!-- 56789 -->   ${fn:trim(   "hello"   ) }<br><!-- hello -->   ${fn:escapeXml("<html></html>") }<br/><!-- <html></html> -->  </body></html>

4 自定义EL函数库

  • 写一个类,写一个有返回值的静态方法;
  • 编写saylove.tld文件,可以参数fn.tld文件来写,把saylove.tld文件放到/WEB-INF目录下;
  • 在页面中添加taglib指令,导入自定义标签库。

saylove.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">      <description>JSTL 1.1 functions library of mine</description>  <display-name>JSTL functions of mine</display-name> <tlib-version>1.1</tlib-version> <short-name>love</short-name>  <uri>http://cug.com/jsp/jstl/myfunctions</uri>  <function>        <name>sayLove</name>    <function-class>com.cug.bean01.MyFunction</function-class>    <function-signature>java.lang.String sayLove()</function-signature>  </function> </taglib>

MyFunction.java

package com.cug.bean01;public class MyFunction {public static String sayLove(){return "I Love you!!!";}}

index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="com.cug.bean01.Person" %><%@taglib prefix="love" uri="http://cug.com/jsp/jstl/myfunctions" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'person.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>${love:sayLove() }  </body></html>



0 0
原创粉丝点击