tapestry 5.pdf

46
Tapestry 5 by Igor Drobiazko and Renat Zubairov

Upload: edgar-rojas

Post on 26-Oct-2015

47 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Tapestry 5.pdf

Tapestry 5

by Igor Drobiazko and Renat Zubairov

Page 2: Tapestry 5.pdf

TOC

• Introduction

• Tapestry 5 overview

• How to get started (+Demo)

• Developer productivity (+Demo)

• Components power (+Demo)

• Page navigation patterns

• AJAX (+Demo)

2

Page 3: Tapestry 5.pdf

Introduction

Page 4: Tapestry 5.pdf

What is Tapestry?

• “Open source framework for creating dynamic, robust, highly scalable web applications in Java”

• Component-based

• Developer friendly

• Fast

• Lightweight&Powerful

• ASF 2.0 License

4

Page 5: Tapestry 5.pdf

Framework history/Roadmap

• Version 1 was developer proprietarily• Version 2 hosted on Sourceforge• Version 3 released as ASF top level project

around 2003/2004• Versions 4.x released in 2006

– Last maintenance release 4.1.5 released 8 Feb. 2008

• Version 5.0.x– Latest version 5.0.13 (beta) released 16 June 2008

• Version 5.1– Development will be started after 5.0 versions will

go into maintenance.

5

Page 6: Tapestry 5.pdf

Who uses Tapestry?

http://wiki.apache.org/tapestry/PoweredByTapestry

6

Page 8: Tapestry 5.pdf

Our experience with Tapestry

• Thresholding for OSS PM.

• Part of biggest OSS PM solution in the world.

• Used by the 3 biggest telecommunication operators in Germany

• Developed with Tapestry 4, prototype/scriptaculous, HiveMind, Quartz, JPOX.

• Deployed on IBM WebSphere cluster.

8

Page 9: Tapestry 5.pdf

Why Tapestry was chosen?

• Component orientation

• Quality

• Testability

• Simplicity

• Efficiency

• Flexibility

• Open source

9

Page 10: Tapestry 5.pdf

Tapestry 5 overview

Page 11: Tapestry 5.pdf

Component oriented

11

Page 12: Tapestry 5.pdf

Tapestry components

• Every component or page is represented – Java class– Optional template

• Java classes are – POJO (Plain Old Java

Object)– No inheritance required– No interfaces implemented– No XML configuration

involved– Just put them to the right

package

Page

Page class

Template (optional)

Component

Component class

Template (optional)

12

Page 13: Tapestry 5.pdf

Application package structure

• Suggested package structure:

• This package structure will work automatically, but you can change it!

com.example.app

components pages services entities

13

Page 14: Tapestry 5.pdf

Templates

• Tapestry templates are:– Well formed XML

files!

– NO JSP!

– Most parts are standard (X)HTML files

– Let designers do their work

• Templates are also:– Localizable

– Inherited

<html xmlns:t=“…tapestry_5_0_0.xsd">

<head>

<title>Hello World</title>

</head>

<body>

<h1>Hello World!</h1>

<p>Welcome, ${userId}</p>

<a t:type=“pagelink”

t:page=“Start”>

Refresh

</a>

</body>

</html>

14

Page 15: Tapestry 5.pdf

Events (1/2)

• Two purposes

– Represent request initiated by user

– Represent flow-of-control within a request

• Events are handled by methods inside page/component classes

• Methods are identified by

– Naming convention

– @OnEvent annotation

15

Page 16: Tapestry 5.pdf

Events (2/2)

Page template<a t:id="select“

t:type="actionlink“>

Action

</a>

Page class@OnEvent(component = "select")

void valueChosen() {

...

}

or

void onActionFromSelect() {

...

}

16

Page 17: Tapestry 5.pdf

Demo #1How to get started

Page 18: Tapestry 5.pdf

Developer productivity

Page 19: Tapestry 5.pdf

Productivity

• Zero turnaround – live reloading of

– Templates

– Page and Component classes (!)

– Message catalogs

– Assets

19

Page 20: Tapestry 5.pdf

Feedback

20

Page 21: Tapestry 5.pdf

Demo #2Error Pages & Live Class Reloading

Page 22: Tapestry 5.pdf

Components power!

Page 23: Tapestry 5.pdf

BeanEditForm

public class User {

private long id;

private String name;

private String email;

private Date birthday;

private Role role = Role.GUEST;

...

}

23

Page 24: Tapestry 5.pdf

Grid

public class User {

private long id;

private String name;

private String email;

private Date birthday;

private Role role = Role.GUEST;

...

}

24

Page 25: Tapestry 5.pdf

Demo #3BeanEditForm & Grid components

Page 26: Tapestry 5.pdf

Page navigation patterns

Page 27: Tapestry 5.pdf

Navigation

• State of the art of URL handling

– Redirect after post (by default)

– RESTfull, bookmarkable URLs

http://www.myapp.de/user/357

http://www.myapp.de/order/K1235DX

Object onEvent() {

return …;

}

• Depends on return type– Page Instance page to render– String Name of the Page to render– Class Class of the page to render– java.net.URLURL Redirect to– …

27

Page 28: Tapestry 5.pdf

Page activation context

• All pages in Tapestry may support a page activation context

• The state of the page is preserved across the requests

• According to REST paradigm, the page activation context is appended to the URL for the page.

• No HTTP Session used!

28

Page 29: Tapestry 5.pdf

Activate & Passivate events

public class Edit {

private User user = null;

private long userId = 0;

void onActivate(long id) {

user = userDAO.find(id);

userId = id;

}

long onPassivate() {

return userId;

}

}

http://localhost:8080/app/edit/1

29

Page 30: Tapestry 5.pdf

Activate/Passivate events with Encoder

Or even simpler:

public class Edit {

private User user;

void onActivate(User user) {

this.user = user;

}

Object onPassivate() {

return user;

}

}

public class Edit {

@PageActivationContext

private User user;

}

http://localhost:8080/app/edit/1

30

Page 31: Tapestry 5.pdf

AJAX

Page 32: Tapestry 5.pdf

What is AJAX?

• Ajax: Asynchronous JavaScript and XML

• A technique that is used for creation of dynamic and more user friendly web sites and web applications

• Increases responsiveness and interactivity of the web pages

• Asynchronous exchange of data between the client and the server

32

Page 33: Tapestry 5.pdf

Tapestry 5 AJAX

• Based on Prototype and script.aculo.us• ActionLink component may be used to trigger actions

on the server-side• Zone:

– Tapestry’s approach to perform partial updates to the client-side

– A component that marks a part of the page that can be dynamically updated

• Block:– A way to pass a chunk of a component template into a

component as a parameter– The content inside a <t:block> is not rendered by default

33

Page 34: Tapestry 5.pdf

Ajax Example

Trigger Link

Update Zone with new content

34

Page 35: Tapestry 5.pdf

Demo #4AJAX made simple

Page 37: Tapestry 5.pdf

Questions?

Page 38: Tapestry 5.pdf

Dependency Injection

Page 39: Tapestry 5.pdf

What is DI/IoC?

• A design approach that allows a working system to be fabricated from many small, easily testable pieces.

• Objects don't create other objects/dependencies

• Objects describe which dependencies should be created

• IoC container manages the life cycle of the service objects and connects them together

• The connecting part is called Dependency Injection

39

Page 40: Tapestry 5.pdf

Tapestry IoC

• Tapestry itself consists of over 120 inter-connected services• Service:

– The basic unit in Tapestry IoC– Consists of a service interface and a service implementation.– Scoped: singletons, threaded, etc

com.example.app

components pages services

AppModule.java UserDAO.java UserDAOImpl.java

entities

40

Page 41: Tapestry 5.pdf

Injecting services

public class AppModule{

public static void bind(ServiceBinder binder){

binder.bind(UserDAO.class, UserDAOImpl.class);

}

}

public class Page{

@Inject

private UserDAO dao;

}

41

Page 42: Tapestry 5.pdf

Testability

Page 43: Tapestry 5.pdf

Automated tests

• Tapestry is probably themost tested framework

• 1800 automated tests

• Designed for testability

• Integration tests

– Even for AJAX!

• It is easy to write tests!

43

Page 44: Tapestry 5.pdf

Integration with other frameworks

Page 45: Tapestry 5.pdf

Integration possibilities

• Tapestry 5 natively integrates with

– Hibernate for ORM

– Spring for dependency injection

– JBoss Seam

– Acegi for security

45

Page 46: Tapestry 5.pdf

Hibernate integration

• Achieved via tapestry-hibernate project

• How to integrate?– Put Hibernate-specific configuration into

hibernate.cfg.xml

com.example.app

components pages services entities

User.java Role.java

46