Wicket Basic

download Wicket Basic

of 26

Transcript of Wicket Basic

  • 8/3/2019 Wicket Basic

    1/26

  • 8/3/2019 Wicket Basic

    2/26

    Web development

    with Java is boring & slow

  • 8/3/2019 Wicket Basic

    3/26

    NOT TRUE!!!!!!

    Welcome Apache Wicket

  • 8/3/2019 Wicket Basic

    4/26

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

    Written several articles / screen casts on misc Wickettopics @ ninomartinez.wordpress.com

  • 8/3/2019 Wicket Basic

    5/26

    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

    REUSABLE Components written in Wicket should be fully reusable

    Reusable components should be easily distributed in ordinary JAR files

    NON-INTRUSIVE HTML or other markup not polluted with programming semantics

    Compatible with any ordinary HTML editor

    SAFE Code is secure by default

    Easy to integrate with Java security

    EFFICIENT / SCALABLE Efficient and lightweight, but not at the expense of other goals

    Clustering through sticky sessions preferred

    Clustering via session replication is easy to accomplish.

  • 8/3/2019 Wicket Basic

    6/26

    Wicket Introduction

    Content

    When we go over the sections below we are going toimplement a very basic blog application

    History

    Theory

    Web-ApplicationComponents

    Behavior & Panel

    Markup inheritance

    Internationalization (i18n)Forms (input and validation)

    Testing

    Ajax

    Beyond basics

    Additional Resources

  • 8/3/2019 Wicket Basic

    7/26

    History of Wicket

    Wicket was conceived in Jonathan Locke's study back in2004, shortly after open sourced at Codehaus, thensourceforge and in 2006 it incubated to a real Apacheproject.

    Top level project at Apache today

    The name wicket is not the little guy from Star Warsbut a term from cricket meaning a small framework atwhich the bowler aims the ball.

  • 8/3/2019 Wicket Basic

    8/26

    Theory

    Apache Wicket is a object oriented stageful webframework, 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 asstandard

    Sample Code

    Java:

    Label label=new Label(label,helloworld);

    Html:

    Result:

    hello World

  • 8/3/2019 Wicket Basic

    9/26

    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:

    Result:

    hello World

  • 8/3/2019 Wicket Basic

    10/26

    Maven Archetype: Wicket Iolite

    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

    Simple stack archetype Has spring setup for you with JPA and hibernate

    http://wicketstuff.org/maven/repositoryhttp://wicketstuff.org/maven/repository
  • 8/3/2019 Wicket Basic

    11/26

    Demo Setup

    What did I doRun archetype generation Domain Logic

    Stylesheet

    Edited base page, to provide a basic layout

    Import project to Eclipse Two module project

    Lets create the basic structure for a blog application

  • 8/3/2019 Wicket Basic

    12/26

    Web-Application

    A Wicket web application consists of a class that extends webapplication

    a corresponding web.xml which points at the webapplication

    some pages

    Holds configuration Homepage (default page redirected to)

    Session (session object could be used for authentication)

    Mounted urls

    Spring injection

    Lots more can happen here.

  • 8/3/2019 Wicket Basic

    13/26

    Components

    PageLabel

    ListView

    Link

    Wicket Core, Wicket Extensions, WicketstuffIf a component contains children you must add them tothe component, for example a form. Hierarchy is thesame as DOM / HTML

  • 8/3/2019 Wicket Basic

    14/26

    Code time:)

    Let's create a Listview iterating over authors and posts

  • 8/3/2019 Wicket Basic

    15/26

    Behavior & Panel

    Panel, your way to do own componentsBehavior, a way to modify components by adding extrajavascript etc...

    MyPanel.html:

    MyPanel.java:

    public MyPanel(IModel nameModel) {

    add(new Label(time,new Model(System.currentTimeMillis()));

    add(new Label(name,nameModel));

    }

  • 8/3/2019 Wicket Basic

    16/26

    Markup Inheritance

    Wicket supports markup inheritanceWhich means that a page can extend another page

    Usage (java side we know using the extends keyword) encapsulate your markup with

    Subclass:

    Superclass:

  • 8/3/2019 Wicket Basic

    17/26

    Code time:)

    Lets see how it's used in our blog application.

  • 8/3/2019 Wicket Basic

    18/26

    Internationalization

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

    [text]

    matching property file:

    mykey=mytext

  • 8/3/2019 Wicket Basic

    19/26

    Forms

    Validation Required

    More, or roll your own?

    Controls Radio

    Check

    List

    Text

    Palette

    ...

  • 8/3/2019 Wicket Basic

    20/26

    Code time:)

    Time for authentication, lets do a Login form on thelogin page

    And do the AuthorPage with a form so we can postposts.

  • 8/3/2019 Wicket Basic

    21/26

    Testing

    Wicket provides some simple facilities to make tests Wicket Tester

    Simulates Http requests

    Allows you to make wicket specific assertions

    Wicket FormTester

    Allows you to manipulate forms

  • 8/3/2019 Wicket Basic

    22/26

    Code time:)

    Let's examine a test case that tests our LoginPage &AuthorPage flow..

  • 8/3/2019 Wicket Basic

    23/26

    Ajax

    Adding Ajax to wicket is a breeze, just add aajaxBehavior and thats it.

    Sometimes though you can use one of the Ajax controlsinstead.

    Sample CodeAjaxButton ajaxBtn = newAjaxButton("ajaxButton") {

    @Override

    protectedvoid onSubmit(AjaxRequestTarget target, Form form){

    }@Override

    protectedvoid onError(AjaxRequestTarget target, Form form) {

    target.addComponent(feedbackPanel);

    }

    };

    d )

  • 8/3/2019 Wicket Basic

    24/26

    Code time:)

    Time to Ajax-ify our home page, let's refresh ourAuthor/post overview at a given interval.

    B d B i B t Ni t k

  • 8/3/2019 Wicket Basic

    25/26

    Beyond Basics, But Nice to know

    Authorization Controlling visibility at component level for roles

    Protecting against CSRF, withCryptedUrlWebRequestCodingStrategy, set it inWebApplication.java

    IVisitor, visit your components. Good for visitingmultiple children and perhaps modifying them, like forexample changing styles based on validation etc.

    Additi l R G i f th

  • 8/3/2019 Wicket Basic

    26/26

    Additional Resources Going further

    Wicket In Action (Book)Jayway Wicket Course, 2 days or 3 days

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

    Wicket User Mailinglist [email protected] (very

    friendly and helpful community)Maven2 archetypes (fast way to get started ) Quickstart (official), sets up wicket naked..

    WicketStuff Iolite (wicketstuff), sets up wicket withJPA(Hibernate) and Spring

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

    mailto:[email protected]://wicketstuff.org/http://wicketstuff.org/mailto:[email protected]