ch. 8 script free pages

35
Chapter 8 Script-free Pages St.Actions and EL

Upload: manolis-vavalis

Post on 06-Jul-2015

296 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Ch. 8 script free pages

Chapter 8 Script-free Pages

St.Actions and EL

Page 2: Ch. 8 script free pages

MVC model: set/get attributesAs

strings

Value of the “name” attribute

Page 3: Ch. 8 script free pages

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.

Page 4: Ch. 8 script free pages

MVC model: set/get attributesAs

instances

Page 5: Ch. 8 script free pages

JavaBean standard actions

Print the result of getName()

Using an expression this is a javaBean

Page 6: Ch. 8 script free pages

<jsp:useBean> and <jsp:getProperty>

Standard actionIdentifier of bean object

Class type

Page 7: Ch. 8 script free pages

<jsp:useBean> to create a bean

_jspService()

Page 8: Ch. 8 script free pages

<jsp:setProperty>

Page 9: Ch. 8 script free pages

Conditional body in <jsp:useBean>

Page 10: Ch. 8 script free pages

class = reference = object type

Polymorphic (reference and object) bean references

Page 11: Ch. 8 script free pages

Reference ≠ object type? Abstract class

Concrete classin package foo

Page 12: Ch. 8 script free pages

add a type attribute to the tag

Page 13: Ch. 8 script free pages

No class just type

Page 14: Ch. 8 script free pages

Add a scope attribute

default

Page 15: Ch. 8 script free pages

From the request to the JSP. Directly!

Page 16: Ch. 8 script free pages

Add a param attribute

Set bean property to value of the

request parameter

Page 17: Ch. 8 script free pages

All request parameters match bean properties

Page 18: Ch. 8 script free pages

person has a dog & dog has a name

Page 19: Ch. 8 script free pages

property of a property?

Page 20: Ch. 8 script free pages

Expression Language (EL)

Page 21: Ch. 8 script free pages

Expression Language (EL)A scripting language which allows easier

access to JavaBeans through JSP.

Access Java objects without using Java

Widely spread

Page 22: Ch. 8 script free pages

Moving towards the designer

Page 23: Ch. 8 script free pages

The EL language

Map objects

Page 24: Ch. 8 script free pages

“[]” instead of “.”

An index into a list of arrays

Page 25: Ch. 8 script free pages

example

Beware: [] does not mean “array”

Page 26: Ch. 8 script free pages

Example(use either “.” and “[]”) example

(beans and maps)

Page 27: Ch. 8 script free pages

Deeper understanding

Page 28: Ch. 8 script free pages

Nesting

Page 29: Ch. 8 script free pages

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

Page 30: Ch. 8 script free pages

Requesting Parameters

Page 31: Ch. 8 script free pages

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

Page 32: Ch. 8 script free pages

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}

Page 33: Ch. 8 script free pages

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}

Page 34: Ch. 8 script free pages

EL functions

Page 35: Ch. 8 script free pages

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}" />