1. 自定义标签,输出到页面上(可用于css、scripte等标签简写)
首先:定义标签处理类
package com.inspur.test.taglabel; import java.io.IOException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class testTagLabel extends TagSupport { private String name = "lili"; private String pwd = "lucky"; public int doStartTag (){ try { JspWriter out = this.pageContext.getOut(); out.println("===============lili+++lucky============"); out.println("<br>"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SKIP_BODY; } }
其次,定义标签tld
<?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 web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>jsplabel</short-name> <uri>/tags/tagtest</uri> <tag> <name>showUserInfo</name> <tag-class>com.inspur.test.taglabel.testTagLabel</tag-class> <body-content>empty</body-content> <attribute> <name>user</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> <attribute> <name>pwd</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <type>java.lang.String</type> </attribute> </tag> </taglib>
然后,在web-inf下的web.xml里配置映射
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>testJsp</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <jsp-config> <taglib> <taglib-uri>/tags/tagtest</taglib-uri> <taglib-location>/WEB-INF/tagtest.tld</taglib-location> </taglib> </jsp-config> </web-app>
最后,在jsp里使用:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="jsplabel" uri="/tags/tagtest"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <jsplabel:showUserInfo/> </body> </html>
输出结果:
===============lili+++lucky============
如果是<script .........>====>可以被解析成引入js,简单方便
2. 自定义函数标签
首先:定义标签处理类,一定注意,定义的函数必须是public static的,切记
package com.inspur.test.taglabel;
public class TestFunLabel {
public static String getUserName (){
return "=============name==lili========";
}
}
其次,定义函数标签tld
<?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 web-jsptaglibrary_2_0.xsd" version="2.0"> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>jsplabel</short-name> <uri>/tags/testfun</uri> <function> <name>getUserName</name> <function-class>com.inspur.test.taglabel.TestFunLabel</function-class> <function-signature>java.lang.String getUserName()</function-signature> </function> </taglib>
然后,在web-inf下的web.xml里配置映射
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>testJsp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/tags/testfun</taglib-uri>
<taglib-location>/WEB-INF/tlds/funtest.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
最后,在jsp里使用:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="fn" uri="/tags/testfun"%>
<!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>
${fn:getUserName()}
</body>
</html>
输出结果:‘
=============name==lili========