Jstl Label Tutorial

Source: Internet
Author: User
Tags coding standards expression include variables reference

The JSP standard Tag library (JSP Standard tag Library,jstl) is a set of custom tag libraries that implement common functions commonly found in WEB applications, including iterative and conditional judgment, data management formatting, XML operations, and database access. In the first article on DeveloperWorks's new series, software engineer Mark Kolb showed you how to use the JSTL tag to avoid scripting elements in JSP pages. You will also learn how to simplify software maintenance by removing source code from the presentation layer. Finally, you'll learn about JSTL's simplified expression language, which allows you to specify dynamic property values for a JSTL operation without having to use a full-featured programming language.

JavaServer Pages (JSP) is the standard presentation layer technology for Java EE platform. JSP technology provides scripting elements and operations for performing calculations that dynamically generate page content. Scripting elements allow you to include program source code in a JSP page that can be executed when the page is rendered in response to a user request. Operations encapsulate calculation operations in markup that is much like an HTML or XML tag, and the template text for JSP pages typically contains these tags. The JSP specification defines only a few operations as standards, but starting with JSP 1.1, developers have been able to create their own operations in the form of custom tag libraries.

The JSP standard Tag library (JSTL) is a set of JSP 1.2 custom tag libraries that implement the basic functionality commonly used by server-side Java applications. By providing a standard implementation for typical presentation-layer tasks such as data formatting and iteration or conditional content, JSTL enables JSP authors to focus on application-specific development requirements rather than "reinvent the stick" for these generic operations.

Of course, you can use JSP scripting elements (scriptlet, expressions, and declarations) to implement such tasks. For example, you can use three scriptlet to implement conditional content, and the three scriptlet are highlighted in Listing 1. However, because scripting elements rely on embedding program source code (usually Java code) in a page, the complexity of the software maintenance task is greatly increased for JSP pages that use these scripting elements. For example, the scriptlet example in Listing 1 strictly relies on proper matching of curly braces. If you inadvertently introduce a syntax error, nested other scriptlet in the conditional content can cause serious damage, and it can be difficult to make the error message meaningful when the JSP container compiles the page.

Listing 1. Implement conditional content through Scriptlet

<% if (user.getRole() == "member")) { %>
   <p>Welcome, member!</p>
<% } else { %>
   <p>Welcome, guest!</p>
<% } %>

Fixing this type of problem often requires considerable programming experience. Although the JSP is usually developed and maintained by designers who are very proficient in page layout and graphic design, the scripting elements on the same page have problems that require programmer intervention. This situation shares the responsibility of the code in a single file to multiple people, making it a cumbersome task to develop, debug, and enhance such JSP pages. By wrapping common functions into a standard collection of custom tag libraries, JSTL enables JSP authors to reduce the need for scripting elements, even without them, and to avoid associated maintenance costs.

JSTL 1.0

JSTL 1.0, published in June 2002, consists of four custom tag libraries (core, format, XML, and SQL) and a pair of universal Tag Library validators (SCRIPTFREETLV and PERMITTEDTAGLIBSTLV). The core tag library provides custom operations to manage data by restricting scoped variables, and to perform iterative and conditional operations on page content. It also provides tags for generating and manipulating URLs. As the name implies, the format tag library defines the operations that are used to format data, especially numbers and dates. It also supports the internationalization of JSP pages using a localized resource bundle. The XML library contains tags that manipulate the data represented through XML, and the SQL Library defines the operations that are used to query the relational database.

The two JSTL Tag Library Validator allows developers to enforce coding standards in their JSP applications. You can configure the SCRIPTFREETLV validator to disable various types of JSP script element ―scriptlet, expressions, and declarations in the JSP page. Similarly, the PERMITTEDTAGLIBSTLV validator can be used to restrict the set of custom tag libraries (including JSTL tag libraries) that may be accessed by the application's JSP pages.

Although JSTL will eventually become a required component of the Java EE platform, only a handful of application servers currently include it. The JSTL 1.0 reference implementation can be obtained as part of the Jakarta Taglibs project (see Resources) of the Apache Software Foundation (Apache Software Foundation). The custom tag libraries in this reference implementation can be merged into any server that supports the JSP 1.2 and Servlet 2.3 specification to add support for JSTL.

Expression language

In JSP 1.2, you can use a static string or an expression (if allowed) to specify the properties of the JSP operation. For example, in Listing 2, a static value is specified for the name and property properties of a <jsp:setProperty> operation, and its Value property is specified with an expression. The effect of this action is to assign the current value of the request parameter to the named Bean attribute. An expression used in this form is called the request-time property value (Request-time attribute value), which is the only mechanism built into the JSP specification to dynamically specify the property value.

Listing 2. JSP action for property values when merging requests

<jsp:setProperty name="user" property="timezonePref"
         value='<%= request.getParameter("timezone") %>'/>

Because property values are specified with expressions at request time, they often have the same software maintenance problems as other scripting elements. Therefore, the JSTL custom tag supports another mechanism for specifying dynamic property values. You can use a simplified expression language (EL) instead of a complete JSP expression to specify the property value of the JSTL operation. EL provides identifiers, accessors, and operators for retrieving and manipulating data residing in a JSP container. EL is based to some extent on EcmaScript (see Resources) and XML Path Language (XML Paths Language,xpath), so page designers and programmers should be familiar with its syntax. EL is good at finding objects and their attributes, and then performing simple operations on them; it's not a programming language, not even a scripting language. However, when used with the JSTL tag, it can use simple and convenient symbols to represent complex behavior. The EL expression is formatted in such a way that the dollar sign ($) is bounded and the content is included in the curly braces ({}), as shown in Listing 3.

Listing 3. Describes the JSTL operation of the EL expression delimiter

<c:out value="${user.firstName}"/>

In addition, you can combine multiple expressions with static text to construct dynamic property values through string literals, as shown in Listing 4. An individual expression consists of identifiers, accessors, literals, and operators. Identifiers are used to refer to data objects stored in the datacenter. El has 11 reserved identifiers, corresponding to 11 EL implicit objects. Assume that all other identifiers refer to variables that limit the scope. An element that an accessor uses to retrieve an attribute or collection of an object. Text represents a fixed value--a number, character, String, Boolean, or null value. Operators allow you to combine and compare data and text.

Listing 4. Combine static text and multiple EL expressions to specify dynamic property values

<c:out value="Hello ${user.firstName} ${user.lastName}"/>

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.