8. simpletagsupport class
After jsp2.0, simpletagsupport can be used for development to simplify tag development;
1. Develop general labels
Note:
(1) The simpletagsupport class must be inherited;
(2) implement public voidDotag() Throws jspexception, ioexception;
(3) super.Getjspcontext().Getout().Write("..."); Output;
(4) In simpletagsupport, the content of <body-content> in TLD cannot be JSP. If the label body is not empty, it can only beScriptless;
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. The development iteration tag is passed through Super. getjspbody (). Invoke (null); the label body content can be executed; simpletagsuppdemo demo. 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 and bodytagsupport, and no return value is required.
1. Identify whether there is a TAG body
<Xiazdong: Hello name = "">
</Xiazdong: Hello>
It belongs to a TAG body, but the TAG body is empty;
<Xiazdong: Hello name = ""/> the label body is empty;