53
Component Based Web Development with Apache Wicket Web Application Development with just JAVA and HTML - Karthik Gurumurthy, Architect, Symcon Global Technologies Author – Pro Wicket, Apress

Wicket Tutorial

Embed Size (px)

Citation preview

Page 1: Wicket Tutorial

Component Based Web Development with Apache Wicket

Web Application Development with just JAVA and HTML

- Karthik Gurumurthy,

Architect, Symcon Global Technologies

Author – Pro Wicket, Apress

Page 2: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Agenda

2

• What is Wicket

• Core concepts of Wicket

• Developing a custom component

• Q&A

Page 3: Wicket Tutorial

POJO Based Web Development with Apache Wicket

The Java Ecosystem

3

‘ When depressed, Women consume chocolates & go shopping while

men invade other countries’

- An “old” chinese proverb

Luckily, Apache Wicket, by no means, gives an impression of having been

created when under depression unlike a few others ;-)

‘ When depressed, Women (still) consume chocolates & go shopping

while men create “yet another open source java web framework”’

- Karthik Gurumurthy ;-)

Page 4: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Quotes

4

‘ Wicket is not a framework, it’s a candy bar and

everybody loves candy bars..’

- Romain Guy, Lead Software Engineer, Java Swing

Team, Author, ‘Filthy Rich Swing Clients’

'Dude, where are my objects?!’ and then I saw Wicket!

Page 5: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Apache Wicket in a Nutshell

5

• Open Source (ASF)

• Component Based

• Clean separation of Markup and Code

• Plain Old Java and Plain old HTML– No new templating / expression language to learn

• Minimum Configuration needs – Zero XML Framework ( No Annotations either ☺)

• If you know Java and HTML, you already know quite a bit about Wicket!

Page 6: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Features

6

• Page Composition

– Panels, Borders and Markup Inheritance

• Excellent Localization & Style Support

– template and resource loading (_be.html, .xml)– localized models (e.g. for labels)– sophisticated resource bundle lookup (composition &

inheritance hierarchy)

Page 7: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Features

7

• Integration

– Spring– Guice– Hibernate – Jasper Reports

• Fancy Components

– sortable, filterable, pageable, data aware tables– date picker, rich text editor, Google Maps– tabbed panel, navigation, tree, wizard

Page 8: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Features

8

• State management– type safe sessions

• clustering support• back button support• Double submit strategies

– render redirect/ redirect to buffered response/ none• Testing support

– JUnit testing• extensive logging and error reporting

Page 9: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Features

9

• Ajax support and components

– Ajax without writing JavaScript, Dojo, Scriptaculous, ...

• Header contribution

– Javascript & CSS

• URL mounting

– pretty URLs

• Component level security

Page 10: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Apache Wicket in a Nutshell

10

• ‘Event’ based as opposed to ‘Action’ based

• Mission Statement– A Java Software Framework to enable component-

oriented, programmatic manipulation of markup

Page 11: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Model2 (Struts)

11

Client

Action

2

4

1 3

pushpushpush

response

request

Hello.jsp

Page 12: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Component Oriented (Wicket)

12

Client

Comp-1

Comp-3

Comp-2

response

request

Model

pull

pull

pull

Model

Model

HelloPage.java

Page 13: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Agenda

13

• What is Wicket

• Core concepts of Wicket

• Developing a custom component

• Q&A

Page 14: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Concepts

14

• Application

• Session

• RequestCycle

• Components

• Behaviors

• Models

Page 15: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Application

15

• Main entry point for your web application

• Configuration

– Output Wicket specific tags?

– Watch for markup changes every …?

– Defines homepage

• Factories for

– Session

– RequestCycle

– Security

– …

Page 16: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Application

16

<filter><filter-name>wicket</servlet-name><filter-class>

org.apache.wicket.protocol.http.WicketFilter</filter-class><init-param>

<param-name>applicationClassName</param-name><param-value>

com.mycompany.MyApplication</param-value>

</init-param><load-on-startup>1</load-on-startup>

</filter>

Configured in web.xml

Page 17: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Concepts

17

• Application

• Session

• RequestCycle

• Components

• Behaviors

• Models

Page 18: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Session

18

• Abstraction of a user session

• Storage typically in HttpSession

• Stores session specific data

– Locale, Client info (browser vendor and version)

– Your own data?

• Logged in user

• Shopping cart contents

– Limited page history for back button support

Page 19: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Session

19

class ApplicationSession extends WebSession {

private User loggedInUser;

public User getLoggedInUser() { … }

public void setLoggedInUser (User cart) { … }

}

appSession.setLoggedInUser(new User());

User loggedInUser = mysession. getLoggedInUser();

Permission[] permissions = loggedInUser.getPermissions();

Page 20: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Concepts

20

• Application

• Session

• RequestCycle

• Components

• Behaviors

• Models

Page 21: Wicket Tutorial

POJO Based Web Development with Apache Wicket

RequestCycle

21

• Steps in a request cycle:

– Create request cycle object

– Decode the request

– Identify request target (page, component, …)

– Process event (onClick, onSubmit, …)

– Respond (page, component, image, pdf, …)

– Clean up

Page 22: Wicket Tutorial

POJO Based Web Development with Apache Wicket

RequestCycle

22

• Two types of requests:

– Stateful

• Tied to specific user session

• Not bookmarkable

– Stateless

• Not necessarily tied to specific user session

• Bookmarkable

Page 23: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Concepts

23

• Application

• Session

• RequestCycle

• Components

• Behaviors

• Models

Page 24: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Component

24

• Ultimate super class org.apache.wicket.Component

– Label

– MultiLineLabel

– TextField

– PasswordTextField

– Image

– Link

– Tree

– BookmarkablePageLink

– Panel

– ListView

– Loop

– PagingNavigator

– ImageMap

– Button

– Ajax…

– Sorting, paging repeaters

– Wizard

– DatePicker

Page 25: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Components and Markup

25

• A component is identified in markup with wicket:id

– Html: <h1 wicket:id=“msg”>Gets replaced</h1>

– Java:new Label(“msg”, “Hello, World!”);

– Final (wicket tags can be stripped):

– <h1>Hello, World!</h1>

Page 26: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Convention over configuration

26

• Component can have its own markup file

– Page

– Panel

– Border

• Put java, markup and supporting files in same package on class path

• Can also place localization properties file in the same package

Page 27: Wicket Tutorial

POJO Based Web Development with Apache Wicket

A Page: Hello, World!

27

Page 28: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Concepts

28

• Application

• Session

• RequestCycle

• Components

• Behaviors

• Models

Page 29: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Behaviors

29

• Behaviors are plug-ins for Components• They can modify the components markup

item.add(new AbstractBehavior() {public void onComponentTag(

Component component, ComponentTag tag) {String css = (((Item)component).getIndex() % 2 == 0)

? "even" : "odd";tag.put("class", css);

}});

Output:<tr class=“odd”>…</tr><tr class=“even”>…</tr>

Page 30: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Behaviors

30

• Change attributes of your component’s markup

• Add javascript events

• Add Ajax behavior

• component.add(new AjaxSelfUpdatingBehavior(

Duration.seconds(1)));

Page 31: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Concepts

31

• Application

• Session

• RequestCycle

• Components

• Behaviors

• Models

Page 32: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Models

32

• Models bind your POJO’s to Wicket components

Label(“name”, model)

<<Person>>

+name : String+city : String

PropertyModel

Page 33: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Models

33

• Lazy binding in Java sucks

– Doesn’t update:

• new TextField(“txt”, person.getName())

– Gives null pointers:

• new Label(“street”, person.getAddress().getStreet())

• Solution: OGNL/EL like expressions

Page 34: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Models

34

• PropertyModel:

– new PropertyModel(person, “name”)

– new PropertyModel(person, “address.street”)

Page 35: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Advanced Models

35

• StringResource– Internationalization & localization

• LoadableDetachableModel– Keeps session state minimal

– Store only entity ID in session

• CompoundPropertyModel– Shared between components

– Uses component ID for OGNL traversals

Page 36: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Compound Property Model

36

form.add(new Label(“name”, new PropertyModel(person, “name”));

form.add(new Label(“birthdate”, new PropertyModel(person, “birthdate”));

form.add(new TextField(“street”, new PropertyModel(person, “address.street”)));

form.setModel(new CompoundPropertyModel(person));

form.add(new Label(“name”));

form.add(new Label(“birthdate”));

form.add(new TextField(“address.street”));

Page 37: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Ajax

37

• Out-of-the-box

– Dynamic updating of components in page

– Update multiple components in one request

– No JavaScript skills necessary

– Several Ajax components and behaviors available

• Other Projects

– Dojo integration

– Scriptaculous integration

– Yahoo! UI library integration

Page 38: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Ajax Components

38

• Wicket core

– AjaxLink

– AjaxFallbackLink

– AjaxPagingNavigator

– AjaxSubmitButton

– AjaxSubmitLink

– AjaxCheckBox

• Wicket extensions

– IndicatingAjaxLink

– AjaxEditableLabel

– AutoCompleteTextField

– UploadProgressBar

– AjaxTabbedPanel

Page 39: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Agenda

39

• What is Wicket

• Core concepts of Wicket

• Developing a custom component

• Q&A

Page 40: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Lead on Custom Components

40

Eelco Hillenius:

« Imagine being told that you can use Java as your

programming language, but at the same time

being told not to create your own classes. [...]

I fail to understand why that has to be different

for UI development, and Wicket proves it doesn't

have to be so. »

Page 41: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Wicket Lead on Custom Components

41

• Creating custom component is as easy as typing in ‘extends’

• Extends wicket.Component down the line

• Available on application classpath

Page 42: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Custom Components

42

• Panels provide grouping

– Have own markup file

– Can be exchanged in pages for other components

– Can contribute to header

– Can contain any number of components, including panels

Page 43: Wicket Tutorial

POJO Based Web Development with Apache Wicket

TogglePane HTML Markup (Designer)

43

<div id="I_D" class="pane_border"><div class="pane">

<div class="pane_header"><span>Countries</span>

</div><div class="pane_content">

<span>India, Pakistan, Australia</span></div>

</div></div>

Page 44: Wicket Tutorial

POJO Based Web Development with Apache Wicket

TogglePane – JavaScript (toggle.js)

44

function init_toggle(){var panes = $$('div#I_D .pane');panes = $A(panes);panes.each(

function(pane){var header = $A(pane.childNodes).find(

function(child){return child.className == 'pane_header';

});

var content = $A(pane.childNodes).find(function(child){

return child.className == 'pane_content';}

);header.onclick = function(){

Effect.toggle(content,'blind');};

});

}

Fetch all elements under a div with id ‘I_D’ having class ‘pane’

For each pane , look for ‘header’

and ‘content’ elements.

Attach a ‘onclick’ listener to the

‘header’ to toggle the ‘content’ when

clicked

Scriptaculous for “toggle” effect

Page 45: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Add the required scripts to the <head>

45

<head><script src="toggle.js" type="text/javascript"></script><script src="prototype.js" type="text/javascript"></script><script src="scriptaculous.js" type="text/javascript"></script><script src="effects.js" type="text/javascript"></script>

</head>

Page 46: Wicket Tutorial

POJO Based Web Development with Apache Wicket

TogglePane Custom Component

46

Designer ensures that the HTML+ JS prototype works

Page 47: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Enter Wicket : Identifying Components

47

<div id="I_D" class="pane_border"><div class="pane">

<div class="pane_header"><span>Countries</span>

</div><div class="pane_content">

<span>India, Pakistan, Australia</span></div>

</div></div>

Repeating Block

Header

Content

Page 48: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Identifying Components (attach ‘wicket:id’)

48

<wicket:panel><div id="I_D" class="pane_border">

<div class="pane“ wicket:id=“panes”><div class="pane_header">

<span wicket:id=“header”>Countries</span></div><div class="pane_content">

<span wicket:id=“content”>India, Pakistan, Australia</span></div>

</div></div></wicket:panel>

Repeating Block

Header

Content

This is a group of components – A Panel !

Page 49: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Time to Code – TogglePane Component

49

Add “Dynamic Behavior” to the markup in Wicket

Page 50: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Identifying Components for Ajax Behavior

50

<div id="I_D" class="pane_border"><div class="pane“ wicket:id=“pane”>

<div class="pane_header"><span wicket:id=“header”>Countries</span>

</div><div class="pane_content">

<span wicket:id=“content”>India,</span></div>

</div></div>

“onclick” of header

‘Repaint’ content

Page 51: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Add wicket:id in order to attach Ajax Behavior

51

<div id="I_D" class="pane_border"><div class="pane“ wicket:id=“panes”>

<div class="pane_header“ wicket:id=“h”><span wicket:id=“header”>Countries</span>

</div><div class="pane_content">

<span wicket:id=“content”>India, Pakistan, Australia</span></div>

</div></div>

Repaint Content

On click of header

Page 52: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Avoid FUD and illogical reasoning..

Q) “Why is JSF better than Wicket ? ”

A) “Well, uhhm because JSF is a standard”

http://www.theserverside.com/news/thread.tss?thread_id=45345

“I can stand brute force, but brute reason is quite unbearable”

- Oscar Wilde

Page 53: Wicket Tutorial

POJO Based Web Development with Apache Wicket

Resources

http://wicket.apache.org

• ##wicket

– irc.freenode.net

Books:

Pro Wicket, Karthik GurumurthyWicket in Action , Eelco Hillenius & Martijn DashorstEnjoying Web Development with Wicket, Kent Tong