Taglib Applications in JSP (4)--zt

Source: Internet
Author: User
Tags define continue include interface resource valid
Taglib applications in JS JSP (4)


Author: FALSE/ASPCN

----------------------------------------------------------------------

Next to the key part of the music. The tag is processed. In fact, in many cases we are using the taglib already provided.

Someone/company has done the tag and processing part, the package we need to do is only in our JSP to apply.

But when we do a taglib ourselves, we need to write this part of the tag handler.

This is only for insert tag mentioned in the above file, others to avoid repetition.

==================== inserttag.java==============================
/*

* $Id: Inserttag.java,v 1.13 2000/03/04 02:54:57 Brydon EXP $

* Copyright 1999 Sun Microsystems, Inc. All rights reserved.

* Copyright 1999 Sun Microsystems, Inc. tous droits réservés.

*/



Package com.sun.estore.taglib;



Import javax.servlet.jsp.JspTagException;

Import Javax.servlet.jsp.tagext.TagSupport;



Import Com.sun.estore.util.Debug;



/**

* This class is a easy interface to the JSP template or other

* Text that needs to be inserted.

* @author Greg Murray

*/



public class Inserttag extends TagSupport {



Private Boolean directinclude = false;

Private String parameter = NULL;

Private String templatename = null;

Private Template Template = null;

Private Templateparameter templateparam = null;



/**

* Default constructor

*/

Public Inserttag () {

Super ();

}



public void SetTemplate (String templatename) {

This.templatename = templatename;

}



public void Setparameter (String parameter) {

This.parameter = parameter;

}



public int doStartTag () {

try{

if (templatename!= null) {

Template = (template) pagecontext.getrequest (). getattribute ("template");

}

catch (NullPointerException e) {

Debug.println ("Error extracting template from session:" + E);

}



if (parameter!= null && template!= null) Templateparam = (templateparameter) template.getparam (parameter);

if (Templateparam!= null) Directinclude = Templateparam.isdirect ();

return skip_body;

}



public int Doendtag () throws Jsptagexception {

try{

Pagecontext.getout (). Flush ();

catch (Exception e) {

Doing nothing

}

try {

if (directinclude && templateparam!= null) {

Pagecontext.getout (). println (Templateparam.getvalue ());

else if (Templateparam!= null) {

if (Templateparam.getvalue ()!= null) pagecontext.getrequest (). Getrequestdispatcher (Templateparam.getvalue ()). Include (Pagecontext.getrequest (), Pagecontext.getresponse ());

}

catch (Throwable ex) {

Ex.printstacktrace ();

}

return eval_page;

}

}
Can see. Inserttag.java inherits the Javax.servlet.jsp.tagext.TagSupport class. Because some interfaces are defined in the TagSupport.
When we handle the custom tag, we overload the doStartTag () and Doendtag () of the parent class, and if you define the Tag property in the TLD file, you need to define the corresponding Setxxx/getxxx method for each property in the tag handler.

In doStartTag (), the Templateparameter object is obtained from the Hashtable defined by the template class.

In Doendtag ()

Pagecontext.getrequest (). Getrequestdispatcher (Templateparam.getvalue ()). Include (Pagecontext.getrequest (), Pagecontext.getresponse ());

This is what is generated by the resource object (RequestDispatcher) that is returned through the context of the JSP page, as specified by the property value of tag.

doStartTag () and Doendtag () return values are static int defined in tag interface

Skip_body implied 0

Skip body evaluation. Valid return value for doStartTag and Doafterbody. Skip the handling of the body.

is to skip the code between the start and end tags.



Eval_body_include implied 1

Evaluate body into the existing out stream. Valid return value for doStartTag.

This is a illegal return value to doStartTag when the class implements Bodytag,

Since Bodytag implies the creation of a new bodycontent.

The contents of the body are exported to the existing output stream. Include JSP code, also can be output

Skip_page implied 5

Skip the rest of the page. Valid return value for Doendtag.

Ignore the remaining pages.

Eval_page implied 6

Continue evaluating the page. Valid return value for Doendtag ().

Continue to the following page

There are references to other classes in this class, and I'm not going to list them. You can do your own research.

Custom tag (custom tags) implements the Javax.servlet.jsp.tagext.Tag or Javax.servlet.jsp.tagext.BodyTag

Interface. Apply Javax.servlet.jsp.JspWriter to output.


TagSupport class provides an implicit implementation of the interface tag. Serialization of the braided data.

public class TagSupport extends Java.lang.Object implements Tag, java.io.Serializable


The Bodytagsupport class provides an implicit implementation of the interface Bodytag. Inherit TagSupport

public class Bodytagsupport Extendstagsupport implements Bodytag



We need to inherit TagSupport class or Bodytagsupport class when writing tag handler, and then Overload doStartTag () and Doendtag ().

can be further separated. Put the concrete implementation into the bean

The classes of these custom tags should be placed in web-inf/classes or Web-inf/lib

Finally, custom tag libraries is packaged into a. war file. For the. war file, a resource map is given as shown in the following illustration.




Control mappings by deploying descriptors.

The deployment descriptor is an XML file. Please refer to the relevant documentation for detailed information. This only means

Deploy the Taglib section.

JSP Custom tag libraries (optional)

Specifies URL for locating Tag Library Descriptor


...


Uri

Path


...




/************** Custom Taglib Example: JSP page *******************

.........








*******************************************************/

The file location is as follows:




This example is very simple, just use it as an exercise. You can also expand on your own and practice a few more ways. For example, the tag has attributes,

Tags with content, using bean processing logic, and so on

About packaging these files into a. war file. Here is not a detailed description. Please do it yourself according to the application server you use.

Related Article

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.