Iv. Write labels with label bodies
Need to return to Tagsupport.eval_body_include, you can enter the label body;
Attributetag.java
Package org.tagext;
Import javax.servlet.jsp.tagext.*;
Import javax.servlet.jsp.*;
public class Attributetag extends tagsupport{
private String name;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int doStartTag () throws jspexception{
Object value = null;
Value = Super.pageContext.getAttribute (name,pagecontext.page_scope);
if (value==null) {return
tagsupport.skip_body;
}
else{return
tagsupport.eval_body_include
}}
Xiazdong.tld
<tag>
<name>attribute</name>
<tag-class>org.tagext.attributetag</tag-class >
<body-content>JSP</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute >
</tag>
attributetag.jsp
<%@ page contenttype= "text/html" pageencoding= "GBK"%> <%@ taglib prefix=
"Xiazdong" uri= "Xiazdong"%>
v. Iterative labels
Iterative labels are defined as repeated execution of the label body, often used in the output set, and often used in MVC;
Code instance:
I will not write some documents that are not changed;
Iteratetag.java
Package org.tagext;
Import javax.servlet.jsp.tagext.*;
Import javax.servlet.jsp.*;
Import java.util.*;
public class Iteratetag extends tagsupport{private String name;
Private String ID;
Private iterator<string> iter;
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 int doStartTag () throws jspexception{Object value = null;
Value = Super.pageContext.getAttribute (Name,pagecontext.page_scope); if (value==null| |! (Value instanceof list<?>))
{return tagsupport.skip_body;
else{iter = ((list<string>) value). iterator ();
if (Iter.hasnext ()) {Super.pageContext.setAttribute (Id,iter.next ());
return tagsupport.eval_body_include;
} else{return tagsupport.skip_body; The public int doafterbody () throws jspexception{if (Iter.hasnext ()) {Super.pageContext.setAttribute (Id,iter.nex
T ()); return tagsupport.eval_body_again;
} else{return tagsupport.skip_body; }
}
}
iteratetag.jsp
<%@ page contenttype= "text/html" pageencoding= "GBK" import= "java.util.*"%> "<%@ taglib" prefix= "Xiazdong"
Uri= "Xiazdong"%>
Six, Bodytagsupport class
The characteristic of Bodytagsupport is that the output content can be bodycontent and all output at once.
According to the above iteration output code, a slight change can be, the main change is in Iteratetag.java, need extends bodytagsupport;
Common methods of Bodytagsupport:
(1) Bodytagsupport. eval_body_buffered;
(2) Getpreviousout (); Get output stream from output to Web page
(3) The Bodycontent object stores the data;
Package org.tagext;
Import javax.servlet.jsp.tagext.*;
Import javax.servlet.jsp.*;
Import java.util.*;
public class Iteratetag extends bodytagsupport{private String name;
Private String ID;
Private iterator<string> iter;
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 int doStartTag () throws jspexception{Object value = null;
Value = Super.pageContext.getAttribute (Name,pagecontext.page_scope); if (value==null| |! (Value instanceof list<?>))
{return tagsupport.skip_body;
else{iter = ((list<string>) value). iterator ();
if (Iter.hasnext ()) {Super.pageContext.setAttribute (Id,iter.next ()); return tagsupport.eval_body_buffered;
exists in Bodycontent} else{return tagsupport.skip_body; '} ' public int doafterbody () throws jspexception{if (Iter.hasnext ()) {super.pageContext.setAtTribute (Id,iter.next ());
return tagsupport.eval_body_again;
} else{return tagsupport.skip_body; The public int Doendtag () throws jspexception{if (super.bodycontent!=null) {try{Super.bodyContent.writeOut (supe R.getpreviousout ());
Output to Page} catch (Exception e) {}} return bodytagsupport.eval_page; }
}
Vii. use of Tagextrainfo and Variableinfo
The id attribute in the <jsp:usebean id = "" > represents the object name, and the method can be invoked by this name, and if the effect is to be achieved, the Tagextrainfo class must be inherited;
We need to create a separate file to indicate that the object of the attribute can be used in the script;
Myextrainfo.java
Package Org.tag;
Import javax.servlet.jsp.tagext.*;
public class Mytagextrainfo extends tagextrainfo{public
variableinfo[] Getvariableinfo (tagdata data) {
return New Variableinfo[]{new Variableinfo (Data.getid (), "java.lang.String", true,variableinfo.nested)}
After writing, add in Xiazdong.tld:
<tei-class> /Note
org.tag.MyTagExtraInfo
</tei-class>
The last xiazdong.tld to:
<tag>
<name>iterate</name>
<tag-class>org.tagext.IterateTag</tag-class>
<body-content>JSP</body-content>
<tei-class> /Note
org.tag.MyTagExtraInfo
</tei-class>
<attribute>
<name>name</name>
<required>true</ required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</ rtexprvalue>
</attribute>
</tag>
In this way in the JSP can be used in scriptlet;
<%@ page contenttype= "text/html" pageencoding= "GBK" import= "java.util.*"%> "<%@ taglib" prefix= "Xiazdong"
Uri= "Xiazdong"%>