首页 / JAVA / javaweb之EL自定义函数
javaweb之EL自定义函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javaweb之EL自定义函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2406字,纯文字阅读大概需要4分钟。
内容图文
![javaweb之EL自定义函数](/upload/InfoBanner/zyjiaocheng/629/4b872fb960504d79b87e9ef345f810c2.jpg)
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>
运行后输出结果为:
转: https://www.cnblogs.com/naihuangbao/p/9910905.html
内容总结
以上是互联网集市为您收集整理的javaweb之EL自定义函数全部内容,希望文章能够帮你解决javaweb之EL自定义函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。