TAGLIB principle and implementation Chapter 1: To achieve the simplest TAG

xiaoxiao2021-03-06  90

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 , this value is the entry between JSP data to TAG. So there must be a setValue method in TAG, and the specific properties can be called Value. For example, setValue (String Data) {this.data = data;} This "value" name is defined in TLD. What is the name, you can provide a corresponding set method in tag. 2) Handle DostartTAG or DOENDTAG. These two methods are Tagsupport provided. Or use as an example, when the JSP parsing this tag, the DostartTAG event is triggered at "<", ">" triggers the DOENDTAG event. Logical operations are usually performed in DostartTag, and the output is controlled in DOENDTAG. 3) Write a TLD file. 4) Import TLD in the JSP page, your JSP page can use your TAG. Usually you will find that most of your activities are concentrated in the DostartTAG or DoreTAG method. In this way, after familiar with some interfaces and classes, it is easy to write Taglib. As the author of JSP Design: The logic inside is slightly more complicated, but after all, there is no rocket that is so difficult.

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

1.0 1.2 Diego out diegoyun.outputtag EMPTY name false FALSE

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 ...... is Body-Content between start and closing tags. Since the body-content is not processed, it is set to the name: attribute name in EMPTY . For example, Value in <% @ Taglib URI = "/ Web-inf / TLDS / DIEGO.TLD" prefix = "Diego"%> test Tag: My programming environment is Eclipse Tomcat. Start the server, if everything follows the steps above, you can see Test Tag: Hello! Diegoyun Word

The easiest tag is coming out. It's not difficult, is it?

转载请注明原文地址:https://www.9cbs.com/read-95576.html

New Post(0)