Java EE Custom Tags: implementing label scopes

Source: Internet
Author: User

Well, we always like to customize some nouns, where "label scope" means that the scope of a variable is only within the tag, and the value is not obtained outside the label.

The effect of labeling requirements is as follows:

JSP page:

... <cms:category var= "Cate" ...> column name: ${cate.name} column Article list:<c:foreach var= "item" items= "${ Cate.articlelist} ">${item.name}<br/></c:forEach></cms:category>...

HTML pages that are printed after compilation:

... Column Name: News column column article list: News news Two news three ...
<cms:category> is a custom tag whose content is to get database data and then upload it to a JSP page
There are several ways to transfer from custom tags to JSP pages
Note: The JSP has 4 scopes, the smallest of which is page
And here I want the effect to be a bit similar to returning a "local variable"
This variable is only available in the tag (with a value) and is unavailable outside the label, with the following effect:
<cms:category var= "Cate" >
${cate} ==>> has value
</cms:category>
${cate} ==>> No value

At first I thought of this effect similar to the JSTL standard library <c:forEach> tag iteration values are only valid within the tag, and are similar to "local variables" from the JSP perspective
So I created a new Java-web test project, but did not find the corresponding method to set the "local variables" (here also silly spent a lot of time to check information, debugging, Combat)

So I decided to check the <c:forEach> tag source code, found Looptagsupport class will go to implement a trycatchfinally interface
In fact, the principle is very simple, from the interface name can be seen from a little bit of mystery it (here, the heart monologue is "CNMMD, the original is so")
In fact, there is no way to set "local variables"
You can use the Pagecontext.setattribute () method to return a value in a label, but the return value is the page scope
After implementing the Trycatchfinally interface there will be a dofinally method that will be called after Doendtag ()
Then the <c:forEach> tag is the Dofinally method that uses Pagecontext.removeattribute () to destroy the return value.

Well, the smart little buddy here should know how it's done.
Well, if there is a wrong place, please correct me, thank you for checking.











Java EE Custom Tags: implementing label scopes

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.