javaweb之EL自定义函数
2018-11-06 06:51:54来源:博客园 阅读 ()
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 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文件中导入和使用自定义函数。
<%@ 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%>">
<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">
-->
</head>
<body>
${koala:concat(param.name1,param.name2)}
</body>
</html>
运行后输出结果为:

标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:常用的Eclipse 快捷键
- 构造函数中的this()和super() 2020-06-10
- JAVA自定义注解 2020-06-01
- 自定义ClassLoader 2020-05-28
- 自定义持久层框架设计实现思路 2020-05-27
- Java连载118-编译一个类(包括内部函数、方法、类型、参数) 2020-05-27
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
