1. Question: What is TAG? How to implement a TAG? A tag is a normal Java class, which is the only special thing that it must inherit the Tagsupport or the Bodytagsupport class. These two classes provide some ways to be responsible for the interaction between JSP pages and the classes you have written, such as input, and output. And these two classes are provided by the JSP container without developers themselves. In other words, you only need to inherit tagsupport or Bodytagsupport, and do some special work, your class is a tag. And it is responsible for interacting with the JSP page, don't worry about you. "Special Work" usually has the following steps: 1) Provide a set method for attributes, which will now be set in JSP page. Take the JSTL tag as an example
2. A simple example: Outputtag
package diegoyun; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; / ** * @author chenys * / public class OutputTag extends TagSupport {private String name = NULL; public void setname (string name) {this.name = name;} public int desartTAG () throws jspexception {TRY {JSPWRITER OUT = pageContext.getOut (); out.print ("Hello!" name);} Catch (Exception E) {Throw new JSPEXCEPTION (E);} Return Eval_page;}} How to output to the JSP page: call jspwriter jspwriter out = pageContext.getut (); out.print ... Remember this method. 2. How to process a function after the output will return one of several values. Eval_page indicates that the TAG has been processed and returned to the JSP page. There are several values, such as Eval_Body_Again and Eval_Body_include, etc., then we will discuss the writing of TLDs
XML Version = "1.0" encoding = "ISO-8859-1">
Add a new TLDS folder under Web-INF, name Diego.TLD, put it under the TLDS folder. The path should be like this: web-inf / TLDS / DIEGO.TLD is a brief description of TLD: short-name: Taglib's name, also known as a prefix. For example, "C" Name: Tag's name in
The easiest tag is coming out. It's not difficult, is it?