个性化阅读
专注于IT技术分析

JSP自定义标签中的自定义URI

我们可以使用自定义URI来告诉Web容器有关tld文件的信息。在这种情况下, 我们需要在web.xml中定义taglib元素。 Web容器从web.xml文件中获取有关指定URI的tld文件的信息。

在JSP自定义标签中使用自定义URI的示例

在此示例中, 我们将在JSP文件中使用自定义uri。对于此应用程序, 我们需要专注于4个文件。

  • index.jsp
  • web.xml
  • mytags.tld
  • PrintDate.java

index.jsp

<%@ taglib uri="mytags" prefix="m" %>
Today is: <m:today></m:today>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app 
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
  "http://java.sun.com/dtd/web-app_2_3.dtd"> 

<web-app>

<jsp-config>
<taglib>
<taglib-uri>mytags</taglib-uri>
<taglib-location>/WEB-INF/mytags.tld</taglib-location>
</taglib>
</jsp-config>

</web-app>

mytags.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>simple</short-name>
  <uri>mytags</uri>
  <description>A simple tab library for the examples</description>

  <tag>
    <name>today</name>
    <tag-class>com.srcmini.taghandler.PrintDate</tag-class>
  </tag>
</taglib>

PrintDate.java

package com.srcmini.taghandler;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PrintDate extends TagSupport{

public int doStartTag() throws JspException {
	JspWriter out=pageContext.getOut();
	try{
		out.print(java.util.Calendar.getInstance().getTime());
	}catch(Exception e){e.printStackTrace();}
	
	return SKIP_BODY;
	}
	

}
赞(0)
未经允许不得转载:srcmini » JSP自定义标签中的自定义URI

评论 抢沙发

评论前必须登录!