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;