In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use JSTL". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use JSTL.
The JSP Standard tag Library (JSP Standard Tag Library,JSTL) is a set of custom tag libraries that implement common functions common in Web applications, including iteration and conditional judgment, data management formatting, XML operations, and database access. In the first article in his new series on developerWorks, software engineer Mark Kolb shows you how to use JSTL tags to avoid scripting elements in JSP pages. You will also learn how to simplify software maintenance by removing the 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 JSTL operations without having to use a full-featured programming language.
JavaServer Pages (JSP) is the standard presentation layer technology for the J2EE platform. JSP technology provides scripting elements and actions for performing calculations that are used to generate page content dynamically. Scripting elements allow program source code to be included in the JSP page, which can be executed when the page is rendered in response to a user's request. Actions encapsulate calculation operations in tags that look much like HTML or XML tags, which are typically contained in the template text of a JSP page. The JSP specification defines only a few operations as standards, but since JSP 1.1, developers have been able to create their own operations in the form of custom tag libraries.
JSP Standard tag Library (JSTL) is a set of JSP 1.2 custom tag libraries that implement a large number of basic functions commonly used by server-side Java applications. By providing a standard implementation for typical presentation-layer tasks, such as data formatting and iterations or conditional content, JSTL enables JSP authors to focus on application-specific development needs, rather than "starting a fresh start" for these general operations.
Of course, you can use JSP scripting elements (scriptlet, expressions, and declarations) to accomplish such tasks. For example, you can implement conditional content with three scriptlet, which are highlighted in listing 1. However, because scripting elements rely on embedding program source code (usually Java code) in the page, the complexity of software maintenance tasks for JSP pages that use these scripting elements is greatly increased. For example, the scriptlet example in listing 1 depends strictly on the correct matching of curly braces. If a syntax error is inadvertently introduced, the nesting of other scriptlet in conditional content can cause serious damage, and it can be difficult to make the resulting error message meaningful when the JSP container compiles the page.
Listing 1. Implement conditional content through scriptlet
Welcome, member!
Welcome, guest!
Fixing such problems usually requires considerable programming experience. Although JSP is usually developed and maintained by designers who are well versed in page layout and graphic design, programmers are required to intervene when there are problems with scripting elements on the same page. This situation shares the responsibility for 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 was released in June 2002 and consists of four custom tag libraries (core, format, xml, and sql) and a pair of generic tag library validators (ScriptFreeTLV and PermittedTaglibsTLV). The core tag library provides custom actions to manage data through scoped variables, and to perform iterations and conditional actions on page content. It also provides tags for generating and manipulating URL. As the name implies, the format tag library defines the operations used to format data, especially numbers and dates. It also supports the internationalization of JSP pages using localized resource bundles. The xml library contains tags that manipulate data represented by XML, while the sql library defines the operations used to query the relational database.
Two JSTL tag library validators allow developers to enforce coding standards in their JSP applications. You can configure the ScriptFreeTLV validator to disable various types of JSP scripting elements-scriptlet, expressions, and declarations-in the JSP page. Similarly, the PermittedTaglibsTLV validator can be used to restrict the set of custom tag libraries (including the JSTL tag library) that may be accessed by the application's JSP page.
Although JSTL will eventually become a required component of the J2EE platform, only a few application servers currently include it. The reference implementation of JSTL 1.0 is available as part of the Apache Software Foundation (Apache Software Foundation) Jakarta Taglibs project (see Resources). The custom tag libraries in this reference implementation can be merged into any server that supports the JSP 1.2 and Servlet 2.3 specifications to add support for JSTL.
Expression language
In JSP 1.2, you can use static strings or expressions (if allowed) to specify the properties of the JSP operation. For example, in listing 2, static values are specified for the name and property attributes of the operation, and its value attribute is specified with an expression. The effect of this operation is to assign the current value of the request parameter to the named bean property. Expressions used in this form are called request-time attribute values (request-time attribute value), which is the only mechanism built into the JSP specification to specify attribute values dynamically.
Listing 2. JSP operation of attribute values when merging requests
Because attribute values are specified by expressions when requested, they often have the same software maintenance problems as other script elements. Therefore, the JSTL custom tag supports another mechanism for specifying dynamic attribute values. You can use a simplified expression language (EL) instead of using a full JSP expression to specify the property values of the JSTL operation. EL provides identifiers, accessors, and operators for retrieving and manipulating data that resides in an JSP container. EL is somewhat based on EcmaScript (see Resources) and the XML path language (XML Path Language,XPath), so page designers and programmers should be familiar with its syntax. EL is good at finding objects and their features and then performing simple operations on them; it is not a programming language or even a scripting language. However, when used with JSTL tags, it can use simple and convenient symbols to represent complex behavior. The format of the EL expression is as follows: delimited by the dollar sign ($), and the content is included in curly braces ({}), as shown in listing 3.
Listing 3. Describes the JSTL operation of EL expression delimiters
In addition, you can combine multiple expressions with static text to construct dynamic property values through string juxtaposition, as shown in listing 4. Individual expressions consist of identifiers, accessors, literals, and operators. Identifiers are used to reference data objects stored in the data center. EL has 11 reserved identifiers that correspond to 11 EL implicit objects. Assume that all other identifiers refer to scoped variables. Accessors are used to retrieve the properties of an object or the elements of a collection. Text represents a fixed value-numeric, character, string, Boolean, or null. Operators allow you to combine and compare data and text.
Listing 4. Combine static text and multiple EL expressions to specify dynamic attribute values
A variable that limits its scope
JSP API operates to allow data to be stored and retrieved from four different scopes within the JSP container. JSTL extends this capability by providing additional operations for specifying and removing objects in these scopes. In addition, EL provides built-in support for retrieving these objects as scoped variables. In particular, any identifier that appears in an EL expression but does not correspond to any EL implicit object is automatically assumed to refer to an object stored in one of the four JSP scopes:
Page scope
Request scope
Session scope
Application scope
You may recall that objects stored in the scope of the page can be retrieved only during the processing of the page for a particular request. If objects are stored in the request scope, they can be retrieved during the processing of all pages involved in processing a request (such as one or more operations encountered in the processing of a request). If the object is stored in session scope, it can be retrieved by any page accessed by the user during an interactive session with the Web application (that is, until the HttpSession object associated with the user is invalid). Objects stored in the application scope can be accessed by any user from any page until the Web application itself is uninstalled (usually due to closing the JSP container).
The object is stored in the scope by mapping the string to an object in the desired scope. You can then retrieve the object from the scope by providing the same string. Finds the string in the scope mapping and returns the mapped object. In Servlet API, such objects are referred to as properties of the corresponding scope. However, in the context of EL, the string associated with an attribute is also treated as the name of a variable that obtains a specific value through attribute mapping.
In EL, identifiers that are not associated with implicit objects are considered name objects stored in four JSP scopes. First check the page scope for the existence of such an identifier, then check the request scope, then the session scope, and finally the application scope, and then test whether the name of the identifier matches the name of an object stored in the scope. The first such match is returned as the value of the EL identifier. In this way, you can think of EL identifiers as references to scoped variables.
From a more technical point of view, identifiers that are not mapped to implicit objects are evaluated using the findAttribute () method of the PageContext instance, which represents the processing of the page where the expression for the request is currently being processed. The name of the identifier is passed to the method as an argument, and the method then searches the four scopes for properties with the same name. And returns the first match found as the value of the findAttribute () method. If such a property is not found in the four scopes, null is returned.
Finally, the scoped variables are four JSP-scoped attributes with names that can be used as EL identifiers. As long as scoped variables are given alphanumeric names, they can be created through any mechanism provided in JSP for setting properties. This includes built-in operations and the setAttribute () method defined by several classes in Servlet API. In addition, many of the custom tags defined in the four JSTL libraries can themselves set property values that are used as scoped variables.
Implicit object
The identifiers of 11 EL implicit objects are listed in Table 1. Don't confuse these objects with JSP implicit objects (there are only nine in total), only one of which is common to them.
Table 1. EL implicit objects
Category identifier description
The JSP pageContext PageContext instance corresponds to the processing of the current page
Scope pageScope the Map class associated with the name and value of the page scope attribute
RequestScope the Map class associated with the name and value of the request scope attribute
SessionScope the Map class associated with the name and value of the session scope attribute
ApplicationScope the Map class associated with the name and value of the application scope property
Request parameter param A Map class that stores the main values of request parameters by name
ParamValues stores all values of the request parameter as the Map class of the String array
Request header header A Map class that stores the main values of the request header by name
HeaderValues stores all the values of the request header as the Map class of the String array
Cookie cookie stores the Map class of the cookie that comes with the request by name
The initialization parameter initParam stores the Map class of the Web application context initialization parameters by name
Although there is only one common object (pageContext) in JSP and EL implicit objects, other JSP implicit objects can also be accessed through EL. The reason is that pageContext has the ability to access all eight other JSP implicit objects. In fact, this is the main reason for including it in EL implicit objects.
All remaining EL implicit objects are mappings that can be used to find objects that correspond to names. The first four mappings represent the various attribute scopes discussed earlier. You can use them to find identifiers in a specific scope without relying on the sequential lookup process that EL uses by default.
The next four mappings are used to get the values of the request parameters and request headers. Because the HTTP protocol allows request parameters and request headers to have multiple values, they each have a pair of mappings. The first mapping in each pair returns the main value of the request parameter or header, usually the value that happens to be specified first in the actual request. The second mapping in each pair allows all values of the parameter or header to be retrieved. The keys in these maps are the names of parameters or headers, but these values are an array of String objects, where each element is a single parameter value or header value.
The cookie implicit object provides access to the cookie name set by the request. This object maps all cookie names associated with the request to Cookie objects that represent those cookie attributes.
The last EL implicit object, initParam, is a mapping that stores the names and values of initialization parameters for all contexts associated with the Web application. The initialization parameters are specified through the web.xml deployment descriptor file, which is located in the application's WEB-INF directory.
Access device
Because EL identifiers are parsed as implicit objects or scoped variables (implemented by attributes), it is necessary to convert them to Java objects. EL can automatically wrap and unpack primitive types in its corresponding Java class (for example, you can cast int to an Integer class in the background, and vice versa), but most identifiers will become pointers to complete Java objects.
As a result, access to the properties of these objects or (where the objects are arrays and collections) to their elements is usually satisfactory. To achieve this purpose, EL provides two different accessors (dot operator (.) and square bracket operator ([])), and also supports manipulating features and elements through EL.
The dot operator is typically used to access the properties of an object. For example, in the expression ${user.firstName}, the dot operator is used to access the property named firstName of the object referenced by the user identifier. EL uses the Java bean convention to access object properties, so the getter method for this property (usually a method called getFirstName ()) must be defined in order for the expression to evaluate correctly. The point operator can be applied recursively when the property being accessed is itself an object. For example, if our fictional user object has an address property that is implemented as a Java object, you can also use the dot operator to access the properties of that object. For example, the expression ${user.address.city} will return the city property nested within this address object.
The square bracket operator is used to retrieve the elements of arrays and collections. In the case of arrays and ordered collections (that is, collections that implement the java.util.List interface), place the subscript of the element to be retrieved in square brackets. For example, the expression ${urls [3]} returns the fourth element of the array or collection referenced by the urls identifier (as in the Java language and JavaScript, the subscript in EL is zero-based).
For collections that implement the java.util.Map interface, the square bracket operator uses the associated key to look up the value stored in the map. Specify the key in square brackets and return the corresponding value as the value of the expression. For example, the expression ${commands ["dir"]} returns the value associated with the "dir" key in the Map referenced by the commands identifier.
In both cases, the expression is allowed to appear in square brackets. The result of evaluating a nested expression is used as a subscript or key to retrieve the appropriate elements of the collection or array. Like the dot operator, the square bracket operator can be applied recursively. This enables EL to retrieve elements from multidimensional arrays, nested collections, or any combination of both. In addition, the dot operator and the square bracket operator are interoperable. For example, if the element of an array is itself an object, you can use the square bracket operator to retrieve the element of the array and combine the dot operator to retrieve a property of the element (for example, ${urls [3] .protocol}).
Assuming that EL acts as a simplified language for specifying dynamic attribute values, EL accessors have an interesting feature (unlike accessors in the Java language) that they do not throw exceptions when applied to null. If the object to which the EL accessor is applied (for example, the foo identifier in ${foo.bar} and ${foo ["bar"]}) is null, the result of applying the accessor is also null. This turns out to be a pretty useful behavior in most cases, and you'll soon learn about it.
Finally, the dot operator and the square bracket operator may be interchanged to some extent. For example, you can also use ${user ["firstName"]} to retrieve the firstName property of a user object, just as you can use ${commands.dir} to get the value associated with the "dir" key in the commands map.
Operator
EL can also traverse object hierarchies that contain application data (exposed through scoped variables) or information about the environment (through EL implicit objects) by using identifiers and accessors. However, simply accessing this data is usually not enough to implement the presentation logic required by many JSP applications.
Finally, EL includes several operators for manipulating and comparing data accessed by EL expressions. These operators are summarized in Table 2.
Table 2. EL operator
Category operator
Arithmetic operators +, -, *, / (or div) and% (or mod)
Relational operators = = (or eq),! = (or ne), (or gt), = (or ge)
Logical operators & & (or and), | | (or or) and! (or not)
Validation operator empty
Arithmetic operators support addition, subtraction, multiplication, and division of numeric values. A remainder operator is also provided. Note: both division and remainder operators have alternative, non-symbolic names (to be consistent with XPath). A sample expression that demonstrates the use of arithmetic operators is shown in listing 5. The result of applying an arithmetic operator to several EL expressions is the result of applying the arithmetic operator to the numeric values returned by those expressions.
Listing 5. Using EL expression of arithmetic operator
${item.price * (1 + taxRate [user.address.zipcode])}
Relational operators allow you to compare numeric or textual data. The result of the comparison is returned as a Boolean. The logical operator allows you to merge Boolean values and return new Boolean values. Therefore, you can apply EL logical operators to nested relationships or the results of logical operators, as shown in listing 6.
Listing 6. EL expressions using relational and logical operators
${(x > = min) & & (x
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.