Apache Wicket Web Framework

25
Apache Wicket Java Web Application Framework Luther Baker • September 2009 St. Louis - Java User’s Group

description

Apache Wicket Web Framework Presentation

Transcript of Apache Wicket Web Framework

Page 2: Apache Wicket Web Framework

• Web Application Framework

• Component-based Framework

• Wicket 1.4 is Java 1.5+ compliant

What is Wicket?

Page 3: Apache Wicket Web Framework

Where does Wicket fit?

Presentation Tier (Wicket)

Business Tier

Integration Tier

SharedObjects

User

Page 4: Apache Wicket Web Framework

Request / Response

request

responsebrowser

JSP Request / Response

server

homejsp

Page 5: Apache Wicket Web Framework

Model 2 (MVC)

homejsp

request

responsehome

controller

browser server

Struts, Spring MVC, Stripes

Page 6: Apache Wicket Web Framework

Advantages of R/R

• Rendering views is generally quite fast

• Development can leverage existing tag libraries

• Recruiting developers may be easier

• Modern implementations have good support for DI and IoC frameworks

Page 7: Apache Wicket Web Framework

Disadvantages of R/R• Controller implementations must explicitly

consider multiple concurrent users and threads

• Controllers generally work literally in terms of HTTP requests and responses

• Controllers often explicitly manage state

• Not strictly Object Oriented

• The programming model is skewed

Page 8: Apache Wicket Web Framework

The Impedance Mismatch

• Programming in Java - do we regularly focus on how the JVM manages object instances and variables?

• Generally, website development requires an understanding of the HTTP protocol.

IE: manually managing state within and across requests forces front end handlers to be protocol specific.

The Programming Model

Page 9: Apache Wicket Web Framework

What if ... ?

• What if we considered a page ... a Page?

• What if we considered a button ... a Button?

• And upon clicking a button, handled an onClick event?

• What if web-development resembled Swing or event-driven development?

• What kind of framework could possibly enable this?!

Page 10: Apache Wicket Web Framework

Enter ... Wicket

• Component-based framework

• Instead of creating a controller, servlet or action class ... create a Page

• Place components on said Page and define how each component reacts to user input

• Build the Page in Java to manage HTML page elements ... not the other way around

Page 11: Apache Wicket Web Framework

The Component Model

• Graphic elements are laid out in HTML while their actual representation, behavior and implementation is defined in Java

• A DOM style parent/child approach

• An event-driven programming model

• You wonder ...“How can such an abstraction sit on top of HTTP?”

The Underlying Abstraction

Page 12: Apache Wicket Web Framework

Web App Configweb.xml

<web-app>

<context-param> <param-name>configuration</param-name> <param-value>development</param-value> </context-param>

<filter> <filter-name>WebApplication</filter-name> <filter-class> org.apache.wicket.protocol.http.WicketFilter </filter-class> <init-param> <param-name>applicationClassName</param-name> <param-value>mypackage.HelloWorldApplication</param-value> </init-param> </filter>

<filter-mapping> <filter-name>WebApplication</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

</web-app>

Page 13: Apache Wicket Web Framework

Wicket ConfigWicketApplication.java

package mypackage;

import org.apache.wicket.protocol.http.WebApplication;

public class HelloWorldApplication extends WebApplication{ public Class getHomePage() { return HelloWorld.class; }}

Page 14: Apache Wicket Web Framework

General Structure

• Layout the element hierarchy

• Style the elements

Markup (HTML’s Role)

Code (Java’s Role)

Properties Files (~Auxiliary Roles)

• Mirror and implement markup’s element hierarchy

• Event handling

• Literal strings and i18n

Page 15: Apache Wicket Web Framework

Hello World<html> <body> <span wicket:id="message">Message goes here!</span> </body></html>

Markup

Javaimport org.apache.wicket.markup.html.WebPage;import org.apache.wicket.markup.html.basic.Label;

public class HelloWorld extends WebPage{ public HelloWorld() { add(new Label("message", "Hello World!")); }}

Page 16: Apache Wicket Web Framework

Forms (HTML)

Markup

<html> <body> <span wicket:id="message">Message goes here</span> <form wicket:id="messageInputForm"> <input type="text" wicket:id="messageInput"/> <input type="submit" value="update"/> </form> </body></html>

Page 17: Apache Wicket Web Framework

Forms (Java)Java

public class HelloWorld extends WebPage{ public HelloWorld() { IModel messageModel = new Model("Hello World!"); add(new Label("message", messageModel)); add(new MessageForm("messageInputForm", messageModel)); }

private final class MessageForm extends Form { public MessageForm(String id, IModel model) { super(id); add(new TextField("messageInput", model)); }

protected void onSubmit() { // nothing to do here as the model is automatically updated } }}

Page 18: Apache Wicket Web Framework

Component Family

WebPagePanel

Link

AjaxLink

Page

Button

FormTextField

TextAreaCheckbox

DropDown

Label ...many more

ComponentWebComponent

Page 19: Apache Wicket Web Framework

Super Models

• Static Model

• Dynamic Model

• Property Model

• Compound Property

• Loadable Detached

Page 20: Apache Wicket Web Framework

Basic Modelspublic class HelloWorld extends WicketExamplePage{ public HelloWorld() { add(new Label ("name", new Model(person.getName()))); }}

personForm.add(new RequiredTextField("personName", new IModel<String>(){ @Override public String getObject() { return person.getName(); }

@Override public void setObject(String value) { person.setName(value); }}));

Static Model

Dynamic Model

Page 21: Apache Wicket Web Framework

More Models

public PropertyModel(final Object modelObject, final String expression)

class Person{ private Address address;

public Address getAddress() { return name; } ...}

personForm.add(new RequiredTextField("zip", new PropertyModel<Person>(person, "address.zip")));

Property Model

class Address{ private String zip;

public String getZip() { return zip; } ...}

Page 22: Apache Wicket Web Framework

Demonstration

• JavaSubclass Master Page

• HTMLchild & parent tags

WebPage Component

Custom Component

Event Handling

• Java, HTML

• Panel Superclass• Child components

• Tags to include

• Original HTML

• Generated Javascript• Ajax (Debug)

• Submit handling

Page 23: Apache Wicket Web Framework

Ideally Suited For ...• Highly interactive apps

• Help-Desk style ticketing applications and online Registration applications

• Apps with lots of Forms and/or UI controls

• Apps requiring seamless Ajax behavior

• Apps simulating thick clients

• Anytime an event programming model better suites the problem domain

Page 24: Apache Wicket Web Framework

Deeper Dive

• An easy way to start with a simple, Mavenized, Wicket project

Maven Quickstart Archetype

• More on Models, Sessions and Security

• Unit Testing

• Custom components

• URL mapping, IoC Integration, Persistence ...

Other Topics

Page 25: Apache Wicket Web Framework

Wicket Resources

• http://wicket.apache.org/

• http://wicketstuff.org/

• http://cwiki.apache.org/WICKET/

• Wicket in Action (Manning)

• Pro Wicket (Apress)

Wicket Links

Wicket Books