You are on page 1of 38

Apache Wicket

Java Internship 2013

Agenda

What is Wicket Core concepts of Wicket Developing a custom component

What is Wicket

Open Source (Apache Software Foundation) Simple Pure HTML and Pure Java clear separation markup/logic Component based framework Custom components

Comparison to Spring MVC

Spring MVC

Pages are hard to maintain Pages are slow because they need jsp compilation time no more scriptlets and no more jstl which is limited compile time errors reusability easier html templates - no more jsp inclusions ajax

Wicket

Features
Page composition
panels and markup inheritance

Localization and style support Integration: Spring, Hibernate Fancy components


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

Ajax support Nice URLs

Agenda

What is Wicket Core concepts of Wicket Developing a custom component

Wickets concepts

Application Session RequestCycle Components Behaviors Models

Application

Main entry point for your web application Initialization and Configuration
Defines homepage Output Wicket specific tags?

Factories for
Session
RequestCycle Security

Application

Configured in web.xml:
<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>example.MyApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </filter>

Wickets concepts

Application Session RequestCycle Components Behaviors Models

Session

Abstraction of a user session Stores session specific data


Locale, Client info (browser vendor and version) Your own data
Logged in user Shopping cart contents

Session

class MySession extends WebSession { private ShoppingCart cart;

public ShoppingCart getCart() { }


public void setCart(ShoppingCart cart) { } }

mysession.setCart(new ShoppingCart());
ShoppingCart cart = mysession.getCart();

cart.add(quantity, selectedProduct);

Wickets concepts

Application Session RequestCycle Components Behaviors Models

RequestCycle

Responsible for processing requests Cleans up resources at the end Steps in a request cycle:
1.Create request cycle object 2.Decode the request 3. Identify request target (page, component, ) 4. Process event (onClick, onSubmit, ) 5. Respond (page, component, image, pdf, ) 6.Clean up

RequestCycle

Two types of requests:


Stateful
Tied to specific user session Not bookmarkable

Stateless
Not necessarily tied to specific user session Bookmarkable

Wickets concepts

Application Session RequestCycle Components Behaviors Models

Components

encapsulate the programmatic manipulation of markup can receive an event


onClick, onSubmit

Component

Ultimate super class wicket.Component


Label MultiLineLabel TextField PasswordTextField Image Link Tree BookmarkablePageLink ListView PagingNavigator Button Ajax Sorting, paging repeaters Wizard DatePicker

Components and markup

A component is identified in markup with wicket:id Html: <h1 wicket:id=msg>Gets replaced</h1> Java component must have the same wicket:id new Label(msg, Hello, World!); Final (wicket tags can be stripped with one configuration): <h1>Hello, World!</h1>

Components and markup

Component can have its own markup file


Page Panel

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

A page: Hello, World!

Wickets concepts

Application Session RequestCycle Components Behaviors Models

Behaviors
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>

Behaviors

Change attributes of your components markup Add javascript events Add Ajax behavior component.add( new AjaxSelfUpdatingBehavior( Duration.seconds(1)));

Wickets concepts

Application Session RequestCycle Components Behaviors Models

Models

Models bind your data(POJOs) to Wicket components

Label(name, model) <<Person>> PropertyModel

+name : String +city : String

Models

PropertyModel:
new PropertyModel(person, name) new PropertyModel(person, address.street)

CompoundPropertyModel
setModel(new CompoundPropertyModel(p)) add(new Label(name)); add(new Label(address.street));

Agenda

What is Wicket Core concepts of Wicket Developing a custom component

Example: Password strength

weak

medium

strong

Components

Creating custom component is as easy as typing in extends Extends wicket.Component down the line Available on application classpath

Example: Password strength

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

Example: Password strength

Example: Password strength

Example: Password strength

Components are reusable

Put them in a JAR Package all necessary resources:


HTML, JavaScript, Images, CSS class file

Put JAR on classpath Ready for (re)use

Conclusion

Smart component oriented web framework Easy creation and use of custom components Let's the user concentrate on the business logic Enthustiatic community
http://wicket.apache.org/

Documentation

Wicket in action Martin Dashorst Wicket cookbook Igor Vaynberg Wicket free guide Andrea del Bene http://wicket.apache.org/ https://cwiki.apache.org/confluence/display/WICKE T/How+to+do+things+in+Wicket http://wicketstuff.org/

Appendix examples

Create a wicket web application from scratch using Maven:


http://wicket.apache.org/start/quickstart.html

Use Wicket version 6.9.1 Create nice URLs by mapping web pages to suggestive paths https://cwiki.apache.org/confluence/display/ WICKET/Request+mapping

You might also like