Create your first custom JSP TAG (JSP tag)

xiaoxiao2021-03-06  106

What is JSP TAG pull? If you know HTML, XML, you should know tag. Any TAG-based language (such as HTML) must be between '<' and '>'. is a title TAG These HTML tags (tags) are usually used for client browsers, which are the format of uniform display data. Similarly, we also use '<' and '>', using them, can do in the server Anything you think. In a subtle difference in JSP and HTML, all JSP tags must follow the syntax of the XML tag, that is, all start tags in JSP (EG ) There must be an ended tag (EG ). In addition, all JSP tags have a prefix, EG 'Star' in tag. Of course, like HTML and XML. , JSP tags also have attributes eg There are two attributes and have been given two values. So, the ready-made label is not used, why should it be self Definition pull-defined benefits: 1. JSP TAG allows Java (server) code and HTML (client) code to separate, this is very important when you are developing a large project, separate the server and client 2.Tags is easy to reuse Java code. 3. You can use a very useful custom Tag library to use it to end customers. 4. Tags is easy to maintain. Oh, you are trying to customize JSP tags. Is it a very easy thing. He is a very easy thing. Just like writing a normal Java class, all we have to do is writing a Java class, then directly execute an interfaces, ---- this is We have to do in this article or expand into a predefined Java class. If you need to overload their methods. So, you will have to write a text tag library descriptor (.tld) Document, make you packaged TAG can be used. Look at our firsttag.java file. Create a new Java program and save in / web-inf / class / com / stardeveloper / tag / test / file ,code show as below:

Package com.stardeveloper.tag.test;

Import java.io. *;

Import javax.servlet.jsp. *;

Import javax.servlet.jsp.tagext. *;

Public class firstttag imports tag, serializable {

Private pageContext PC = NULL;

PRIVATE TAG PARENT = NULL;

Private string name = NULL;

Public void setPageContext (PageContext P) {

PC = P;

}

Public void setparent (tag t) {

Parent = T;

}

Public tag getparent () {

Return Parent;

}

Public void setname (string s) {

Name = s;

}

Public string getname () {

Return Name;

}

Public int desartTartTag () throws jspexception {

Try {

IF (name! = null) {

Pc.getout (). Write ("Hello" Name "!");

} else {

Pc.getOut (). Write ("You Didn't Enter your name"); pc.getout (). Write (", What are you Afraid of?");

}

} catch (ioexception e) {

Throw new jsptagexception ("an ioexception iocurred.");

}

Return Skip_body;

}

PUBLIC INT DOENDTAG () THROWS JSPEXCEPTION {

Return Eval_page;

}

Public void release () {

PC = NULL;

Parent = NULL;

Name = NULL;

}

} Note: Package com.stardeveloper.tag.test; indicates that the firstttag class is packaged and placed in com.stardeveloper.tag.test. 2. Introducing three packages (we use the methods and classes in them) Import java.io . *;

Import javax.servlet.jsp. *;

Import javax.servlet.jsp.tagext. *; Note these two sense interfaces Serializable and Tag, which is not necessary for creating a JSP tags class, and tag is the most important

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

New Post(0)