JSP Custom Label (3) __jsp

Source: Internet
Author: User

Eight, Simpletagsupport class


After JSP2.0, in order to simplify the difficulty of label development, you can use simpletagsupport for development;


1. Developing generic labels

Note the point:

(1) need to inherit Simpletagsupport class;

(2) Realizing the public void Dotag() throws jspexception,ioexception;

(3) Super. Getjspcontext (). getout (). Write   ("...."); for output;

(4) in Simpletagsupport, the <body-content> content in TLD can not be JSP, if the label body is not empty, it can only be scriptless;


Code:

Simpletagsupportdemo.java

Package org.tagext;
Import javax.servlet.jsp.tagext.*;
Import javax.servlet.jsp.*;
Import java.io.*;
public class Simpletagsupportdemo extends simpletagsupport{
	private String name;
	Public String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	public void Dotag () throws jspexception,ioexception{
		Super.getjspcontext (). Getout (). Write ("


2. Development of iterative labels
Through Super.getjspbody (). Invoke (null); Can execute tag body content;
Simpletagsupportdemo.java

Package org.tagext;
Import javax.servlet.jsp.tagext.*;
Import javax.servlet.jsp.*;
Import java.io.*;
Import java.util.*;
public class Simpletagsupportdemo extends simpletagsupport{
	private String name;
	Private String ID;
	Public String GetName () {return
		name;
	}
	public void SetName (String name) {
		this.name = name;
	}
	Public String GetId () {return
		ID;
	}
	public void SetId (String id) {
				this.id = ID;
	}
	public void Dotag () throws jspexception,ioexception{
		Object value = Super.getjspcontext (). getattribute (Name, Pagecontext.page_scope);
		Iterator<string> iter = ((list<string>) value). iterator ();
		while (Iter.hasnext ()) {
			super.getjspcontext (). setattribute (Id,iter.next ());
			Super.getjspbody (). Invoke (null);}}}

In general, Simpletagsupport is much simpler than the previous tagsupport,bodytagsupport and does not require any return value;

ix. FAQ

1. Distinguish whether there is a label body

<xiazdong:hello name= "" >

</xiazdong:hello>

It belongs to the label body, but the label body is empty;

<xiazdong:hello name= ""/> is empty for the label body;



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.