Custom tag-2

Source: Internet
Author: User
Tags tld

 

1. Use of simple labels

1) use custom tags to control whether the page content (TAG body) is output

 

Public void doTag () throws JspException, IOException {

 

// JspFragment jf = this. getJspBody ();

 

// Jf. invoke (null );

 

// Equivalent to jf. invoke (this. getJspContext (). getOut ());

 

}

 

 

 

2) The simple tag controls whether the jsp content after the tag is executed

Public void doTag () throws JspException, IOException {

JspFragment jf = this. getJspBody ();

Jf. invoke (null );

 

Throw new SkipPageException ();

}

 

3) custom tags implement loop output of content (TAG body)

Public void doTag () throws JspException, IOException {

JspFragment jf = this. getJspBody ();

 

For (int I = 0; I <5; I ++ ){

Jf. invoke (null );

}

}

 

4) custom tag modification content (TAG body) -- case-sensitive Conversion

Public void doTag () throws JspException, IOException {

JspFragment jf = this. getJspBody ();

 

// To get the content in JspFragment, enter it in a buffer-containing Writer, // obtain the string

StringWriter sw = new StringWriter ();

Jf. invoke (sw );

 

String content = sw. toString (). toUpperCase ();

 

JspWriter out = this. getJspContext (). getOut ();

Out. write (content );

}

 

 

2. Custom tags with attributes

1) control the number of times that the TAG body loops are output

Add attribute variables and their setter methods to the tag processing class

Private int times;

Public void doTag () throws JspException, IOException {

JspFragment jf = this. getJspBody ();

 

For (int I = 0; I <times; I ++ ){

Jf. invoke (null );

}

}

Public void setTimes (int times ){

This. times = times;

}

Add the attribute description <attribute> </attribute> to the tld file.

...

<Attribute>

<Name> times </name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

</Attribute>

...

 

Demo <trexprvalue> true </rtexprvalue>

<Class3g: mySimpleTag5 times = "<% = 3 + 1%>">

Aaaaaaaaa <br>

</Class3g: mySimpleTag5>

 

2) explains the type conversion problem when passing parameters, and uses the transmission of Data to demonstrate parameter transfer of non-Basic Data Types

L add Data type members and their setter methods to the tag processing class

Private Date date;

Public void setDate (Date date ){

This. date = date;

}

L add attribute description to tld

<Attribute>

<Name> date </name>

<Required> true </required>

<Rtexprvalue> true </rtexprvalue>

<Type> java. util. Date </type>

</Attribute>

L call in jsp

<Class3g: mySimpleTag5 times = "3" date = "<% = new Date () %>">

Xxxxxxxx

</Class3g: mySimpleTag5>

 

From the path of the mouse programmer

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.