#{…} Syntax
The first article in the "Web Tier to Go With Java EE 5" series, "Summary of New Features in JavaServer Pages (JSP) 2.1 Technology," introduced the new unified expression language. This fifth article of the series explains
The JSP 2.0 Expression Language
The expression language (EL) introduced in JSP 2.0 significantly simplified access to application data in JSP files. For example, the following expression accesses the name of a
The dollar-sign character triggers the evaluation of an EL expression by the JSP software engine. In terms of the preceding example, the expression In the early versions of JSP technology, page authors had to use scriptlets (pieces of Java programming language code) to insert dynamic data into their pages. With the expression language—along with other ease-of-use features available in JSP technology—page authors can write JSP pages that are totally free of code. The JavaServer Faces Expression Language
During the development of JSP 2.0 technology, another web technology offering significant ease-of-use features appeared on the scene: JavaServer Faces technology. This technology's primary feature is its flexible and extensible UI component model, which includes
It also includes a set of component tags that are used to map directly to individual, stateful, server-side UI component objects.
A JavaServer Faces page implemented using JSP technology should look very familiar to a JSP page author, except for one thing: Its EL expressions make use of a different syntax, the pound syntax (also known as the sharp syntax, octothorpe syntax, and number-sign syntax): Comparison of Request Processing Models Supported by JSP and JavaServer Faces Technologies
As shown in figure 1, the request processing performed on a JSP page is very simple. The JSP engine simply takes the mix of static and dynamic data that it encounters in a page and writes the data to the HTTP response.
In JSP 2.0, an EL expression is always evaluated as soon as it is encountered by the JSP engine. These expressions are therefore referred to as immediate expressions. They yield a read-only value, which is also known as an rvalue. In programming language terminology, an rvalue represents the right-hand side of an assignment expression. An immediate expression is all that is needed in the context of JSP pages, because the request-processing model features a single phase of execution called the render phase, during which static and dynamic data is immediately written to the response. The story is quite different for a JavaServer Faces page. It is structured as a tree of UI components that is subjected to a much more complex request-processing life cycle, as shown in Figure 2.
When a page is first displayed, components simply render themselves. Through their renderers, they write data to the HTTP response. This is exactly the same behavior as a normal JSP tag. However, the power of JavaServer Faces technology lies in its ability to abstract the handling of input components when a form is submitted back to the server for processing. This is where the JavaServer Faces request processing life cycle really shines. For example, consider the following JavaServer Faces component tag:
When the form is submitted, the apply request values phase of the JavaServer Faces life cycle evaluates the EL expression
If the user enters
A JavaServer Faces component tag also has a set of attributes for referencing methods that can perform certain functions on that component. In the preceding example, the validator attribute uses an EL expression to refer to the Why the Unified Expression Language Supports Two Syntaxes
As the previous section demonstrates, EL expressions used in JavaServer Faces tags are clearly quite different from EL expressions used in JSP pages. With the You could argue, however, that a single syntax could have done the job. Indeed, a deferred expression can only be used in the context of a tag handler. Furthermore, it is a requirement for a tag handler to declare whether an attribute accepts immediate or deferred expressions because the JSP engine must know how to deal with the expression. Given that it is the tag developer who dictates whether immediate or deferred expressions are accepted by a specific tag attribute, it is definitely legitimate to question why two different syntaxes have been defined while a single one would have sufficed. The expert groups for the JSP and JavaServer Faces technologies have debated this question at great length. While they agreed that it seems simpler to support a single syntax for EL expressions (whether they are immediate or deferred), the expert groups also had to consider the fact that both syntaxes were already well established in their respective communities. Deprecating a syntax in favor of the other one would have been painful for the affected community, so the expert groups finally decided to keep the two syntaxes, each with its own semantics. This is an acceptable compromise, given that both history and backwards compatibility had to be considered. Finally, you might also wonder whether attributes in JavaServer Faces components that are strictly used with read-only values really have a need for deferred expressions. For example, the following tag represents a component that simply writes the content of the value attribute to the HTTP response.
In this case, why not simply use Conclusion
As this article explained, the new unified expression language's support for the dollar-sign and pound syntaxes is important for aligning JSP and JavaServer Faces technologies, satisfying the needs for both immediate and deferred expressions, and for preserving backwards compatibility. See the article Unified Expression Language to learn more about the unified expression language. Meanwhile, watch for the next article in this series on the improvements made to the tree-creation and content-interweaving mechanisms supported by JavaServer Faces technology. For More Information
Other articles in the "Web Tier to Go With Java EE 5" series:
JavaServer Pages Technology FAQ |
| |||||||||||||||||||||||
|
| ||||||||||||