Custom tags for getting started with JSP

Source: Internet
Author: User

Custom tags for getting started with JSP

The second part is a brief explanation: It mainly describes el expressions and core tag libraries. This chapter describes how to configure custom tag libraries, 404 pages, 505 pages, and error pages.


Download all code: Link

1. JSP custom tag:

Custom tags are custom JSP language elements. When a JSP page contains a custom tag, it is converted into a servlet, And the tag is converted into an operation on the object called tag handler, that is, the Web iner calls those operations when the servlet executes. JSP tag extension allows you to create new tags and insert them directly to a JSP page. The Simple Tag Handlers is introduced in the JSP 2.0 specification to write these custom tags. You can inherit the SimpleTagSupport class and override the doTag () method to develop a simple custom tag.

2. develop custom tags

Follow these steps to create a custom tag for the IP address of the tactical client:
1. Write a common java class that inherits the SimpleTagSupport class,

Public class ShowIp extends SimpleTagSupport {/*** the following shielded code has been done in SimpleTagSupport code! You do not need to repeat it here! * // * Private JspContext context; * // *** input 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 ("display customer ip addresses using custom tags" + ip addresses); List
  
   
A = null ;}}
  
Create the mytaglib. tld file in the WEB-INF directory of the web project, which is called the Declaration file of the tag library. (Refer to the tld file of the core tag Library)

  
      
   
    
A tag library exercising SimpleTag handlers.
       
       
   
    
1.0
       
       
   
    
Rlovep
        
      
   
    
Http://rlovep.com
      
      
      
       
    
     
ShowIp
       
       
    
     
Com. rlovep. tags. ShowIp
       
    
     
Empty
       
      
   
  

Import the custom tag library in the header of the jsp page: the url you write in the tld, And the prefix is also defined in the tld file.

          <%@ taglib uri="http://rlovep.com" prefix="rlovep" %>
Use custom tags in jsp
<% -- Test a simple custom tag. The TAG body (I am you) is not displayed. -- %>
  
   
I am you
  
3. Execution Process of custom tags

When accessing: http: // localhost: 8080/stuJsp/Hellotags. jsp; to restart Tomcat so that the server is started, load all the files under the WEB-INF directory of each web application !!! For example. Web. xml, tld file !!!
The procedure is as follows:
1. Check the taglib command of the jsp file to see if there is a tld file with a url of http://rlovep.com. If no, an error is returned.
2. Execute jsp file conversion: translate jsp file into java source file-> compile class-> construct class Object-> call _ jspService () method
3. Read the mytaglib. tld file to check whether there are tags with showIp.
4. Find the corresponding tag, read the content, and get com. rlovep. tags. ShowIp.
5. Construct the ShowIp object, and then call the method in ShowIp: dotag method;

4. Access TAG body

You can include the message content in a tag like a standard tag library. For example, we want to include content in our custom
1. The format is as follows:


  
   
I am you
  
However, to display text, you need to modify the processing class and tld file:
Add the following content to the doTag method:
JspContext jspContext2 = this. getJspContext (); // two methods for displaying the label body // method 1 is called directly // jspBody. invoke (null); // method 2 is output to out // jspBody. invoke (jspContext2.getOut ());

Modify the tld file:

 
     
      
   
    
ShowIp
      
      
   
    
Com. rlovep. tags. ShowIp
      
      
   
    
Scriptless
      
  
Now you can display the content of the label body;
<% -- The tag display -- %>
  
   
I am you
  
Content format of the output TAG body:
JSP: Used in Traditional labels. Java code of jsp can be written and executed.
Scriptless: the TAG body cannot write jsp java code.
Empty: the label must be empty.
Tagdependent: the TAG body content can be written into jsp java code, but does not hold it. 5. Add attributes to the TAG body:

You can set various attributes in the Custom standard. To receive attributes, the custom value tag class must implement the setter method;
1. The format is as follows:


  <Rlovep: attributetags name = "peace" value = "12345 </code> </pre>: <br> write a processing class: attributeTags extends SimpleTagSupport <pre class = "brush: java;" = "">Add two attributes: // declare the member variable private Integer value; private String name; and write the setter method for the two member attributes; public void setValue (Integer value) public void setName (String name)

Specify the tag and attributes in the tag library file tld:

   AttributeTags    Com. rlovep. tags. AttributeTags    Scriptless    Name    True    True    Value    True    True  
Now you can use a tag with attributes. You can configure the following attributes When configuring attributes in tld:
6. Custom tags with sub-tags:

Just like the choose tag of the core tag library, we can also define nested custom tags. This section mainly describes how to create a choose tag similar to the core tag library. The procedure is as follows:
1. Create a processing class. The processing class is the same as the previous method. The getParent () method is used to obtain the parent tag;
Create three processing files: ChooseTag, OtherWiseTag, and whenTag

// ChooseTag class: public class ChooseTag extends SimpleTagSupport {// when this variable is not a tag attribute, it is changed by the when tag; obtained by othewise; private boolean flag; public boolean isFlag () {return flag;} public void setFlag (boolean flag) {this. flag = flag;} @ Override public void doTag () throws JspException, IOException {// The Choose label displays the label body and serves as the parent label of the other two labels; getJspBody (). invoke (null) ;}// whenTag class public class whenTag extends SimpleTagSupport {// Add the test attribute private boolean test; public boolean isTest () {return test;} public void setTest (boolean test) {this. test = test ;}@ Override public void doTag () throws JspException, IOException {// if the tag attribute is true, display the TAG body if (test) {getJspBody (). invoke (null);} // set the parent tag to otherwise and use ChooseTag parent = null; if (getParent () instanceof ChooseTag) {parent = (ChooseTag) getParent (); parent. setFlag (test) ;}}// Other WiseTag class: public class OtherWiseTag extends SimpleTagSupport {@ Override public void doTag () throws JspException, IOException {boolean test = true; // obtain the test of the parent tag, set if (getParent () instanceof ChooseTag) by his previous when {// obtain the test of the parent tag, set ChooseTag parent = (ChooseTag) by his previous when) getParent (); test = parent. isFlag ();} if (! Test) {getJspBody (). invoke (null );}}}
Write a tld file: same as other tag Definitions
     Choose    Com. rlovep. tags. ChooseTag    Scriptless       When    Com. rlovep. tags. whenTag    Scriptless    Test    True    True       Otherwise    Com. rlovep. tags. OtherWiseTag    Scriptless  
Tags using tape tags: they are slightly different from other tags and need to be nested.
    When the condition is true   If the condition is not true, execute otherwise.  

Here is an introduction to custom tags;

404 page, 505 page, error page configuration method:

You can configure the global 404 page, 505 page, and error page for your website in web. xml. The configuration method is as follows: Remember to create the corresponding jump file.

      Java. lang. NullPointerException   /Error. jsp      500   /Common/500.jsp      404   /Common/404.html   

Okay. Here is the introduction in this chapter.
This is the introduction to JSP;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.