This article for everyone to share the JSP custom label Introductory Learning course, I hope you like.
1.JSP Custom Label:
Custom labels are user-defined JSP language elements. When a JSP page contains a custom label, it is converted to a servlet, which is converted to an operation on an object called the tag handler, that is, when the servlet executes, the Web container invokes those operations. The JSP tag extension allows you to create new tags and can be inserted directly into a JSP page. The JSP 2.0 specification introduces simple tag handlers to write these custom tags. You can inherit the Simpletagsupport class and rewrite the Dotag () method to develop one of the simplest custom tags.
2. Develop Custom tags
The following steps establish a custom label for the IP address of the tactical client:
Write an ordinary Java class, inherit the Simpletagsupport class,
The public class Showip extends Simpletagsupport {
/**
* The following masked code has been done in the Simpletagsupport code! There's no need to do it again!
* *
/*private jspcontext context;
*//**
* Incoming PageContext
*//*
@Override public
void Setjspcontext (Jspcontext pc) {
This.context = PC;
} */
@Override public
void Dotag () throws Jspexception, IOException {
PageContext pagecontext= ( PageContext) This.getjspcontext ();
ServletRequest request = Pagecontext.getrequest ();
String ip=request.getremotehost ();
JspWriter out = Pagecontext.getout ();
Out.write ("Use custom label to display customer IP address" +ip);
List<string> a=null
}
}
Create the Mytaglib.tld file in the Web-inf directory of the Web project, which is called the tag library's declaration file. (Reference core Tag library's TLD file)
<?xml version= "1.0" encoding= "UTF-8"?> <taglib "xmlns="
xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd "
version=" 2.1 ">
<description>a Tag Library exercising Simpletag handlers.</description>
<!--tag library version-->
<tlib-version>1.0 </tlib-version>
<!--tag library prefix-->
<short-name>rlovep</short-name>
<!-- A unique tag for a TLD file-->
<uri>http://rlovep.com</uri>
<!--define the label to be placed in front of the method-->
<tag >
<!--tag name-->
<name>showIp</name>
<!--label Processing class-->
<tag-class> com.rlovep.tags.showip</tag-class>
<body-content>empty</body-content>
</tag>
<tag>
</taglib>
Import a custom tag library in the header of a JSP page: URL for the URL you wrote in the TLD, and the prefix you defined in the TLD file
<%@ taglib uri= "http://rlovep.com" prefix= "Rlovep"%>
Using custom labels in your JSP
<%--Test Simple Custom label, label body (I am You) do not show--%>
<rlovep:showIp> I am you
</rlovep:showIp>
3. Custom Label Execution Process
When accessing: http://localhost:8080/stuJsp/Hellotags.jsp, restart Tomcat to load all files under the Web-inf directory for each Web application when the server is started!!! For example. Web.xml, TLD file!!!
The steps are as follows:
- Check the taglib directive for the JSP file, and whether there is a TLD file with a URL of http://rlovep.com. If not, the error occurs.
- Execute JSP file transformation: Translate JSP file into Java source file-> compile Class-> construct class object-> invoke _jspservice () method
- Read to the Mytaglib.tld file to see if the query exists as a SHOWIP label
- Find the corresponding tag, then read the content, get com.rlovep.tags.ShowIp
- Constructs the Showip object and then calls the method inside the Showip: Dotag method;
4. Access Tag Body
You can include the message content in the tag like the standard tag library. If we want to include content in our customizations
The format is as follows:
<rlovep:showIp> I am you
</rlovep:showIp>
But to text display you need to modify the processing class and the TLD file:
Modify the processing class to add the following in the Dotag method:
Jspcontext jspContext2 = This.getjspcontext ();
Two methods/Method 1 for displaying the label body
call
//jspbody.invoke (null) directly;
Method 2 is output to out
//jspbody.invoke (Jspcontext2.getout ());
To modify a TLD file:
<tag>
<!--tag name-->
<name>showIp</name>
<!--label Processing class-->
< Tag-class>com.rlovep.tags.showip</tag-class>
<!--output label body content format label body can not write JSP Java code-->
< Body-content>scriptless</body-content>
</tag>
Now you can display the contents of the tag body;
<%--label Show--%>
<rlovep:showIp> I am you
</rlovep:showIp>
The content format of the output label body:
JSP: used in a traditional label. Java code that can write and execute JSP.
scriptless: The label body can not write JSP Java code
empty: must be an empty label.
tagdependent: tag body content can write JSP Java code, but will not hold
5. Label body with attributes:
You can set various properties in a custom standard, to receive attributes, and a value custom tag class must implement setter methods;
The format is as follows:
<!--test tag with attributes, label body display through class handling-->
<rlovep:attributetags name= "Peace" value= "12345
Define property steps as follows:
Write the processing class: Attributetags extends Simpletagsupport
Add two attributes:
//Declare attribute's member variable
private Integer value;
private String name;
And writes a setter method for two member properties; public
void SetValue (Integer value) public
void SetName (String name)
In tag library file TLD Note this label and attribute:
<!--tag name-->
<name>AttributeTags</name>
<!--label Processing class-->
<tag-class> Com.rlovep.tags.attributetags</tag-class>
<!--output label body content format label body can not write JSP Java code-->
< Body-content>scriptless</body-content>
<!--configuration Properties name-->
<attribute>
<name >name</name>
<!--required-->
<required>true</required>
<!--support El expression-- >
<rtexprvalue>true</rtexprvalue>
</attribute>
<!--configuration property value-->
<attribute>
<name>value</name>
<!--are required to fill-->
<required>true </required>
<!--support El expression-->
<rtexprvalue>true</rtexprvalue>
</ Attribute>
</tag>
Now we can use the tag with the attribute.
In the case of TLD configuration properties, you can configure the following properties:
6. Custom Label with child label:
Just like the Choose tag in the core tag library, we can also define nested custom tags, which we mainly talk about creating a choose tag similar to the core tag library. The steps are as follows:
The
establishes the processing class, or process the class or the same method as before. You need to introduce a getparent () method, from the name can be known to get the parent tag, is to get the parent tag class;
Create three processing class files: Choosetag,otherwisetag,whentag
Choosetag class: public class Choosetag extends simpletagsupport{//This go-time variable is not a label property, changed by when tag; Othewise acquired; private Boolean flag
; public Boolean Isflag () {return flag.} public void Setflag (Boolean flag) {This.flag = flag;} @Override public void D
Otag () throws Jspexception, IOException {//Choose label function displays the label body, as well as the parent tag of the other two tags; getjspbody (). Invoke (null);} Whentag class public class Whentag extends simpletagsupport{//Add test Property Private Boolean test; public boolean istest () {Retu
RN test; The public void Settest (Boolean test) {this.test = test;} @Override the public void Dotag () throws Jspexception, IOException
{//If the Label property is True, display the label body if (test) {getjspbody (). Invoke (null);
//Set parent tag to otherwise with Choosetag Parent=null;
if (GetParent () instanceof Choosetag) {parent= (Choosetag) getparent ();
Parent.setflag (test); //otherwisetag class: public class Otherwisetag extends Simpletagsupport {@Override public void Dotag () throws Jspexcept
Ion, IOException {Boolean test=true; Gets the test for the parent tag, by his last whenSet the IF (GetParent () instanceof Choosetag) {//Get test of the parent tag, set Choosetag parent= (Choosetag) GetParent () by his last when;
Test=parent.isflag ();
} if (!test) {getjspbody (). Invoke (null);
}
}
}
Write a TLD file: Exactly the same as other labels
<!--definition label,choose--> <tag> <!--tag name--> <name>choose</name> <!--tag processing class--> <ta g-class>com.rlovep.tags.choosetag</tag-class> <!--output label body content format label body can not write JSP Java code--> <body-content >scriptless</body-content> </tag> <!--definition label,when--> <tag> <!--tag name when--> <name& Gt When</name> <!--label processing--> <tag-class>com.rlovep.tags.whenTag</tag-class> <!-- Output label body content format label body can not write JSP Java code--> <body-content>scriptless</body-content> <!--configuration property name--> < Attribute> <name>test</name> <!--must fill in the--> <required>true</required> <!--whether to support the El table --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <!--definition label,otherwise--> &L t;tag> <!--tag name--> <name>otherwise</name> <!--label Processing class--> <tag-class>com.rlovep.tags. otherwisetag</tag-class> <!--output label bodyThe content format tag body can not write JSP Java code--> <body-content>scriptless</body-content> </tag>
Using tagged Labels: slightly different from using other labels, you need to nest
<!--test Choose-->
<rlovep:choose>
<rlovep:when test= "${10<5}" >
conditions are set to execute
when </rlovep:When>
<rlovep:otherwise>
conditions not established executive otherwise
</rlovep:otherwise>
</ Rlovep:choose>
The custom label is introduced here;
404 pages, 505 pages, error page Configuration method:
You can configure the global 404 pages, 505 pages, and error pages for your site in Web.xml, and the configuration method is as follows: Remember to set up the corresponding jump file.
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi=
"Http://www.w3.org/2001/XMLSchema-instance" "
xmlns=" Http://java.sun.com/xml/ns/javaee "
xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id version= "3.0" >
<!--Configure NULL pointer exceptions-->
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
< location>/error.jsp</location>
</error-page>
<!--Configure 505 error page-->
<error-page >
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
<!--Configure 404 error page-->
<error-page>
<error-code>404</ error-code>
<location>/common/404.html</location>
</error-page>
</ Web-app>
JSP introduction is introduced here, I hope to help you learn.