Date post: | 03-Jun-2018 |
Category: |
Documents |
Upload: | thuong-nguyen |
View: | 224 times |
Download: | 0 times |
of 16
8/12/2019 Atij More Jsp II 2x1
1/16
ATIJ EL + JSTL 1/32
EL + JSTL
Advanced Topics in Java
Khalid Azim [email protected]
http://www.ii.uib.no/~khalid/atij/
Version date: 2006-09-04
ATIJ EL + JSTL 2/32
Overview
Accessing Java Code using JSP Expression Language (EL)
Introduction to using JSP Standard Tag Library (JSTL) in JSP Pages
8/12/2019 Atij More Jsp II 2x1
2/16
ATIJ EL + JSTL 3/32
Accessing Java Code using JSP Expression Language (EL)
Simplifies using the "Pull Model".
Provides a navigational notation to pull information from the model and into a JSPpage.
Makes using the server state easier in a JSP page. The expression language automatically handles typecasting, nullvalues, and error
handling.
Notation for evaluating and returning the textual representation of values that arestored in the standard scopes:
${ expression }
ATIJ EL + JSTL 4/32
Topics on JSP EL
Activating EL Evaluation
Accessing Attributes in the Standard Scopes
Accessing Properties of Beans stored as Attributes in the Standard Scopes
Accessing Collections stored as Attributes in the Standard Scopes
Using EL Implicit Objects
Using EL Operators
EL Conditional Evaluation
8/12/2019 Atij More Jsp II 2x1
3/16
ATIJ EL + JSTL 5/32
Activating EL Evaluation
The web.xmlfile should be compatible with JSP 2.0:
Shopping ...
ATIJ EL + JSTL 6/32
EL Syntax
Syntax: ${ expression }
The result of expression evaluation replaces the expression and its delimiters.
EL expressions are comprised of literals, operators and variables.
Used in template text and within tags:
Thevalue of an attribute stored in a scopecan be accessed in an EL expression.
The value of an attribute in tags/elementscan be specified by an EL expression.
Functionscan also be called from EL expressions (covered later in these notes). To escape ${, use \${.
8/12/2019 Atij More Jsp II 2x1
4/16
ATIJ EL + JSTL 7/32
Accessing Attributes in the Standard Scopes
The variables in EL expressions can reference attributes that are stored in thestandard scopes.
The variable is looked up in the standard scopes in the following order: page, request,
session, or application. The first match is returned. If no match, return null.
The value of the variable is converted to a string if necessary by calling thetoString()method.
User name: ${username}
User name: ${sessionScope.username}
are equivalent to:
User name:
Standard Scope EL Implicit Object with Attributes from the Standard Scope (Maps)
page pageScope
request requestScope
session sessionScope
application applicationScope
ATIJ EL + JSTL 8/32
Accessing Properties of Beans stored as Attributes in the StandardScopes
Syntax: ${ beanAttr.prop1.prop2. ....propk}
All propiare legal Java identifier.
The dot operator allows navigating from one property to another every time it is usedin an expression.
${userinfo.username}
${userinfo.address.street}
${userinfo.address.zipcode}
8/12/2019 Atij More Jsp II 2x1
5/16
ATIJ EL + JSTL 9/32
Accessing Collections stored as Attributes in the Standard Scopes
If the attribute is an array, a List or a Map, then the following syntax can be used:
${ beanAttr[indexValue] }
${userTab[0]}
${userTab["0"]}
${userTab.0}
${itemList[0]}
${callMap["Monday"]}
${callMap[Monday]}
8/12/2019 Atij More Jsp II 2x1
6/16
ATIJ EL + JSTL 11/32
EL Implicit Object (cont.)
Getting the HTTP request method.
${request.method} ${requestScope.method}
${pageContext.request.method}
ATIJ EL + JSTL 12/32
Using EL Operators
Used for simple tasks related to presentation logic.
Operators have the standard precedence and associativity rules.
EL expressions with only literals and operators are evaluated as arithmeticexpressions.
Predefined EL literals: true, false, null.
Type of
Operators Operators Example Result
Indexing [].( )
${initparam["dt_table"]}${array["0"]}${initparam.dt_table}${ 2 * (3 + 4)}
Arithmetic -* / div% mod+ -
${1 + 2 * 3}${"10" + 2}${100 % 98}${var * 2}
7122var coerced to number beforeoperation
8/12/2019 Atij More Jsp II 2x1
7/16
ATIJ EL + JSTL 13/32
Relational == eq!= ne< lt
> gt= ge
${obj == obj}${2 == 3}${obj == null}
${bigI == bigJ}${obj1 == obj2}${"10" < "2"}
truefalsefalse
dep. on compareTo(bigI, bigJ)value of obj1.equals(obj2)true
Logical && and|| or! not
${obj1 != null && obj2 != null}${!(2 == 3)} true
The EmptyOperator
empty ${empty null}${empty attr.prop}
true
trueif attr.propis null, an emptystring, an empty array, an emptymap, or an empty collection, i.e
an empty container.The TernaryOperator
test ?trueExpr :falseExpr
${(shopping.total > 1000)? "Esteemed Customer": "Dear Customer" }
Dependent on the test.
Type ofOperators
Operators Example Result
ATIJ EL + JSTL 14/32
null-friendly JSP EL
In expressions containing dot or []operators, an undefined variable is treated ashaving the nullvalue.
In arithmetic and relational expressions, an undefined variable is treated as 0(zero).
In logical expressions, an undefined variable is treated as false.
${ying}
${ying.yang}
${10 - yang}
${10 / yang}
${10 % yang}
${10 < yang}
${not yang}
8/12/2019 Atij More Jsp II 2x1
8/16
ATIJ EL + JSTL 15/32
Implementing JSP EL Functions
1. Implement a Java class with a publicstaticmethod.
Can have a non-empty parameter list, and is normally declared as non-void.
The class file is installed in /WEB-INF/classesdirectory
2. Create a Tag Library Descriptor (TLD) file specifies a mapping between the functionand the JSP page.
The element is used to specify the pertinent information.
A subelement specifies the name that will be used in a JSP page.
A subelement specifies the name () that will be used to invokethe function from a JSP page, the class of the function () , thesignature of the method () .
The TDL file has a .tldextension and can be installed in /WEB-INFdirectory.
3. Create a taglibdirective in the JSP page.
The prefixattribute specifies the namespacefor invoking the method.
The uriattribute value matches the one in the TLD file.
4. Invoke the EL function from the JSP page.
${namespaceInTaglibDirective:nameinTLD()}
ATIJ EL + JSTL 16/32
Example: JSP EL function (ELFunctionapplication)
The TLD file (DatingFunction.tld) 1.2 /mughal/data dateMe kam.DateWrapper java.util.Date getDate()
The class (DateWrapper.java)implementing the function
package kam;import java.util.Date;public class DateWrapper{ public static Date getDate(){ return new Date(); }}
The JSP file (callingDateFunction.jsp) Calling EL Function Calling EL Function ${kam:dateMe()}
8/12/2019 Atij More Jsp II 2x1
9/16
ATIJ EL + JSTL 17/32
Introduction to using JSP Standard Tag Library (JSTL) in JSP Pages
The Core Library:
The Formatting Library:
The SQL Library:
The XML Library:
ATIJ EL + JSTL 18/32
Installing JSTL 1.1
From TOMCAT_HOME\webapps\jsp-examples\WEB-INF\lib, copy the following filesto myApp\WEB-INF\lib:
- jstl.jar
- standard.jar
These jarfiles can also be installed in TOMCAT_HOME\shared\lib, so that allapplications can share them.
8/12/2019 Atij More Jsp II 2x1
10/16
ATIJ EL + JSTL 19/32
Core JSLT
General purpose
Output:
Set property:
Removing attributes: Exception handling:
Conditionals
Conditional Include:
Multiple Choice: , ,
URL-related
Importing:
URL rewriting:
Redirecting:
Parameter passing:
Iteration
Iteration:
Tokenizing:
ATIJ EL + JSTL 20/32
The Tag
Version for setting attribute variables:
or
...value inside body...
Version for setting bean propertiesandMap values:
or
...value inside body...
The targetmust evaluate to a reference.
8/12/2019 Atij More Jsp II 2x1
11/16
8/12/2019 Atij More Jsp II 2x1
12/16
ATIJ EL + JSTL 23/32
myJSTLExamples
JSTL Examples
1:
2:
3:
4:
5:
6: ${myShoes}
7:
8:
ATIJ EL + JSTL 24/32
9:
--%>
10:
Output in browser window:
1: Hello2: Shoes Ekin 999.9 13: Wednesday4: attribute in session5: Shoes sadida 999.9 16: Shoes sadida 999.9 17: Shoes sadida 999.9 18: Shoes sadida 999.9 19: {2=Tuesday, 4=Thursday, 6=Saturday, 1=Monday, 3=Wednesday, 7=Sunday, 5=Friday}
10: {2=Tuesday, 4=Thursday, 6=Saturday, 1=Monday, 3=Wednesday, 7=FREE DAY, 5=Friday}
8/12/2019 Atij More Jsp II 2x1
13/16
ATIJ EL + JSTL 25/32
The Tag
Used to iterate over the elements in an iterable: an array, Collection, Map, ResultSetor a comma-separated Strings.
${anItem.property}
...
See files forEachTag.jspand kam.Item.javain myJSTLExamplesapplication.
Examples:
The loop
${item}
prints: en
to
tre
fire
ATIJ EL + JSTL 26/32
The loop
Item no. ${itemLoopCount.count}:
${item.itemName}
${item.itemDescription}
${item.price}
${item.quantity}
prints:Item no. 1: Shoes Ekin 999.9 10
Item no. 2: Shirt Van Heusen 9.9 30
Item no. 3: Shampoo Simple 99.9 15
Item no. 4: Socks Golden Toe 900.0 9
Item no. 5: Suit Armani 49.9 17
An optional attribute called varStatuscan be defined. It provides access to theiteration number.
forEach-loops can be nested.
8/12/2019 Atij More Jsp II 2x1
14/16
ATIJ EL + JSTL 27/32
The Tag
Allows the contents of its body to be executed or skipped, depending on the testcondition. Note there is no tag.
...if-body...
Example: See file conditionalTags.jspin myJSTLExamplesapplication.
Item no. ${itemLoopCount.count}:
${item.itemName}
${item.itemDescription}
${item.price}
${item.quantity}
Output:Item no. 2: Shirt Van Heusen 9.9 30
Item no. 4: Socks Golden Toe 900.0 9
ATIJ EL + JSTL 28/32
The Multiple Choice Tags: , ,
Allows the body of only onetag to be executed.
If none of the tags match, the body of the tag is executed.
There is no fall-through.
...
...
...
...
...
8/12/2019 Atij More Jsp II 2x1
15/16
ATIJ EL + JSTL 29/32
Example: See file conditionalTags.jspin myJSTLExamplesapplication.
The following code:
HOLIDAY
HOLIDAY
${day}
generates:
Monday Tuesday Wednesday Thursday Friday HOLIDAY HOLIDAY
ATIJ EL + JSTL 30/32
Example: the myELShop application
The myELShopweb application is a rewrite of the myEShopapplication using EL andJSTL.
See the following files for the myELShopweb application:
index.html,WEB-INF\web.xml,WEB-INF\src\kam\shopping\Item.java,WEB-INF\src\kam\shopping\ELShoppingServlet.java,
ELShop.jsp,ELCart.jsp, ELCheckout.jsp,ELError.html myELShop.xml
8/12/2019 Atij More Jsp II 2x1
16/16
ATIJ EL + JSTL 31/32
JSTL Libraries Overview
CORE LIBRARY FORMATTING LIBRARY XML LIBRARY SQL LIBRARY
GENERAL PURPOSE INTERNATIONALIZATION CORE XML DATABASE
XML FLOW
CONDITIONAL
FORMATTING
URL-RELATED TRANSFORMATION
ATIJ EL + JSTL 32/32
ITERATION
CORE LIBRARY FORMATTING LIBRARY XML LIBRARY SQL LIBRARY