ch. 8 script free pages
TRANSCRIPT
Chapter 8 Script-free Pages
St.Actions and EL
MVC model: set/get attributesAs
strings
Value of the “name” attribute
javaBeans (not EJB)• reusable software components • classes written in Java conforming to a particular convention • used to encapsulate many objects into a single object (the
bean), so that they can be passed around as a single bean object
• a Java Object that • is serializable, • has a nullary constructor, and • allows access to properties using getter and setter methods.
MVC model: set/get attributesAs
instances
JavaBean standard actions
Print the result of getName()
Using an expression this is a javaBean
<jsp:useBean> and <jsp:getProperty>
Standard actionIdentifier of bean object
Class type
<jsp:useBean> to create a bean
_jspService()
<jsp:setProperty>
Conditional body in <jsp:useBean>
class = reference = object type
Polymorphic (reference and object) bean references
Reference ≠ object type? Abstract class
Concrete classin package foo
add a type attribute to the tag
No class just type
Add a scope attribute
default
From the request to the JSP. Directly!
Add a param attribute
Set bean property to value of the
request parameter
All request parameters match bean properties
person has a dog & dog has a name
property of a property?
Expression Language (EL)
Expression Language (EL)A scripting language which allows easier
access to JavaBeans through JSP.
Access Java objects without using Java
Widely spread
Moving towards the designer
The EL language
Map objects
“[]” instead of “.”
An index into a list of arrays
example
Beware: [] does not mean “array”
Example(use either “.” and “[]”) example
(beans and maps)
Deeper understanding
Nesting
Back to EL implicit objects
Maps t
o sco
pe at
tribute
s
Maps t
o req
uest
param
s
Map of
cook
ies
Maps t
o req
uest
head
ers
Map of
conte
xt inn
ate pa
rams
refere
nce t
o obje
ct
Requesting Parameters
Getting useful staff• Scripting
• Host is: <%= request.getHeader(“host”) %> • Method is: <%= request.getMethod() %>
• EL • Host is: ${header[“host”]} • Host is: ${header.host} • Method is: ${request.method} • Method is: ${requestScope.method} • Method is: ${pageContext.request.method}
Gives attributes (not properties)
There is no such implicit object
Prefacing attributes• Control scoping
${person.name} or ${requestScope.person.name}
• Avoid problems with the name’s string nature
request.setAttribute(“person”, p);
request.setAttribute(“foo.person”, p);
${foo.person.name}
${requestScope[“foo.person”].name}
Cookies & init params
<% Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { if ((cookies[i].getName()).equals(“userName”)) { out.println(cookies[i].getValue()); } } %>
${ cookie.userName.value}
<context-param> <param-name>mainEmail</param-name> <param-value>[email protected] </param-value> </context-param>
email is: <%= application.getInitParameter(“mainEmail”) %>
email is: ${ initParam.mainEmail}
EL functions
EL operators<c:if test="${sessionScope.cart.numberOfItems > 0}">
... </c:if>
<c:if test="${bean1.a < 3}" > ...
</c:if>
<c:if test="true" > ...
</c:if>
<mytags:example attr1="an expression is ${'${'}true}" />