Apache Wicket is a Java web application framework that uses a component-based programming model to build web UIs, allowing developers to treat page elements like buttons and labels as objects and handle events like clicks. It aims to bridge the gap between desktop and web development by enabling an event-driven programming style and component hierarchy similar to Swing. Wicket pages are composed of reusable Java components that correspond to HTML elements, avoiding the impedance mismatch between Java and HTTP programming models.
5. Model 2 (MVC)
Struts, Spring MVC, Stripes
home
jsp
request
browser server
response
home
controller
6. 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
7. 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
8. The Impedance Mismatch
The Programming Model
• 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.
9. 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?!
10. 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
11. The Component Model
The Underlying Abstraction
• 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?”
14. General Structure
Markup (HTML’s Role)
• Layout the element hierarchy
• Style the elements
Code (Java’s Role)
• Mirror and implement markup’s element hierarchy
• Event handling
Properties Files (~Auxiliary Roles)
• Literal strings and i18n
15. Hello World
Markup
<html>
<body>
<span wicket:id="message">Message goes here!</span>
</body>
</html>
Java
import 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!"));
}
}
17. 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
}
}
}
18. Component Family
Component Page
WebComponent Link
Panel
WebPage
AjaxLink
Button
Checkbox TextArea
Form
TextField
DropDown
Label
...many more
19. Super Models
• Static Model
• Dynamic Model
• Property Model
• Compound Property
• Loadable Detached
20. Basic Models
Static Model
public class HelloWorld extends WicketExamplePage
{
public HelloWorld()
{
add(new Label ("name", new Model(person.getName())));
}
}
Dynamic Model
personForm.add(new RequiredTextField("personName", new IModel<String>()
{
@Override
public String getObject() {
return person.getName();
}
@Override
public void setObject(String value) {
person.setName(value);
}
}));
21. More Models
Property Model
public PropertyModel(final Object modelObject, final String expression)
class Person class Address
{ {
private Address address; private String zip;
public Address getAddress() public String getZip()
{ {
return name; return zip;
} }
... ...
} }
personForm.add(new RequiredTextField("zip",
new PropertyModel<Person>(person, "address.zip")));
22. Demonstration
WebPage Component
• Java • HTML
Subclass Master Page child & parent tags
Custom Component
• Java, HTML • Child components
• Panel Superclass • Tags to include
Event Handling
• Original HTML • Ajax (Debug)
• Generated Javascript • Submit handling
23. 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
24. Deeper Dive
Maven Quickstart Archetype
• An easy way to start with a simple, Mavenized,
Wicket project
Other Topics
• More on Models, Sessions and Security
• Unit Testing
• Custom components
• URL mapping, IoC Integration, Persistence ...