Jstl Simplified JSP coding

Source: Internet
Author: User
Tags foreach array exception handling expression logical operators variables reference tagname
js| Code

With the popularity of Java-ee thin client technology JavaServer Pages (JSP) over the past few years, independent developers have created many custom JSP tag libraries. Although many tag libraries are written to achieve different goals, they tend to provide similar solutions for iterations, conditions, and other common operations.

To reduce the need for independent tag libraries that address similar generic issues, the Jstl (JavaServer Pages Standard tag library,jsp) standard Tag Library was created under the auspices of the Java Community Process (JSR 52), Provides a single standard solution to address these common features.

JSTL Library

JSTL specifically provides support for conditional handling, iterations, internationalization, database access, and Extensible Markup Language (XML) processing. Jstl also introduces the expression language (EL, expression language), which greatly simplifies the access and operation of the data applied to the JSP. JSTL includes 4 JSP 1.2 custom tag libraries, each covering a specific functional area.

Core tag Libraries provide common support for day-to-day tasks, such as displaying and setting variables, reusing a set of projects, testing conditions, and other actions such as importing and redirecting Web content.

XML tag libraries provide support for XML processing and operations, including parsing of XML nodes, iterative, conditional assessment based on XML data, and the execution of Extensible Stylesheet Language Transformations (extensible Style Language transformations,xslt).

The internationalization (internationalization) tag library supports multilingual applications.

Database tag libraries provide standardized support for accessing and modifying database data.

Table 1:jstl four Tag libraries

Functional domain URI Prefix example core http://java.sun.com/jstl/corec<c:tagname ... >xml http://java.sun.com/jstl/xml x <x: TagName ...> Internationalization (internationalization) http://java.sun.com/jstl/fmt FMT <fmt:tagname ...> Databases (database) HTTP ://java.sun.com/jstl/sql SQL <sql:tagname ...>

Getting Started with Jstl

The best way to understand Jstl is to visit the Apache website--jakarta.apache.org and download Jstl's reference implementation. Detailed installation instructions can also be found at the Apache site. The downloadable reference implementation is a combination of a jar file, a document, and a simple code example.

To use Jstl in your Java EE Web application, simply copy the Jstl jar file from the "Lib" directory to your application's Web-inf/lib directory. To use the JSTL tag in a particular JSP, you must also provide a taglib instruction. For example, to import the "core" Jstl library into your page, you should include the following instructions at the top of your JSP, as follows:

<%@ taglib uri= "Http://java.sun.com/jstl/core" prefix= "C"%>

El Support for Jstl

An important advantage of JSTL is that it employs a simple expression language (EL), which provides a simple way to access and manipulate application data, such as data stored in a servlet context.

The syntax for El is simple and more user-friendly than the representation of the same functionality in Java. For example, the Pagecontext.getattribute ("aname") expression becomes ${aname in El. All Jstl tags use an el expression in their property values. An El expression uses the ${java.expression} or ${data.reference} format when accessing nested properties. A data reference can be an object and its properties or an array of objects and their properties:

${myobject.property}

Array access operators are also used for data displayed as a collection of indexed elements, such as Java arrays or java.util.List:

${mylist[2]}$

In addition to using attributes and array element operators and arithmetic, relational, and logical operators in El expressions, you can also use the special operator to test whether an object is empty.

In addition to object and array access, El also provides a complete set of commonly used operators, including = 、!、 <, >, <=, >=, + 、-、 *,/etc.

Objects in any JSP scope (page, request, session, or application) can be referenced in an El expression. For example, if you have a Java bean--employee with an attribute "ename", you can access the variable using the EL expression ${employee.ename}.

In addition to explicit variables, El also provides direct access to implicit variable requests and implicit variables in the answering object. For example, the following statement accesses a request parameter named "EmpName":

${param.empname}

The upcoming JSP 2.0 and JSTL 1.0 all use El. However, the El used in JSP 2.0 is slightly different. The JSTL Expert Group (JSR-052) has agreed to use the El JSP 2.0 version in the upcoming JSTL Maintenance edition.

Using the JSTL core tag Library

The JSTL Core Tag library provides the most commonly used markup for operations such as display, iteration, and setting variables. Below, we introduce some of the most common JSTL core tag libraries in more detail. First, before using any of the JSTL core tags, you need to add the following instructions to your JSP:

<%@ taglib uri= "Http://java.sun.com/jstl/core" prefix= "C"%>

One of the most common JSTL operations you use is to display dynamic values. To display dynamic Data, the core library provides a c:out tag. The c:out tag displays the value of an El expression in a page. For example:

Name: <c:out value= "${employee.ename}"/>

The Value property of a c:out can also contain a combination of text and an expression:

<c:out value= "Name: ${employee.ename}"/>

(Note: When JSP 2.0 provides support for El, you do not need to use the c:out operation, you can embed the JSP expression directly in the page.) )

Another action is to set the variable. To set a variable in one page, the core tag library provides C:set markup. This example shows the value of setting the variable ename to the parameter "Enameparm":

<c:set var= "ename" value= "${param.enameparm}"/>

The JSTL Core Tag library also provides tags for handling conditions. C:IF handles simple conditional testing. Computes the value of the Boolean expression in the Test property, or, if it is true, the contents of the body. In the following operation, you can also see the optional var attribute used in storing test results for later use in the page (or elsewhere, if you specify additional optional range attributes):

<c:if test= "${employee.salary <= 10000}" >it ' s time for a raise <c:out "value=" ${employee.name >! >

Below, you can see JSTL support for jump logic through C:choose, C:when, and C:otherwise. You can include a set of C:when actions in a select (choose) tag, and if the expression in the C:when block evaluates to true, the tests in the c:choose operation below are not evaluated. If none of the test values in the C:when block is true, the contents of the c:otherwise operation (if any) are computed:

<c:choose><c:when test= "${dept.name = = ' Development '}" >...</c:when><c:when test= "${dept.name = = ' marketing '} ' >...</c:when><c:otherwise>...</c:otherwise></c:choose>

The C:foreach tag provides an easy way to iterate over the collection of elements. If you only want to iterate over a subset of the elements in a collection, you can specify both the start and end indexes and the incremental values with optional start, end, and step attributes. In the following example, we iterate over the contents of a collection in a variable empnames; in each loop, the next element is placed in the variable name and evaluated in the body of the C:foreach action.

<table><c:foreach var= "name" items= "${empnames}" ><tr><td><c:out value= "${name}"/> </td></tr></c:forEach></table>

JSTL Core tag libraries can also simplify exception handling. Previously, you had to place Java Try/catch Statements in the Java scriptlet or provide them in the error page. JSTL provides a clever way to handle exceptions through the c:catch tag without using Scriptlet.

<c:catch>   <!—... Some set of nested JSTL tags that fire a exception-></c:catch>

Examples of other JSTL tag libraries such as XML, internationalization, and database tag libraries can be found in the JSTL documentation provided in the jakarta.apache.org reference implementation.




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.