javaweb之EL自定义函数
2021-05-06 01:29
标签:xsd 标签 func body stl 完成 表达 expires 使用
1.什么是EL自定义函数
EL自定义函数是在EL表达式中调用的某个java类的静态方法,这个静态方法需在web应用程序中进行配置才可以被EL表达式调用。EL自定义函数可以扩展EL表达式的功能,让EL表达式完成普通java程序代码所能完成的功能。
2.EL自定义函数开发步骤
- 编写EL自定义函数映射的java类中的静态方法:这个Java类必须带有public修饰符,方法必须是这个类的带有public修饰符的静态方法;
- 编写标签库描述文件(tld文件),在tld文件中描述自定义函数;
- 在jsp页面中导入和使用自定义函数。
3.示例代码
实现的功能是连接两个字符串。
编写静态方法,有public修饰符,且为静态方法,elFunction.java
package com.javaweb.tag; public class elFunction { public static String concat(String str1,String str2){ return str1+str2; } }
编写标签库描述文件,即tld文件,相关的自定义函数的描述在function标签中,elFunction.tld
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 librarydescription> display-name>MyTag coredisplay-name> tlib-version>1.1tlib-version> short-name>cshort-name> uri>http://java.www.com/jsp/jstl/core/elFunctionuri> function> name>concatname> function-class>com.javaweb.tag.elFunctionfunction-class> function-signature>java.lang.String concat(java.lang.String,java.lang.String)function-signature> function> taglib>
jsp文件中导入和使用自定义函数。
@ 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="">
title>My JSP ‘elFunction.jsp‘ starting pagetitle>
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">
head>
body>
${koala:concat(param.name1,param.name2)}
body>
html>
运行后输出结果为:
转: https://www.cnblogs.com/naihuangbao/p/9910905.html
javaweb之EL自定义函数
标签:xsd 标签 func body stl 完成 表达 expires 使用
原文地址:https://www.cnblogs.com/fps2tao/p/13191305.html
上一篇:java输出字符串对齐问题
下一篇:Dijkstra算法思想