"> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

® IBM Software Group © 2007 IBM Corporation JSP Expression Language 4.1.0.3.

Similar presentations


Presentation on theme: "® IBM Software Group © 2007 IBM Corporation JSP Expression Language 4.1.0.3."— Presentation transcript:

1 ® IBM Software Group © 2007 IBM Corporation JSP Expression Language 4.1.0.3

2 2 After completing this unit, you should be able to:  Understand and create JSP expressions using JSP expression language  Understand the use of implicit objects in JSP expression language  Disable expression evaluation when required  Create and deploy JSP expression functions After completing this unit, you should be able to:  Understand and create JSP expressions using JSP expression language  Understand the use of implicit objects in JSP expression language  Disable expression evaluation when required  Create and deploy JSP expression functions Unit objectives

3 3 JSP Expressions  Expressions allow JSP pages to dynamically access and process server-based data  For example, inserts the current date (on the server) into the HTTP output stream  JSP Pages and JSP Documents have equivalent forms with different syntaxes:  Page:  Document: expr  Expressions are often used to provide dynamic content to static HTML tags <INPUT type="text" name="name" value=" ">

4 4 Drawbacks of JSP Expressions  Expressions are Java  Along with other forms of scripting, they tend to violate separation of responsibilities  Expressions for simple concepts can become complex  Example - there is a User object in session scope. To access the name of that user might require the following: ((User)session.getAttribute(“user”)).getName()  In JSP Documents, expressions cannot appear inside quotes  Legal: ”>  Illegal:... ”>

5 5 What is JSP Expression Language?  JSP Expression Language (EL) is a simple language based on:  Local variables of the current page  Nested properties and accessors to collections  Relational, logical and arithmetic operators  A set of implicit objects  Extensible functions mapping into static methods in Java classes  EL is invoked via the construct ${...}  EL may be used:  In attribute values for standard and custom actions  Within template text

6 6 EL Examples  EL expressions can be used in any attribute that can accept a run-time expression...  EL can be used directly in template text Hello, ${user.firstName} ${user.lastName}

7 7 Implicit Objects  Several implicit objects are available to EL expressions used in JSP pages:  pageContext  pageScope  requestScope  sessionScope  applicationScope  param  paramValues  header  headerValues  cookie  initParam Example: is equivalent to ${sessionScope.name} Error JSP page examples: ${pageContext.errorData.requestURI} ${pageContext.errorData.throwable}

8 8 Syntax Overview  Variables are accessed by name  Generalized [ ] and. operators  Can be used to access  Maps  Lists  Arrays of objects  JavaBeans properties  Can be nested arbitrarily  Relational comparisons  Arithmetic and Logical operators Example: to access the name property of the user bean in session scope: ${sessionScope.user.name}

9 9 Basic Syntax Elements  Literals  Boolean, integer, floating point, string and null  Operators  Accessor. []  Arithmetic + - * / %  Relational == != =  Logical && || !  Empty empty  Conditional ? : ${param.cost * 1.085} ${empty sessionScope ? “yes” : “no”}

10 10 Named Variables  EL contains a variable resolver which will search the page, request, session, and application scopes for values  For example:  A servlet method contains the code: session.setAttribute("user", aUser);  The servlet then forwards to a JSP with the following EL expression: ${user}  The variable resolver resolves the name to the object in: sessionScope.user  If the attribute is not found in any scope, null is returned  The specification is vague as to what happens when like- named attributes are found in more than one scope

11 11 Functions  EL functions  Can be mapped to static Java methods in any Java class  Can be invoked in an EL expression  Provide the some of the functionality of custom tags, but much more simply... IBM ${stock:lookup("IBM")} lookup com.ibm.Stockfns java.lang.String lookup(java.lang.String)

12 12 Functions Example - Add Two Numbers public class AddFunction { public static int addMethod(String x, String y){ int a=0; int b=0; a=Integer.parseInt(x); b=Interger.parseInt(y); return a+b; } addMethod AddFunction int addMethod(java.lang.String, java.lang.String) …. The total amount is ${af:addMethod(amt1, amt2)} Java class Tag Library Descriptor JSP

13 13 EL Support  The JSP compiler in WebSphere Application Server V6.1 and Rational Application Developer V7.0 supports EL  Required for JSP 2.0 compliance  Application Developer V7.0 does NOT provide explicit support  No content assist  No validation  No wizards  No palette items or snippets Java, not EL

14 14 Checkpoint 1.How can one recognize an EL expression? 2.In what two places may EL be used? 3.Which EL implicit object contains the attributes of the HTTP session? 4.How are the Java functions that underlie EL functions restricted?

15 15 Checkpoint solutions 1.EL expressions are enclosed between ${ and }. 2.EL may be used in tag attributes and in template text. 3.sessionScope 4.They must be public static.

16 16 Having completed this unit, you should be able to:  Understand and create JSP expressions using JSP expression language  Understand the use of implicit objects in JSP expression language  Disable expression evaluation when required  Create and deploy JSP expression functions Having completed this unit, you should be able to:  Understand and create JSP expressions using JSP expression language  Understand the use of implicit objects in JSP expression language  Disable expression evaluation when required  Create and deploy JSP expression functions Unit summary


Download ppt "® IBM Software Group © 2007 IBM Corporation JSP Expression Language 4.1.0.3."

Similar presentations


Ads by Google