How to use custom tags in javaweb programs (tag, TLD two kinds) __java

Source: Internet
Author: User
Tags stub tld

TLD Definition Format

<?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/dtd/
Web-jsptaglibrary_1_2.dtd "> <taglib> <!--definition Tag version library--> <tlib-version>1.0</tlib-version> <!--definition JSP version library--> <jsp-version>1.2</jsp-version> <short-name>cms tag</short-name> < description><!--Tag Description---> A simple appbase Tag library </description> <tag> <name>page</ name><!--Tag's name--> <tag-class>com.cms.common.tag.PageTag</tag-class><!-- Tag corresponding Java class name--> <body-content>empty</body-content> <!--about Body-content has three values optional; empty: The label body must be empty; JSP: Tags are composed of other JSP elements, such as tags containing <%=attributeName%> jsp elements, then body-content value is the actual AttributeName incoming value; Tagdependent: There are tag explanations without JSP conversions (this deep meaning is not very well known)--> <attribute><!---Here is a parameter for this tag--> <name>cmsform</name> <!--the name of this parameter--> <required>true</required><!--whetheris a required option--> <rtexprvalue>true</rtexprvalue><!--Whether the value of this parameter can be written, in other words, whether this parameter can be dynamically assigned--> </
 Attribute> </tag> </taglib>

define tag corresponding class

This class must override doStartTag and Doendtag methods

/**
 * * * * * *
package com.cms.common.tag;

Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import Javax.servlet.jsp.tagext.TagSupport;

/**
 * @author Louisliao * */Public
class Demoviewtag extends TagSupport {
	/**
	 * * 
	 *
	private static final long serialversionuid = 1L;
	Private String Cmsform = "";	
	Public String Getcmsform () {return
		cmsform;
	}
	public void Setcmsform (String cmsform) {
		this.cmsform = cmsform;
	}
		/**
	 * 
	 *
	/Public Demoviewtag () {
		//TODO auto-generated constructor stub
	} public
	int doStartTag ()
	{return
		super. skip_body;
	}
	public int Doendtag () throws Jspexception
	{
		jspwriter writer=pagecontext.getout ();
		try {
			Writer.print ("This is my tag example <br/>" + "Cmsform:" +this.cmsform);
		} catch (Exception e) {
			//Todo:handle Exception
			e.printstacktrace ();
		}
		Return super. Eval_page
	}
}

add taglib corresponding file configuration in Web.xml

Such as:

<taglib>
<taglib-uri>http://mytag.sf.net</taglib-uri>
<taglib-location>/ Web-inf/mytag.tld</taglib-location>
</taglib>

This means that the http://mytag.sf.net corresponds to the Web-inf/mytag.tld file

referencing in JSP pages

Such as:

<%@ taglib uri= "http://mytag.sf.net" prefix= "MyTag"%>

using in JSP pages

<myTag:exname1><myTag:exname1>

Example:

Define MYTAG.TLD label file

<?xml version= "1.0" encoding= "Utf-8"?> <! DOCTYPE taglib Public "-//sun Microsystems, Inc.//dtd JSP Tag Library 1.1//en" "Http://java.sun.com/j2ee/dtds/web-jspta Glibrary_1_1.dtd "> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</ jspversion> <shortname>myjsptag library</shortname> <uri>http://mytag.sf.net</uri> < info> my sample Tag library </info> <tag> <name>demo. Viewport</name> <tagclass>com.myapp.web.tag.DemoViewTag</tagclass> <bodycontent>jsp</ Bodycontent> <info>demo. Viewport tags </info> <attribute> <name>northTitle</name> &LT;REQUIRED&GT;TRUE&LT;/REQUIRED&G
			T <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>westtitle</name&
			Gt
<required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> 

Defining label Classes

/** * * * * * * Package com.myapp.web.tag;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;

Import Javax.servlet.jsp.tagext.TagSupport; /** * @author Louisliao * * */public class Demoviewtag extends TagSupport {/** * */private static final long SE
	Rialversionuid = 1L;
	Private String Northtitle = "";
	
	Private String Westtitle = "";
	Public String Getnorthtitle () {return northtitle;
	} public void Setnorthtitle (String northtitle) {this.northtitle = Northtitle;
	Public String Getwesttitle () {return westtitle;
	} public void Setwesttitle (String westtitle) {this.westtitle = Westtitle;  /** * */Public Demoviewtag () {//TODO auto-generated constructor stub} public int doStartTag () {return Super.
	Skip_body;
		public int Doendtag () throws Jspexception {JspWriter writer=pagecontext.getout ();
		try {writer.print ("This is my tag example <br/>westtitle:" +this.westtitle+ "<br/>northtitle:" +this.northtitle); catch (excEption e) {//Todo:handle exception e.printstacktrace (); Return super.
	Eval_page;
 }
}

Web.xml Add Configuration

<taglib>
<taglib-uri>http://mytag.sf.net</taglib-uri>
<taglib-location>/ Web-inf/mytag.tld</taglib-location>
</taglib>

test Page

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <%@ taglib uri= "Http://mytag.sf.net"
prefix= "MyTag"%> <% String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.