A Demo of tag
<%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %> In tagdir is the path to introduce the tag file to be used, and the prefix is to set how this jsp calls the tag file (two easy files in this article). must be consistent). jsp code:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <!-- Import css --> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="X-UA-Compatible"content="IE=EmulateIE9"/> <%@ taglib prefix="easy" tagdir="/WEB-INF/tags" %> <title>Tag File Introduction Example</title> <link > </head> <body> Today is <easy:tagDemo/> </body> </html>
tag file code:
<%@ tag import="java.util.Date" import="java.text.DateFormat"%> <% DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG); Date now = new Date(System.currentTimeMillis()); out.println(dateFormat.format(now)); %>
Difference between TAG file and TLD file in JSP
In jsp files, tag and tld files can be referenced.
1. For the tag file
<%@ taglib prefix="ui" tagdir="/WEB-INF/tags" %>
the tags are a directory with several tag files in it.
But when using <ti:XXXX>, under the directory WEB-INF/tags, there must be a XXXX.tag file corresponding to it.
TLD files can be referenced in jsp, such as
<%@ taglib uri=" http://struts.apache.org/tags-html" prefix="html"%>
but this http://struts.apache.org/tags What does -html correspond to?
Jsp will find all tld files in \WEB-INF in the current directory, and confirm which TLD file this URL corresponds to.
When the struts-html.tld file is found, it is found that the inside corresponds to this URL.
But when using <html:YYYYY>, there must be a corresponding YYYY item in this TLD file.
There is also a safer way to add
<taglib>
<taglib-uri>
http://jakarta.apache.org/tomcat/examples-taglib
</taglib-uri>
< in WEB-INF/web.xml taglib-location>
</taglib-location>
</taglib>
This means that http://jakarta.apache.org/tomcat/examples-taglib corresponds to /WEB-INF/jsp/example-taglib.tld The
tld file has a complex function and can achieve many Advanced role.
2. For tld files
The role of the tag file is generally a small piece of code, similar to the role of the include file.