我们直接用代码来说明,如何自定义java中JSP的JSTL函数
马上进入正题
1.在WEB-INF/文件夹下创建.tld文件
<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.1" 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"> <tlib-version>1.0</tlib-version> <short-name>mytlds</short-name> <uri>http://www.wutongwei.com/myTlds</uri> <function> <name>charAt</name> <function-class>org.tonway.expressions.Functions</function-class> <function-signature>char charAt(java.lang.String, int)</function-signature> </function> </taglib>
2.创建类文件
package org.tonway.expressions; /** * * @author freaking_crack */ public class Functions { public static char charAt(String input, int index) { return input.charAt(index, 0, 0); } }
3.在JSP文件里使用
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@taglib uri="http://www.wutongwei.com/myTlds" prefix="mt" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> ${mt:charAt("AB",0)} <!-- It will give you A--> </body> </html>