1.什么是EL自定義函數(shù)
EL自定義函數(shù)是在EL表達(dá)式中調(diào)用的某個(gè)java類的靜態(tài)方法,這個(gè)靜態(tài)方法需在web應(yīng)用程序中進(jìn)行配置才可以被EL表達(dá)式調(diào)用。
EL自定義函數(shù)可以擴(kuò)展EL表達(dá)式的功能,讓EL表達(dá)式完成普通java程序代碼所能完成的功能。
2.EL自定義函數(shù)開(kāi)發(fā)步驟
編寫(xiě)EL自定義函數(shù)映射的java類中的靜態(tài)方法:這個(gè)Java類必須帶有public修飾符,方法必須是這個(gè)類的帶有public修飾符的靜態(tài)方法;
編寫(xiě)標(biāo)簽庫(kù)描述文件(tld文件),在tld文件中描述自定義函數(shù);
在jsp頁(yè)面中導(dǎo)入和使用自定義函數(shù)。
3.示例代碼
實(shí)現(xiàn)的功能是連接兩個(gè)字符串。
編寫(xiě)靜態(tài)方法,有public修飾符,且為靜態(tài)方法,elFunction.java
1
2
3
4
5
6
|
package com.javaweb.tag; public class elFunction { public static String concat(String str1,String str2){ return str1+str2; } } |
編寫(xiě)標(biāo)簽庫(kù)描述文件,即tld文件,相關(guān)的自定義函數(shù)的描述在function標(biāo)簽中,elFunction.tld
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/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version = "2.1" > < description >MyTag 1.1 core library</ description > < display-name >MyTag core</ display-name > < tlib-version >1.1</ tlib-version > < short-name >c</ short-name > < uri >http://java.www.com/jsp/jstl/core/elFunction</ uri > < function > < name >concat</ name > < function-class >com.javaweb.tag.elFunction</ function-class > < function-signature >java.lang.String concat(java.lang.String,java.lang.String)</ function-signature > </ function > </ taglib > |
jsp文件中導(dǎo)入和使用自定義函數(shù)。
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
|
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.www.com/jsp/jstl/core/elFunction" prefix="koala"%> <% 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%>" rel="external nofollow" > < title >My JSP 'elFunction.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" rel="external nofollow" > --> </ head > < body > ${koala:concat(param.name1,param.name2)} </ body > </ html > |
運(yùn)行后輸出結(jié)果為:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/naihuangbao/p/9910905.html