Wicket Explained Ease your way into Ease your way into Component - - PowerPoint PPT Presentation

wicket explained
SMART_READER_LITE
LIVE PREVIEW

Wicket Explained Ease your way into Ease your way into Component - - PowerPoint PPT Presentation

Wicket Explained Ease your way into Ease your way into Component Based Web Application Component Based Web Application Development Development Goals Explain why CBD is a good fit for the web tier Show the basics of a Wicket


slide-1
SLIDE 1

Wicket Explained

Ease your way into Ease your way into Component Based Web Application Component Based Web Application Development Development

slide-2
SLIDE 2

Goals

  • Explain why CBD is a good fit for the web

tier

  • Show the basics of a Wicket application:

pages, components and models

  • See how web application development

doesn’t have to be difficult

slide-3
SLIDE 3

Agenda

  • Introduction
  • Java web frameworks
  • Applications, pages, components & models
  • 10 minute workout
  • Processing Input
  • Summary & Questions
slide-4
SLIDE 4

What is Wicket?

  • Open Source (Apache 2 license)
  • Started in 2004 by Jonathan Locke
  • Component Oriented
  • Clean separation of Markup and Code
  • If you know Java and HTML, then you

already know a lot about Wicket

  • Programmer freedom
slide-5
SLIDE 5

Agenda

  • Introduction
  • Java web frameworks
  • Applications, pages, components & models
  • 10 minute workout
  • Processing Input
  • Summary & Questions
slide-6
SLIDE 6

Java web frameworks

  • CGI/ Servlets;
  • Servlets combined with templating

– Webmacro, Velocity

  • JSP (‘model 1’ – usebean)
  • ‘Model 2’ a.k.a. ‘Web MVC’

– Struts, WebWork, SpringMVC, Maverick, etc. – Request oriented

  • Component based
slide-7
SLIDE 7

Keeping state?

  • Wicket has stateful components
  • REST (Representational State Transfer) vs

Stateful programming

– Stateless service layers: are we back to procedural programming once again? – Cost of development hours – Processor & bandwidth vs memory – Do your pages really just do one thing?

  • Keeping state gets you:

– Self contained components – Easy and elegant programming

slide-8
SLIDE 8

Wicket's Goals

  • Bring OO back to the web tier

– Let developers focus on structure instead of

  • peration
  • Make reuse (using and providing) easy
  • Separate concerns

– HTML for presentation

  • Minimal or no scripting

– Java for state and Model/ Control

  • Strongly typed
  • Use your Java IDE
  • Easy = productive and maintainable
slide-9
SLIDE 9

Agenda

  • Introduction
  • Java web frameworks
  • Applications, pages, components & models
  • 10 minute workout
  • Processing Input
  • Summary & Questions
slide-10
SLIDE 10

Applications

  • Application defines:

– Pages:

  • home page
  • error/stale page

– Settings

  • development mode
  • deployment mode

– Request Cycle strategy – Factories for:

  • resources
  • sessions
  • converters
slide-11
SLIDE 11

Pages

  • Pages group your components
  • Pages can inherit from other pages

– The markup as well -> template base page!

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1>

slide-12
SLIDE 12

Pages

  • Markup and Java can be next to each other

– Default: in same directory on classpath

  • allows packaging of components (jar drop-ins)
  • easy access while developing

page class page markup

slide-13
SLIDE 13

Components

  • Everything is a component in Wicket

– A component has a wicket:id – A component has markup associated – A component is a Java class

  • in markup: wicket:id
  • in Java: Component.id

<span wicket:id=“foo”> </span> new Label(“foo”, “bar”); Markup Java

slide-14
SLIDE 14

Components

  • Everything is a component in Wicket

– A component has a wicket:id – A component has markup associated

  • Components can be nested

row

col col col col col

row

col col col col col

listview ‘col’

slide-15
SLIDE 15

Components

  • Everything is a component in Wicket

– A component has a wicket:id – A component has markup associated

  • Components can be nested

row

col col col col col

row

col col col col col

listview ‘row’

slide-16
SLIDE 16

Components

  • Wicket supports all basic HTML tags

– <a href> – <input type=“XXX”> – <form> – <img>, <map> – <frame>

  • Components support JavaScript, CSS

– contribute to header – local

slide-17
SLIDE 17

Components

  • Components are reusable

– address input form, – search panel, – paging navigator for listview, – date picker

slide-18
SLIDE 18

Components

  • Components are reusable

– address input form, – search panel, – paging navigator for listview, – date picker

  • Components are JAR-able

– package Java, HTML, CSS, images, JavaScript – put JAR in classpath – use component in your application

slide-19
SLIDE 19

Component Examples (1/3)

Simple Simple label component label component

HTML markup: <span wicket:id=“message”>contents go here</span> Java code: new Label(“message”, “Hello, World!”);

slide-20
SLIDE 20

Component Examples (2/3)

Simple Simple input input field component field component

HTML markup: <input wicket:id=“name” type=“text” value=“xxxx” /> Java code: new TextField(“name”)

slide-21
SLIDE 21

Component Examples (3/3)

Form with nested field Form with nested field

HTML markup: <form wicket:id=“customerForm”> <input wicket:id=“zipCode” type=“text” value=“xxxx” /> </form> Java code: Form form = new Form(“customerForm”, customer); form.add(new TextField(“zipcode”, new PropertyModel(customer, “zipCode”));

slide-22
SLIDE 22

Models

  • Models bind your POJO’s to Wicket

components

  • Models are the ‘brains’ of your app

Label() <<Person>> + name : String + city : String <<Model>> wicket POJO’s

slide-23
SLIDE 23

Models

  • Basic models:

– Model – PropertyModel – AbstractDetachableModel

  • ‘Advanced’ models:

– LoadableDetachableModel – CompoundPropertyModel – BoundCompoundPropertyModel – StringResourceModel

slide-24
SLIDE 24

Model examples

Simple model: Simple model:

add(new Label("message", "Hello World!"));

slide-25
SLIDE 25

Model examples

Binding to POJO: Binding to POJO:

add(new Label(“name”, new PropertyModel(person, “name”))); <span wicket:id=“name”>will be replaced</span>

slide-26
SLIDE 26

Model examples

Using ID’s to bind components to properties: Using ID’s to bind components to properties:

form = new Form(“person”, new CompoundPropertyModel(person)); form.add(new TextField(“name”)); form.add(new TextField(“city”));

slide-27
SLIDE 27

Agenda

  • Introduction
  • Java web frameworks
  • Applications, pages, components & models
  • 10 minute workout
  • Processing Input
  • Summary & questions
slide-28
SLIDE 28

10 minute workout

slide-29
SLIDE 29

10 minute workout

slide-30
SLIDE 30

Mocking Markup

Page <table> link (<a href>) <h1>

slide-31
SLIDE 31

Mocking Markup

Page <table> rows (<tr>) link (<a href>) <h1>

slide-32
SLIDE 32

Mocking Markup

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1>

slide-33
SLIDE 33

Mocking Markup

<html> ... <body> <h1>Addresses</h1> <table> <!-- heading --> <tr> <td><span>John Doe</span></td> <td><span>555-5555</span></td> <td><span>friend</span></td> </tr> </table> </body> </html>

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1>

slide-34
SLIDE 34

Mocking Markup

<html> ... <body> <h1>Addresses</h1> <table> <!-- heading --> <tr wicket:id=“rows”> <td><span>John Doe</span></td> <td><span>555-5555</span></td> <td><span>friend</span></td> </tr> </table> </body> </html>

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1>

slide-35
SLIDE 35

Mocking Markup

<html> ... <body> <h1>Addresses</h1> <table> <!-- heading --> <tr wicket:id=“rows”> <td><span wicket:id=“name”>John Doe</span></td> <td><span wicket:id=“telnr”>555-5555</span></td> <td><span wicket:id=“type”>friend</span></td> </tr> </table> </body> </html>

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1>

slide-36
SLIDE 36

From Markup to Java

public class public class AddressPage extends extends WebPage { public public AddressPage() { } }

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1>

slide-37
SLIDE 37

From Markup to Java

public class public class AddressPage extends extends WebPage public public AddressPage() { add(new new AddressListView(“rows”, addresses)); } public class public class AddressListView extends extends ListView { public public AddressListView(String name, IModel model) { super super(name, model); } } }

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1>

slide-38
SLIDE 38

From Markup to Java

public class public class AddressPage extends extends WebPage public public AddressPage() { add(new new AddressListView(“rows”, addresses)); } public class public class AddressListView extends extends ListView { public public AddressListView(String name, IModel model) { super super(name, model); } protected void populateItem(ListItem item) { item.add(new Label(“name”)); item.add(new Label(“telnr”)); item.add(new Label(“type”)); } } }

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1>

slide-39
SLIDE 39

Application

public class AddressApplication extends WebApplication { public AddressApplication() { super(); getPages().setHomePage(AddressPage.class); } }

slide-40
SLIDE 40

But where’s the XML?

  • No extra XML configuration necessary
  • Just web.xml

<web-app> <display-name>Wicket Addressbook</display-name> <servlet> <servlet-name>address</servlet-name> <servlet-class>wicket.protocol.http.WicketServlet</servlet-class> <init-param> <param-name>applicationClassName</param-name> <param-value>wicket.examples.AddressApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>address</servlet-name> <url-pattern>/app/*</url-pattern> </servlet-mapping> </web-app>

slide-41
SLIDE 41

Summary

  • Create markup
  • Identify components
  • Assign wicket:id’s to components
  • Create Java class (extends WebPage)
  • Add components to Page
  • Define Application
  • Add wicket servlet to

web.xml

slide-42
SLIDE 42

10 minute workout

slide-43
SLIDE 43

Adding behaviour

  • Add delete link to each item

Page <table> rows (<tr>)

name telnr f/f

link (<a href>) <h1> delete

slide-44
SLIDE 44

Mocking Markup

... <tr> <td><span wicket:id=“name”>John Doe</span></td> <td><span wicket:id=“telnr”>555-555</span></td> <td><span wicket:id=“type”>friend</span></td> <td><a href=“#”>delete</a></td> </tr>

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

slide-45
SLIDE 45

Add Wicket ID

... <tr> <td><span wicket:id=“name”>John Doe</span></td> <td><span wicket:id=“telnr”>555-555</span></td> <td><span wicket:id=“type”>friend</span></td> <td><a wicket:id=“delete” href=“#”>delete</a></td> </tr>

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

slide-46
SLIDE 46

Add Link component

protected void populateItem(ListItem item) { item.add(new Label(“name”)); item.add(new Label(“telnr”)); item.add(new Label(“type”)); item.add(new Link(“delete”)); }

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

<a wicket:id=“delete” href=“#”>delete</a>

slide-47
SLIDE 47

Add onClick()

protected void populateItem(ListItem item) { item.add(new Label(“name”)); item.add(new Label(“telnr”)); item.add(new Label(“type”)); item.add(new Link(“delete”) { public void onClick() { Object addr = getParent().getModelObject(); AddressDAO.delete((Address)addr); } } }

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

slide-48
SLIDE 48

Summary

  • Added link to markup
  • Added wicket:id to link tag
  • Added link component to listitem
  • Added onClick() behaviour to link
slide-49
SLIDE 49

10 minute workout

slide-50
SLIDE 50

Adding paging to the list

  • Add paging to the list

Page

<table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

slide-51
SLIDE 51

Adding paging to the list

  • Add paging to the list

Page

<table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

navigator

slide-52
SLIDE 52

Mocking Markup

... <td><span wicket:id=“telnr”>555-555</span></td> <td><span wicket:id=“type”>friend</span></td> <td><a wicket:id=“delete” href=“#”>delete</a></td> </tr> <tr> <td colspan=“4”> <span>NAVIGATOR</span> </td> </tr> </table>

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

navigator

slide-53
SLIDE 53

Assign Wicket ID

... <td><span wicket:id=“telnr”>555-555</span></td> <td><span wicket:id=“type”>friend</span></td> <td><a href=“#”>delete</a></td> </tr> <tr> <td colspan=“4”> <span wicket:id=“navigator”>NAVIGATOR</span> </td> </tr> </table>

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

navigator

slide-54
SLIDE 54

Make ListView pageable

public class AddressPage extends WebPage { public AddressPage() { add(new AddressListView(“rows”, addresses)); } public class AddressListView extends PageableListView PageableListView { public AddressListView(String name, IModel model) { super(name, model, 2 , 2); } } }

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

navigator

number of items per page

slide-55
SLIDE 55

Add Navigator component

public class AddressPage extends WebPage { public AddressPage() { PageableListView lv = new AddressListView(“rows”,addresses); add(lv); add(new PagingNavigator(“navigator”, lv)); } ... }

Page <table> rows (<tr>) name telnr f/f link (<a href>) <h1> del

navigator

slide-56
SLIDE 56

Summary

  • Add navigator to markup
  • Add wicket:id to navigator tag (span)
  • Change ListView into PageableListView
  • Add Navigator component to Page
  • That’s it!
slide-57
SLIDE 57

10 minute workout

  • Created ‘complex’ page
  • ~ 10 minutes!
  • Just Java and HTML
  • No weird stuff in HTML
  • No weird XML configurations
slide-58
SLIDE 58

Agenda

  • Introduction
  • Java web frameworks
  • Applications, pages, components & models
  • 10 minute workout
  • Processing Input

– Forms – Validation

  • Summary & Questions
slide-59
SLIDE 59

fieldset

Processing Input

h1 form input input input select submit Name: Area code: Phone num: Friend or :

slide-60
SLIDE 60

fieldset

Processing Input

h1 form input input input select submit Name: Area code: Phone num: Friend or :

slide-61
SLIDE 61

fieldset

Processing Input

h1 form input input input select submit Name: Area code: Phone num: Friend or :

slide-62
SLIDE 62

... <body><h1>Add New Address</h1> <fieldset><legend>Person Data</legend> <form> ... <th>Name:</th> <td><input type=“text” /></td> ... <th>Area code:</th> <td><input type=“text” /></td> ... <td><input type=“submit” /></td> ... </form>

Mocking Markup

fieldset h1 form input input input select submit Name: Area: Phone: Friend or :

slide-63
SLIDE 63

... <body><h1>Add New Address</h1> <fieldset><legend>Person Data</legend> <form wicket:id=“form”> ... <th>Name:</th> <td><input type=“text” /></td> ... <th>Area code:</th> <td><input type=“text” /></td> ... <td><input type=“submit” /></td> ... </form>

fieldset h1 form input input input select submit Name: Area: Phone: Friend or :

Assign Wicket ID

slide-64
SLIDE 64

... <body><h1>Add New Address</h1> <fieldset><legend>Person Data</legend> <form wicket:id=“form”> ... <th>Name:</th> <td><input wicket:id=“name” type=“text” /></td> ... <th>Area code:</th> <td><input wicket:id=“areacode” type=“text” /></td> ... <td><input type=“submit” /></td> ... </form>

fieldset h1 form input input input select submit Name: Area: Phone: Friend or :

Assign Wicket ID

slide-65
SLIDE 65

From Markup to Java

public class AddPersonPage extends WebPage { public AddPersonPage() { } }

fieldset h1 form input input input select submit Name: Area: Phone: Friend or :

slide-66
SLIDE 66

From Markup to Java

public class AddPersonPage extends WebPage { public AddPersonPage() { add(new EditPersonForm(“form”, new Person())); } public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, new CompoundPropertyModel(p)); } } }

fieldset h1 form input input input select submit Name: Area: Phone: Friend or :

slide-67
SLIDE 67

From Markup to Java

public class AddPersonPage extends WebPage { public AddPersonPage() { add(new EditPersonForm(“form”, new Person())); } public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, new CompoundPropertyModel(p)); add(new TextField(“name”)); add(new TextField(“areacode”)); ... } } }

fieldset h1 form input input input select submit Name: Area: Phone: Friend or :

slide-68
SLIDE 68

From Markup to Java

public class AddPersonPage extends WebPage { public AddPersonPage() { add(new EditPersonForm(“form”, new Person())); } public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { ... } protected void onSubmit() { Person p = (Person)getModelObject(); PersonDAO.save(p); } } }

fieldset h1 form input input input select submit Name: Area: Phone: Friend or :

slide-69
SLIDE 69

Summary

  • Created HTML mockup
  • Assigned Wicket ID’s
  • Created Java page
  • Created Form class
  • Added fields to form
  • Implemented onSubmit
  • Done
slide-70
SLIDE 70

Validation

  • Many default validators available

– Type (Date, Integer, etc) – RequiredValidator

  • Localization

– Localized parsing of values – Localized messages using resource bundles

  • How does it work?

– Automatic validation on form submit – Beans get populated on valid submission – Form.onSubmit is called after population

slide-71
SLIDE 71

fieldset

Adding Feedback

h1 form input input input select submit Name: Area code: Phone num: Friend or :

slide-72
SLIDE 72

fieldset

Adding Feedback

h1 form

input input input select submit Name: Area code: Phone num: Friend or :

feedback

slide-73
SLIDE 73

Adding Feedback

... <body><h1>Add New Address</h1> <span wicket:id=“feedback”></span> <fieldset><legend>Person Data</legend> <form> ... <th>Name:</th> <td><input type=“text” /></td> ... <th>Area code:</th> <td><input type=“text” /></td> ... <td><input type=“submit” /></td> ... </form>

fieldset h1 form input input input select submit Name: Area: Phone: Friend or : feedback

slide-74
SLIDE 74

Adding Feedback

public class AddPersonPage extends WebPage { public AddPersonPage() { add(new EditPersonForm(“form”, new Person())); } public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, p); ... } } }

fieldset h1 form input input input select submit Name: Area: Phone: Friend or : feedback

slide-75
SLIDE 75

Adding Feedback

public class AddPersonPage extends WebPage { public AddPersonPage() { FeedbackPanel fb = new FeedbackPanel(“feedback”) add(fb); add(new EditPersonForm(“form”, new Person(), fb)); } public class EditPersonForm extends Form { public EditPersonForm(..., IFeedback fb) { super(..., fb); ... } } }

fieldset h1 form input input input select submit Name: Area: Phone: Friend or : feedback

slide-76
SLIDE 76

Adding Validation

  • Make ‘name’ required
  • Make ‘areacode’ between 200 and 999

public class EditPersonForm extends Form { public EditPersonForm(..., IFeedback feedback) { super(..., feedback); add(new TextField(“name”)); add(new TextField(“areacode”)); ...

slide-77
SLIDE 77

Adding Validation

  • Make ‘name’ required
  • Make ‘areacode’ between 200 and 999

public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, new CompoundPropertyModel(p)); add(new RequiredTextField(“name”)); add(new TextField(“areacode”)); ...

slide-78
SLIDE 78

Adding Validation

  • Make ‘name’ required
  • Make ‘areacode’ between 200 and 999

public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, new CompoundPropertyModel(p)); add(new RequiredTextField(“name”)); add(new TextField(“areacode”)); ...

AddPersonPage.properties:

form.name.RequiredValidator=${name} is required

slide-79
SLIDE 79

Adding Validation

  • Make ‘name’ required
  • Make ‘areacode’ between 200 and 999

public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, new CompoundPropertyModel(p)); add(new RequiredTextField(“name”)); add(new TextField(“areacode”)); ...

AddPersonPage.properties:

form.name.RequiredValidator=${name} is required

slide-80
SLIDE 80

Adding Validation

  • Make ‘name’ required
  • Make ‘areacode’ between 200 and 999

public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, new CompoundPropertyModel(p)); add(new RequiredTextField(“name”)); add(new TextField(“areacode”).add( IntegerValidator.range(200,999)));

AddPersonPage.properties:

form.name.RequiredValidator=${name} is required

slide-81
SLIDE 81

Adding Validation

  • Make ‘name’ required
  • Make ‘areacode’ between 200 and 999

public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, new CompoundPropertyModel(p)); add(new RequiredTextField(“name”)); add(new TextField(“areacode”).add( IntegerValidator.range(200,999)));

AddPersonPage.properties:

form.name.RequiredValidator=${name} is required form.areacode.IntegerValidator=${input} is not between ${min} and ${max}

slide-82
SLIDE 82

Adding Validation

  • Make ‘name’ required
  • Make ‘areacode’ between 200 and 999

public class EditPersonForm extends Form { public EditPersonForm(String name, Person p) { super(name, new CompoundPropertyModel(p)); add(new RequiredTextField(“name”)); add(new TextField(“areacode”));

AddPersonPage.properties:

form.name.RequiredValidator=${name} is required

slide-83
SLIDE 83

Summary

  • Add feedback to page
  • Add validation to fields
  • Create page.properties file
  • Add validation message to properties file
  • Done
slide-84
SLIDE 84

Conclusions

  • Component based web development is the

future

  • Wicket is

– Easy – Smart – Pretty – Fun

  • All HTML and Java
  • No complicated configuration files
slide-85
SLIDE 85

Questions?

slide-86
SLIDE 86

Links

  • http://wicket.sourceforge.net
  • http://wicket-stuff.sourceforge.net
slide-87
SLIDE 87

Web MVC

  • Current ‘de-facto standard’ pattern
  • One front controller, Actions (a.k.a.

Commands) and a template technology

  • URL – action mapping
  • Each action can reference multiple views
  • Actions process input and prepare a View.
slide-88
SLIDE 88

Problems with Web MVC (1)

  • Bad support for complexity, like

– Pages with independent UI elements – UI elements that span multiple pages

  • Commonly ‘solved’ by:

– Command chaining – Filter layering – Uncontrolled, ah-hoc HttpSession usage – Interceptors

slide-89
SLIDE 89

Problems with Web MVC (2)

  • No reuse.

– Every project has to start from scratch

  • No library for e.g. address panels, search/results

panels, etc.

– Even within projects a lot of copy ‘n paste

  • Reusing pages or panels for application variant
  • Inheriting behavior by extending gets messy

– No 3rd party components

slide-90
SLIDE 90

Problems with Web MVC (3)

  • Encourages bad programming practices

– Thinking ‘how’ instead of ‘what’

  • Operations vs. Components as HLS call it

– Monolithic commands with copy ‘n paste code – Display hacks all over

  • Java fragments in JSPs
  • Velocity tools and other ‘utility classes’

– Framework layering.

  • Filters for opening/ closing Hibernate sessions,

cleaning up session state, etc

  • Navigation
  • Composition
slide-91
SLIDE 91

Problems with Web MVC (4)

  • Scales bad for development

– URL - Command/ view mapping

  • One or a few large xml files

– Hard to find stuff – Team update conflicts

  • Combine Command code, mapping and view code

to understand what is happening

– Did I mention framework layering?

– Fragile to configuration typos – Copy ‘n paste code is a maintenance problem – Lack of reuse gives less team partitioning

  • ptions