Title: DT2283 Web Development
1DT228/3 Web Development
JSP Actions elements and JSTL
2JSP Action Elements
JSP pre-defined tags (standard actions elements)
Done
External Tag library -------? Custom
Done -------? Java Standard tag library
3JSP Action elements External tag libraries JSTL
- JSP 1.2 introduced supported for a special tag
librarycalled the JSP Standard Tag Library
(JSTL) - Version 1.0 released in June 2002Version 1.1
released in June 2004 - Specification Evaluation 1.2 Maintenance Release
2 - released in Aug 2006
- The JSTL saves programmers from having to develop
custom tag libraries for a range of common
tasks, suchas database access, conditional loops
etc - Enabled developers to produce more maintainable,
simpler JSP code - Important development for JSP technology
4JSTL
- The JSP Standard Tag Library groups actions into
four libraries as follows
more may be added in later releases
5JSTL
- To use any of these libraries in a JSP, need to
declare using the taglib directive in the JSP
page, specifying the URI and the Prefix
Example of declaring use of core library lt_at_
taglib prefix c uri http//java.sun.com/js
p/jstl/core gt
6JSTL Example
Example JSP page using JSTL that outputs 1 to 10
on a webpageusing the ltcforEachgt and ltcoutgt
tags of the core library
lt_at_ taglib uri"http//java.sun.com/jsp/jstl/core"
prefix"c" gt lthtmlgt ltheadgt lttitlegtCount to
10 Example (using JSTL)lt/titlegt lt/headgt
ltbodygt ltcforEach var"i" begin"1" end"10"
step"1"gt ltcout value"i" /gt ltbr /gt
lt/cforEachgt lt/bodygt lt/htmlgt
A taglib directive declare use of core library
JSTL tag examples
7JSTL Example ltcforeachgt
Looking more closely at ltcforEach taggt ..
ltcforEach var"i" begin"1" end"10" step"1"gt
ltcout value"i" /gt ltbr /gt
lt/cforEachgt lt/bodygt lt/htmlgt
The ltforEachgt tag enables loop logic. In this
case, will lookthrough 10 times. Equivalent to
java for loop Closing tag ltcforEachgt placed
after the end of body of loop
8JSTL Example ltcforeachgt
All JSTL tags have a set of attributes (similar
to HTML tags..) e.g. ltcforeachgt tag has 6
attributes var, items, varStatus,
begin, end, step The full details for each
attribute is in the JSTL specification document
(on distrib). See p 65 for ltcforeachgt tag
definition Willl need to use this document to
verify WHICH tag should be used and HOW is
should be used
9JSTL Example ltcoutgt
ltcoutgt .. outputs a value to webpage.
Usually uses just one attribute
value Examples ltcout value"i" /gt ltcout
valueThe result of 1 2 is 12 /gt ltcout
valueparam.userName" /gt
10JSTL Example ltcifgt
ltcifgt .. evaluates a condition. Uses an
attribute test to hold the condition Example
lt-- Simple if conditions --gt ltcif
test'param.p "someValue"'gt Generate
this template text if p equals someValue lt/cifgt
Example 2 ltcif test'param.p'gt Generate
this template text if p equals "true lt/cifgt
11JSTL Multiple if conditions
An if/else action requires the use of the
ltcchoosegt tag Syntax ltcchoosegt body
content (ltwhengt and ltotherwisegt
subtags) lt/cchoosegt
See page 52 of JSTL specification doc.
12JSTL Multiple if conditions
Uses ltcchoosegt, ltcwhengt and ltcotherwisegt Examp
le ltcchoosegt ltcwhen test'param.p
"0"'gt ltcout value zero recorded/gt
lt/cwhengt ltcwhen test'param.p "1"'gt
Generate this ltcout value single value/gt
lt/cwhengt ltcotherwisegt ltcout value Set
to param.p/gt lt/cotherwisegt lt/cchoosegt
13JSTL Other core ltc..gt actions
Other examples (NOT a complete list!)
ltcsetgt .sets the value of a variable
ltcremovegt .removes a scoped
variable ltccatchgt .catches an exception ltcurlgt
.. encodes a URL ltcimportgt imports the content
of a resource ltcredirectgt.. redirects to another
URL ltcparamgt.. adds a request parameter to other
actions
14JSTL ltfmt..gt example
JSTL contains a set of actions in the Formatting
library these tags are useful for formatting
numbers, times and dates e.g. ltfmtparseDategt
(on page 140 of JSTL specification)
15JSTL ltfmtparseDategt example
lt_at_ taglib uri"http//java.sun.com/jsp/jstl/fmt"
prefixfmt" gt lthtmlgt etc etc ltfmtparseDate
value param.empDate var parsedEmpDate
type date pattern yyyy-MM-dd /gt etc
etc lt/htmlgt
The fmtparseDate action takes the date or time
string specified by the value attribute (e.g.
2001-09-28) , interprets it according to the
pattern defined by the pattern attribute and
saves it in a variable called parsedEmpDate
more later
16JSTL other ltfmtgt actions
Other examples
ltfmtformatNumbergt - formats a numeric value
e.g. number of digits, currency, decimal
place e.g. ltfmtformatNumber value"12.3"
pattern".000"/gt will output 12.300 ltfmtforma
tDategt --formats a date and time
17JSTL Expression language
- Up to now, could only use Java expressions to
assign dynamic values ? syntax errors common - JSTL now provides an expression language (EL) to
support the tags ? simpler syntax, less errors - The EL is now part of the JSP specification (as
of versions JSP2.0) can be used in JSTL tags or
directly in JSP pages.
18JSTL Expression language
- All EL expressions are evaluated at runtime
- The EL usually handles data type conversion and
null values --? easy to use - An EL expression always starts with a and ends
with a
19JSTL Expression language
- The expression can include
- - literals ( 1, 100 etc)
- - variables
- - implicit variables (more later)
- Examples
-
- ltcout value 123 /gt
- ltcif test param.Address D6 /gt
expression
20JSTL Expression language - operators
! lt
- / or div
gt lt gt
- Logical operators consist of (or and), (or
or), and ! (or not). - The empty operator is a prefix operator that can
used to determine if a value is null or empty.
For example - ltcif testempty param.namegt
- Please specify your name.
- lt/cifgt
21JSP Implicit objects
- In JSP, need to be able to access information
about the environment in which the page is
running e.g. the parameters passed in a request
for a form, the browser type of the user etc. - Implicit objects are a set of Java objects that
the JSP Container makes available to developers
in each page. These objects may be accessed as
built-in variables via scripting elements
22JSTL implicit variables
- The JSTL EL allows these objects to be accessed
as Implicit Variables - Implicit variable are just pre-agreed fixed
variable names that can be used in JSTL
Expressions -
- --? Think of as variables that are
automatically available to your JSP page..
23JSTL Expression language Implicit Objects
- Full set of implicit variables on page 211 of
JSTL documentation (includes header,
pageScope..) or on page 74 of OReillys Java
Server Pages book - Very common implicit object is param
- param refers to parameter passed in a request
message (e.g. information entered into a form by
a user). - e.g. ltcout value param.userName/gt
- Further Examples of using param in next topic
-
24Comparing JSTL vs Scriptlets
JSTL removes complexity by using tags instead of
java code(abstraction) JSP pages using JSTL
usually easier to maintain JSTL allows HTML
tag developers to program JSTL often more
difficult to debug Note Using JSTL does not
eliminate scriplets entirely.. may still need
them sometimes for more complex logic
http//www.informit.com/isapi/product_id27F6F199
-F5FD-474C-AC7F-B3FC2C1F57B6/st94C03A97-1188-48
75-8A06-17743BA224B7/session_id6A766315-391B-4F
55-9AFB-B5425F6196C5/content/articlex.asp
25Info on JSTL
1. Java Server Pages by Hans Bergsten full
reference on all JSTL tags in the Appendix. 2.
Suns JSTL 1.0 tag specification on distrib.
Good for definition of each tag. Poor on
examples. 3. Specification Evaluation 1.2
Maintenance Release 2 on distrib.
26Setting up JSTL in a web application
- JSTL is provided as part of Tomcat 5
- Ensure the two JSTL .jar files are copied into
the WEB-INF\lib directory of your web application - Ensure the taglib directive(s) is in your JSP page