Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · •...
Transcript of Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · •...
![Page 1: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/1.jpg)
Apache WicketJava Web Application Framework
Luther Baker • September 2009St. Louis - Java User’s Group
![Page 2: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/2.jpg)
• Web Application Framework
• Component-based Framework
• Wicket 1.4 is Java 1.5+ compliant
What is Wicket?
![Page 3: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/3.jpg)
Where does Wicket fit?
Presentation Tier (Wicket)
Business Tier
Integration Tier
SharedObjects
User
![Page 4: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/4.jpg)
Request / Response
request
responsebrowser
JSP Request Response
server
homejsp
![Page 5: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/5.jpg)
Model 2 (MVC)
homejsp
request
responsehome
controller
browser server
Struts, Spring MVC, Stripes
![Page 6: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/6.jpg)
Advantages of R/R
• Rendering views is generally quite fast
• Leverage existing tag libraries
• Recruiting developers may be easier
• Modern implementations have good support for DI and IoC frameworks
![Page 7: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/7.jpg)
Disadvantages of R/R
• Controller implementations must be considerate of multiple concurrent users and threads
• Controllers generally work literally in terms of requests and responses
• Controllers must often explicitly manage state
• Not necessarily Object Oriented
• The programming model is skewed
![Page 8: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/8.jpg)
The Impedance Mismatch
• Programming in Java - do we regularly focus on how the JVM manages object instances and variables?
• Generally, website development must happen with the HTTP protocol in mind, manually managing state within and across requests forcing front end handlers to be protocol specific.
The Programming Model
![Page 9: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/9.jpg)
What if ... ?• What if we considered a page a Page?
• What if we considered a button, a Button?
• And on a button click, handled an onClick event?
• What if a web development resembled Swing or event-driven development?
• What kind of framework could do this?
![Page 10: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/10.jpg)
Enter ... Wicket
• Component-based framework
• Instead of creating a controller, servlet or action class, create a Page
• Place Components on the page and define how each component reacts to user input
• Build the page in Java to manipulate the HTML file, not the other way around
![Page 11: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/11.jpg)
The Component Model
• Graphic devices and their representations are created in Java along with an HTML counterpart that reflects the Java hierarchy of objects.
• DOM style parent/child approach
• Event driven programming model
• How can such an abstraction could be created on top of HTTP?
The Underlying Abstraction
![Page 12: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/12.jpg)
Application 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 - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/13.jpg)
Wicket Config
WicketApplication.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 - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/14.jpg)
General Structure
• Layout the Element Hierarchy
• Styles elements
Page Responsiblities
Java Role
Properties
• Matching Hierarchy
• Event Handling
• Strings and i18n
![Page 15: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/15.jpg)
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 - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/16.jpg)
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 - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/17.jpg)
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 - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/18.jpg)
Component Family
WebPagePanel
Link
AjaxLink
Page
Button
FormTextField
TextAreaCheckbox
DropDown
Label ...many more
ComponentWebComponent
![Page 19: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/19.jpg)
Super Models
• Static
• Dynamic
• Property
• Compound Property
• Loadable Detached
![Page 20: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/20.jpg)
Basic Modelspublic class HelloWorld extends WicketExamplePage{ public HelloWorld() { add(new Label ("name", new Model(person.getName()))); }}
personForm.add(new RequiredTextField("personName", new Model(){ @Override public Object getObject(Component component) { return person.getName(); }
@Override public void setObject(Serializable object) { person.setName((String) object); }}));
Static Model
Dynamic Model
![Page 21: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/21.jpg)
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, "address.zip")));
Property Model
class Address{ private String zip;
public String getZip() { return zip; } ...}
![Page 22: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/22.jpg)
Demo
• Java
Subclass Master Page• HTML
child & parent tags
WebPage Component
Custom Component
Events
• Java, HTML
• Panel• Custom components
• Includes
• HTML
• Javascript• Ajax
• Submit
![Page 23: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/23.jpg)
Ideally Suited For ...
• Interactive style apps
• Help desk style Ticketing applications
• Online Registration applications
• Apps with lots of Forms and Ajax processing
• Apps simulating thick clients
• Anytime an event programming model better suites the problem domain
![Page 24: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/24.jpg)
Deeper Dive
• Builds a simple Maven ProjectMaven Quickstart Archetype
• More on Models
• Security
• Unit Testing
• Custom components
• URL mapping, IoC Integration, Persistence ...
Other Topics
![Page 25: Apache Wicket - Object Computingjava.ociweb.com/javasig/knowledgebase/2009-09/... · • Component-based framework • Instead of creating a controller, servlet or action class, create](https://reader030.fdocuments.us/reader030/viewer/2022041102/5f01c74e7e708231d400fd7e/html5/thumbnails/25.jpg)
Resources
• http://wicket.apache.org/
• http://wicketstuff.org/
• http://cwiki.apache.org/WICKET/
• Wicket in Action (Manning)
• Pro Wicket (Apress)