Jstl Getting Started: expression language

Source: Internet
Author: User
Tags coding standards

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.

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.