1,定义java静态方法,FunctionDemo,有小写转大写的功能
package com.itheima.domain; public class FunctionDemo { public static String toUpperCase(String string){ return string.toUpperCase(); } }
2,编辑xml文件,扩展名为tld
需要指定类的详细名称,带包名,方法的详细信息,返回值+方法名+参数,和访问路径uri,在页面中导入这个uri才能使用,前缀
<?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"> <tlib-version>1.0</tlib-version> <short-name>itheima</short-name> <uri>http://www.heima.com/jsp/myfun</uri> <function> <description> toUpperCase </description> <name>toUpperCase</name> <function-class>com.itheima.domain.FunctionDemo</function-class> <function-signature>java.lang.String toUpperCase(java.lang.String)</function-signature> <example> <itheima:toUpperCase("aavvavb")> </example> </function> </taglib>
3在页面中使用,需要用taglib标签,指定uri和前缀
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib uri="http://www.heima.com/jsp/myfun" prefix="itheima" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> ${itheima:toUpperCase("adsafjka") } </body> </html>