Tapestry 5.pdf

Post on 26-Oct-2015

44 views 2 download

Transcript of Tapestry 5.pdf

Tapestry 5

by Igor Drobiazko and Renat Zubairov

TOC

• Introduction

• Tapestry 5 overview

• How to get started (+Demo)

• Developer productivity (+Demo)

• Components power (+Demo)

• Page navigation patterns

• AJAX (+Demo)

2

Introduction

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

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

Who uses Tapestry?

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

6

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

Why Tapestry was chosen?

• Component orientation

• Quality

• Testability

• Simplicity

• Efficiency

• Flexibility

• Open source

9

Tapestry 5 overview

Component oriented

11

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

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

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

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

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

Demo #1How to get started

Developer productivity

Productivity

• Zero turnaround – live reloading of

– Templates

– Page and Component classes (!)

– Message catalogs

– Assets

19

Feedback

20

Demo #2Error Pages & Live Class Reloading

Components power!

BeanEditForm

public class User {

private long id;

private String name;

private String email;

private Date birthday;

private Role role = Role.GUEST;

...

}

23

Grid

public class User {

private long id;

private String name;

private String email;

private Date birthday;

private Role role = Role.GUEST;

...

}

24

Demo #3BeanEditForm & Grid components

Page navigation patterns

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 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

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

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

AJAX

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

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

Ajax Example

Trigger Link

Update Zone with new content

34

Demo #4AJAX made simple

Questions?

Dependency Injection

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

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

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

Testability

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

Integration with other frameworks

Integration possibilities

• Tapestry 5 natively integrates with

– Hibernate for ORM

– Spring for dependency injection

– JBoss Seam

– Acegi for security

45

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