JSP standard tag Library

Source: Internet
Author: User
Tags tld

The standard tag Library (jstl) is a set of custom tags that implement many common web site functions in standardized formats. In the JSP Best Practices Section, Brett McLaughlin will introduce you to jstl and explain how to change JSP pages to use highly functional tags.
One of the advantages of JSP technology is its custom tag library tool. In addition to core JSP tags, such as JSP: include, the custom tag library tool allows you to create unique tags for site-or application-oriented operations. Once you have created a custom tag Library (taglib for short), you can reuse tags when you expand a project or move to another project. You can also pass taglib to other developers so that they can use it on their own websites or in other parts of your web application. In the next part of this series, I will show you how to create your own custom tag library, but now we will focus on JSP standard tag Library (jstl ).

Jstl is actually a set of standardized custom tag libraries for JSP 1.2. Many jstl tags are similar to core JSP tags, but they provide more features. For example, the C: Import mark is very similar to the include and JSP: include commands described in the previous section (see references ). Like the include command, C: import can help you read (populate) the pages of your web site or web application, but its functional expansion is beyond the scope of the core JSP tag.

In the JSP Best Practices Section, we will use the C: import tag to learn how to convert a web page from JSP to jstl. However, before starting to use the new tag, We need to load jstl into the Web container.

Jstl settings
Although the jstl plan is bound to the next wave of Web containers, you must set it by yourself. To load jstl to a Web Container, follow these steps:

Download jakarta-taglibs-standard-current.tar.gz from apache.org.

Decompress the downloaded file.

Copy Jakarta-taglibs/standard-1.0.3/TLD/C. TLD to the Web/INF/TLDs directory.

Copy all jar files in Jakarta-taglibs/standard-1.0.3/lib to the WEB-INF/lib directory.

Add the following entries to the WEB-INF/Web. xml deployment descriptor.
<Taglib>
<Taglib-Uri> http://java.sun.com/jstl/core </taglib-Uri>
<Taglib-location>/WEB-INF/TLDs/C. TLD </taglib-location>
</Taglib>

 

 

Restart the Web container.
This is a quick version set by jstl, which can meet the requirements in most cases. For more information, see references.

Joint jstl mark
If you want to use any jstl tag on the JSP page, you must first declare the tag library prefix and URL on the page. We are using the core jstl library, so the standard prefix is C (representing the core ). Table 1 shows the web site index file with the taglib command added:

Table 1: taglib commands of the index file

<% @ Page Language = "Java" contenttype = "text/html" %>
<% @ Taglib prefix = "C" uri = "http://java.sun.com/jstl/core" %>
<HTML>
<Head>
<Title> newinstance.com </title>
<Meta http-equiv = "Content-Type" content = "text/html;
Charset = iso-8859-1 "/>
<Link href = "/styles/default.css" rel = "stylesheet" type = "text/CSS"/>
</Head>

<Body>

<JSP: Include page = "header. jsp" Flush = "true">
<JSP: Param name = "pagetitle" value = "newinstance.com"/>
<JSP: Param name = "pageslogan" value = ""/>
</Jsp: Include>
<% @ Include file = "/navigation. jsp" %>
<JSP: Include page = "bookshelf. jsp" Flush = "true"/>

<JSP: Include page = "/MT-blogs/index. jsp" Flush = "true"/>

<% @ Include file = "/footer. jsp" %>
</Body>
</Html>

 

After jstl taglib is added to the JSP page, you can use any tag in the core library. You only need to change the prefix of the tag to C. To understand this process, we will try to convert the page from JSP: Include tag to C: import tag. Table 2 shows how to convert the JSP: Include instance (see references) introduced in the second part of this series to the new C: import tag.

Table 2: Convert to jstl

<% @ Page Language = "Java" contenttype = "text/html" %>
<% @ Taglib prefix = "C" uri = "http://java.sun.com/jstl/core" %>
<HTML>
<Head>
<Title> newinstance.com </title>
<Meta http-equiv = "Content-Type" content = "text/html;
Charset = iso-8859-1 "/>
<Link href = "/styles/default.css" rel = "stylesheet" type = "text/CSS"/>
</Head>

<Body>

<JSP: Include page = "header. jsp" Flush = "true">
<JSP: Param name = "pagetitle" value = "newinstance.com"/>
<JSP: Param name = "pageslogan" value = ""/>
</Jsp: Include>
<% @ Include file = "/navigation. jsp" %>
<C: Import url = "bookshelf. jsp"/>

<C: Import url = "/MT-blogs/index. jsp"/>

<% @ Include file = "/footer. jsp" %>
</Body>
</Html>

 

The only difference between this program and the previous JSP program is that we use the URL attribute in C instead of the page attribute in JSP: include. It's very simple, isn't it? For an entertaining exercise, try to switch to a page by yourself. Just run "use JSP: Include to manage dynamic content" (for the second part of this series, see references) and convert the JSP code to jstl, as shown in table 2.

If you have studied it carefully, you may notice that I should not change the header transfer sion program code in table 2. The header program code contains parameters, which increases the complexity discussed here. In the next section of JSP best practices, you will learn how to use C: import to pass parameters. I will also introduce you to some features of the C: import tag, which provides more features than the simple JSP: Include tag. At that time, you can use the simple jstl Conversion Technology learned here!

Http://www.uml.org.cn/j2ee/j2eeshy.htm
 

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.