Vous êtes sur la page 1sur 26

Wicket Introduction

Nino Martinez

Web development with Java is boring & slow

NOT TRUE!!!!!! Welcome Apache Wicket

About me (wicket specific)


I work @ jayway, a Java specialist house Contributor on the following Wicketstuff projects:

Wicket contrib input events Wicket contrib Openlayers Wicket contrib BBCode Wicket contrib Gmap Wicketstuff minis Wicketstuff Iolite More in progress!

Have worked very intensely with wicket for the last 3 years Written several articles / screen casts on misc Wicket topics @ ninomartinez.wordpress.com

Teaser
EASY (SIMPLE / CONSISTENT / OBVIOUS)

POJO-centric

All code written in Java ala Swing Avoid overuse of XML configuration files Minimum reliance on special tools Components, containers and conventions should be consistent Components written in Wicket should be fully reusable Reusable components should be easily distributed in ordinary JAR files HTML or other markup not polluted with programming semantics Compatible with any ordinary HTML editor Code is secure by default Easy to integrate with Java security Efficient and lightweight, but not at the expense of other goals Clustering through sticky sessions preferred Clustering via session replication is easy to accomplish.

REUSABLE

NON-INTRUSIVE

SAFE

EFFICIENT / SCALABLE

Wicket Introduction
Content When we go over the sections below we are going to implement a very basic blog application
History Theory Web-Application Components Behavior & Panel Markup inheritance Internationalization (i18n) Forms (input and validation) Testing Ajax Beyond basics Additional Resources

History of Wicket
Wicket was conceived in Jonathan Locke's study back in 2004, shortly after open sourced at Codehaus, then sourceforge and in 2006 it incubated to a real Apache project. Top level project at Apache today The name wicket is not the little guy from Star Wars but a term from cricket meaning a small framework at which the bowler aims the ball.

Theory
Apache Wicket is a object oriented stageful web framework, working with it are just plain java! Mapping between html tags and wicket components One .html has one .java (almost always true) Single threaded(session scope) request cycle as standard

Sample Code
Java: Label label=new Label(label,hello world); Html: <span wicket:id=label></span> Result: <span>hello World</span>

Theory
Models?

Models hold data is a container different types (compound,property,detachable, resource)

Sample Code Java: Label label=new Label(label,new Model(new HelloObject())); Html: <span wicket:id=label></span> Result: <span>hello World</span>

Maven Archetype: Wicket Iolite


Simple stack archetype

Has spring setup for you with JPA and hibernate

mvn archetype:generate -DarchetypeGroupId=org.wicketstuff.iolite -DarchetypeArtifactId=wicketstuff-iolite -DarchetypeVersion=0.3-SNAPSHOT -DarchetypeRepository=http://wicketstuff.org/maven/repository -DgroupId=com.mycompany -DartifactId=myproject -Dpackage=com.mycompany -Dversion=1

Demo Setup
What did I do Run archetype generation

Domain Logic Stylesheet Edited base page, to provide a basic layout Two module project

Import project to Eclipse

Lets create the basic structure for a blog application

Web-Application
A Wicket web application consists of

a class that extends webapplication a corresponding web.xml which points at the webapplication some pages Homepage (default page redirected to) Session (session object could be used for authentication) Mounted urls Spring injection Lots more can happen here.

Holds configuration

Components
Page Label ListView Link Wicket Core, Wicket Extensions, Wicketstuff If a component contains children you must add them to the component, for example a form. Hierarchy is the same as DOM / HTML

Code time:)
Let's create a Listview iterating over authors and posts

Behavior & Panel


Panel, your way to do own components Behavior, a way to modify components by adding extra javascript etc... MyPanel.html:
<wicket:panel> <span wicket:id=time></span> <span wicket:id=name></span> </wicket:panel> MyPanel.java: public MyPanel(IModel nameModel) { add(new Label(time,new Model(System.currentTimeMillis())); add(new Label(name,nameModel)); }

Markup Inheritance
Wicket supports markup inheritance Which means that a page can extend another page Usage (java side we know using the extends keyword)

encapsulate your markup with

Subclass: <wicket:extend></wicket:extend> Superclass: <wicket:child />

Code time:)
Lets see how it's used in our blog application.

Internationalization
Resource models Property Files
Just use standard java I18n naming of property files Examples: myPage.properties myPage_da.properties myPage_th.xml

Usage
In page or panel html part

<wicket:message key="mykey">[text]</wicket:message> matching property le: mykey=mytext

Forms
Validation

Required More, or roll your own? Radio Check List Text Palette ...

Controls

Code time:)
Time for authentication, lets do a Login form on the login page And do the AuthorPage with a form so we can post posts.

Testing
Wicket provides some simple facilities to make tests

Wicket Tester

Simulates Http requests Allows you to make wicket specific assertions Allows you to manipulate forms

Wicket FormTester

Code time:)
Let's examine a test case that tests our LoginPage & AuthorPage flow..

Ajax
Adding Ajax to wicket is a breeze, just add a ajaxBehavior and thats it. Sometimes though you can use one of the Ajax controls instead. Sample Code
AjaxButton ajaxBtn = new AjaxButton("ajaxButton") { @Override protected void onSubmit(AjaxRequestTarget target, Form form) { } @Override protected void onError(AjaxRequestTarget target, Form form) { target.addComponent(feedbackPanel); } };

Code time:)
Time to Ajax-ify our home page, let's refresh our Author/post overview at a given interval.

Beyond Basics, But Nice to know


Authorization

Controlling visibility at component level for roles

Protecting against CSRF, with CryptedUrlWebRequestCodingStrategy, set it in WebApplication.java IVisitor, visit your components. Good for visiting multiple children and perhaps modifying them, like for example changing styles based on validation etc.

Additional Resources Going further


Wicket In Action (Book) Jayway Wicket Course, 2 days or 3 days http://cwiki.apache.org/WICKET/ Wicket User Mailinglist users@wicket.apache.org (very friendly and helpful community) Maven2 archetypes (fast way to get started )

Quickstart (official), sets up wicket naked.. WicketStuff Iolite (wicketstuff), sets up wicket with JPA(Hibernate) and Spring

http://wicketstuff.org/ WUG DK 15 October @ jayway office

Vous aimerez peut-être aussi