16.1 Motivating EL Usage

As illustrated in Figure 16-1, there are a number of different strategies that JSP pages can use to invoke Java code. One of the best and most flexible options is the MVC approach (see Chapter 15). In that approach, a servlet responds to the request; invokes the appropriate business logic or data access code; places the resultant data in beans; stores the beans in the request, session, or servlet context; and forwards the request to a JSP page to present the result.

Figure 16-1. Strategies for invoking dynamic code from JSP.


The one inconvenient part about this approach is the final step: presenting the results in the JSP page. You normally use jsp:useBean and jsp:getProperty , but these elements are a bit verbose and clumsy. Furthermore, jsp:getProperty only supports access to simple bean properties; if the property is a collection or another bean, accessing the " subproperties " requires you to use complex Java syntax (precisely the thing you are often trying to avoid when using the MVC approach).

The JSP 2.0 expression language lets you simplify the presentation layer by replacing hard-to-maintain Java scripting elements or clumsy jsp:useBean and jsp:getProperty elements with short and readable entries of the following form.

 ${  expression  } 

In particular, the expression language supports the following capabilities.

  • Concise access to stored objects. To output a "scoped variable" (object stored with setAttribute in the PageContext , HttpServletRequest , HttpSession , or ServletContext ) named saleItem , you use ${saleItem} . See Section 16.5.

  • Shorthand notation for bean properties. To output the companyName property (i.e., result of the getCompanyName method) of a scoped variable named company , you use ${company.companyName} . To access the firstName property of the president property of a scoped variable named company , you use ${company.president.firstName} . See Section 16.6.

  • Simple access to collection elements. To access an element of an array, List , or Map , you use ${ variable [ indexOrKey ]} . Provided that the index or key is in a form that is legal for Java variable names , the dot notation for beans is interchangeable with the bracket notation for collections. See Section 16.7.

  • Succinct access to request parameters, cookies, and other request data. To access the standard types of request data, you can use one of several predefined implicit objects. See Section 16.8.

  • A small but useful set of simple operators. To manipulate objects within EL expressions, you can use any of several arithmetic, relational, logical, or empty-testing operators. See Section 16.9.

  • Conditional output. To choose among output options, you do not have to resort to Java scripting elements. Instead, you can use ${ test ? option1 : option2 } . See Section 16.10.

  • Automatic type conversion. The expression language removes the need for most typecasts and for much of the code that parses strings as numbers .

  • Empty values instead of error messages. In most cases, missing values or NullPointerException s result in empty strings, not thrown exceptions.

Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net