A: The national day ended, came back to work, the results of the boss said two geniuses travel, so this two days to fill their knowledge or not familiar with the previous knowledge, JSP tag is, because in the project itself encapsulated some tags, but I will only use, the real principle of the things I still do not understand, So today's time is basically to find information on the Internet to familiarize yourself with the knowledge of JSP tags.
II: The use of attribute Rtexprvalue in a TLD in a tag
The full name of Rtexprvalue is Run-time expression Value, which says on the Web whether JSP expressions can be used.
Rtexprvalue indicates whether the value in the tag can be specified directly or can be specified dynamically (using a script or an El expression);
Reruired indicates whether it is necessary;
The following is a specific usage, first of all the definitions in the TLD are as follows:
<?xml version= "1.0" encoding= "UTF-8"? <taglib xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "http// Www.w3.org/2001/XMLSchema-instance "version=" 2.0 "xsi:schemalocation=" HTTP://JAVA.SUN.COM/XML/NS/J2EE/http Java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "> <description><! [cdata[Custom Label]] ></description> <display-name>my tags</display-name> <tlib-version>0.1</ tlib-version> <short-name>my</short-name> <uri>/mytag</uri> <tag><!--Here are some of the things I We do not want to let the parsing engine parse execution, but as the original content processing, at this time we can put the content of this part of XML in the content of the CDATA area, the XML parser will not process, but the output intact, This is what we want to display when the JSP page puts the mouse over that property when assigning a value to the property.-<description><! [cdata[Display personal name]]></description>
<!--label name--<name>wh</name> <tag-class>org.wh.gsh.web.tag.persontag</tag-class& Gt <body-content>JSP</body-content> <attribute> <description><! [cdata[Display personal name]]></description>
<!--property name--<name>personbh</name>
< whether!--is a required property--<required>true</required>
<!--see above--<rtexprvalue>true</rtexprvalue> </attribute> <attribute> <description><! [cdata[Department]]</description> <name>departmen</name> <required>false</required > <rtexprvalue>false</rtexprvalue> </attribute></tag>
There are 3 types of content that can be set by the <body-content> element.
<body-content>
The use of JSP pages is as follows: The main function of the element is to specify the type of the label body, the possible values are as follows 4 kinds.
(1) Empty: No label body
(2) JSP: The tag body can contain text, EL, expression, or JSP tags, but is not valid for simple tags.
(3) Scriptless: The tag body can contain text, El expressions, JSP tags, but not the scripting elements of the JSP.
(4) Tagdependent: The label body is referred to the label itself to resolve the processing. If you specify Tagdependent, all the code in the tag body is left intact to the label processor instead of passing the execution result to the label processor.
The above-mentioned label body is such a meaning: if the label is written like this <my:wh/>----> means there is no label body, if written so <my:wh></my:wh> there is a label body
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><%@ taglib prefix=" My "uri="/mytag "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >true</ Rtexpvalue> value is True)--><!--<my:wh personbh= "${p.bh}" department= ' "${p.depid}" ></my:wh>----></body>
Third: The effect and significance of the label return value:
In the TagSupport class, there are 4 methods that are most important: doStartTag (), Doendtag (), Doaftertag (), Release ()
(1) doStartTag (): This method starts the execution of the label, as in the following two return values:
Skip_body: Indicates that the contents of the tag body are ignored, and the execution is given to the Doendtag () method,
Eval_body_include: Represents the content that executes the label body.
(2) Doaftertag (): This method is the difference between the Iterationtag interface and the tag interface, which is used to repeat the contents of the tag body, like the next two return values
Skip_body: Indicates that the contents of the tag body are ignored and the execution is given to the Doendtag () method;
Eval_body_again: Indicates repeated execution of the label body content, repeated calls to the Doafterbody () method, loop execution continues until the Doafterbody () method but skip_body.
(3) Doendtag (): This method executes at the end of the label, like the next two return values
Skip_page: The JSP page should stop executing immediately (that is, do not execute the JSP code below) and return all output to the browser immediately.
Eval_page: Indicates that JSP is running normally
Four: The difference between TagSupport and Bodytagsupport
The main difference between TagSupport and Bodytagsupport is whether the label processing class needs to interact with the tag body, or use the TagSupport if the interaction is not required, or use Bodytagsupport if the interaction is required.
The meaning of the interaction may be understood as whether the label processing class reads the contents of the tag body and changes the content returned by the tag body. The point is, if you want to change or manipulate the contents of the tag body, you must implement the Bodytagsupport class. (There can be content in the tag, but it is necessary to implement the Bodytagsupport class to change the content or make other processing);
Let's talk about the new method Bodytagsupport.
public static final int eval_body_buffered: Indicates that the contents of the label body should be two, all the processing should be saved to the Bodycontent class;
Protected bodycontent bodycontent: Storage processing Results
Public JspWriter getpreviousout (): Gets the output stream object of JspWriter
For example, the following code is to display the part of the tag in the body of the interception, the code is as follows
Mytag.java PackageOrg.wh.demo.Tag;Importjavax.servlet.jsp.JspException;Importjavax.servlet.jsp.tagext.BodyContent;ImportJavax.servlet.jsp.tagext.BodyTag;ImportJavax.servlet.jsp.tagext.BodyTagSupport;ImportJavax.servlet.jsp.tagext.Tag; Public classMyTagextendsBodytagsupport {/** * */ Private Static Final LongSerialversionuid = 1L; Public intdoStartTag ()throwsjspexception{returnbodytag.eval_body_buffered; } Public intDoendtag ()throwsjspexception{bodycontent BC= This. Getbodycontent ();//Gets the contents of the label body String CT=bc.getstring ();//Convert to String RT=ct.substring (5,10); Try{ This. Pagecontext.getout (). write (RT); }Catch(Exception e) {Throw NewRuntimeException (e); } returnTag.eval_page; } }
Mytag.tld<?XML version= "1.0" encoding= "UTF-8"?><taglibxmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"version= "2.0"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <Description>Testing of labels</Description> <Display-name>My tags</Display-name> <tlib-version>0.0</tlib-version> <Short-name>My</Short-name> <URI>/mytags</URI> <Tag> <Description><! [Cdata[label of your own definition]]></Description> <name>Wh</name> <Tag-class>Org.wh.demo.Tag.MyTag</Tag-class> <body-content>Scriptless</body-content><!--here with JSP or scriptless, specifically in the last year has been explained-- </Tag></taglib>
HELLOTAG.JSP:
<%@ page language= "java" contenttype= "text/html; CHARSET=GBK " pageencoding=" GBk "%> <% @taglib uri="/mytags "prefix=" my "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
The results are as follows:
I now just know how to use, I used to just look not to do, and now I look at the side of practice, even the simplest things, but still very little more. Now make up for the weak ground. It's better to do it yourself than to see it hundreds of times. I still have to take the time to learn the source, the most taboo on the Java and Java Web and Linux again, a lot of things to learn, these days no matter to summarize the two months of internship learned things and complementary knowledge points. It's a long way to go. Efforts, see the novel today saw a word "bastard and for the best teacher, a word two for the World Law", this spirit, did not say Ah, received a text message to hand in a 3000-word paper, Pit Daddy Ah, I also have to find papers to make up an article. Try it, boys!!!
Java Web label