Apache Wicket: web applications with just Java
description
Transcript of Apache Wicket: web applications with just Java
![Page 1: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/1.jpg)
Apache Wicket:
web applications with just Java
![Page 2: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/2.jpg)
Agenda
• What is Wicket
• Core concepts of Wicket
• Developing a custom component
• Q&A
![Page 3: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/3.jpg)
Wicket in a Nutshell
• Open Source (Apache Software Foundation)• Component based • Pure Java + Pure HTML• Simple• Enthusiastic community
![Page 4: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/4.jpg)
What is Wicket?
• The technology: enabling component-oriented, programmatic manipulation of markup
• The mission: bringing object oriented programming to the web application view layer
• The reward: have a fun job and be good friends with your manager again
![Page 5: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/5.jpg)
Achieving these goals through
• Keeping simple things simple• Utilizing an object-oriented component model
where components are truly self contained• Re-applying the Model View Controller pattern
on components instead of requests, with models as first class citizens
• Having a clean separation of concerns between HTML and Java
![Page 6: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/6.jpg)
Achieving these goals through
• Providing transparent, fully automated state management
• Keeping configuration needs to the minimum
• No more XML!
• Making using & creating custom components easier than any framework out there
![Page 7: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/7.jpg)
Component versus ‘traditional’
• Traditional– Struts, WebWork, Spring MVC, etc.– Proven for web– A lot of web developers available
• Components– JSF, Wicket, Tapestry, etc.– Traditional model for UI development on thick
clients– A lot of developers available
![Page 8: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/8.jpg)
Features
• Page composition– panels, borders and markup inheritance
• Excellent localization and style support– template and resource loading
(_be.html, .xml)– localized models (e.g. for labels)– sophisticated resource bundle lookup
(composition & inheritance hierarchy)– automatic client capabilities detection
![Page 9: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/9.jpg)
Features
• Integration– Spring– Guice– Hibernate– JasperReports– OSGi
• Fancy components– sortable, filterable, pageable, data aware tables– date picker, rich text editor, Google Maps– tabbed panel, navigation, tree, wizard
![Page 10: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/10.jpg)
Features
• State management– type safe sessions
• clustering support• back button support• Double submit strategies
– render redirect/ redirect to buffered response/ none
• Testing support– JUnit testing
• extensive logging and error reporting
![Page 11: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/11.jpg)
Features
• Ajax support and components– Ajax without writing JavaScript, Dojo,
Scriptaculous, ...
• Header contribution– Javascript & CSS
• URL mounting– pretty URLs
• Component level security
![Page 12: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/12.jpg)
Agenda
• What is Wicket
• Core concepts of Wicket
• Developing a custom component
• Q&A
![Page 13: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/13.jpg)
Wicket’s concepts
• Application
• Session
• RequestCycle
• Components
• Behaviors
• Models
![Page 14: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/14.jpg)
Application
• Main entry point for your web application
• Configuration– Output Wicket specific tags?
– Watch for markup changes every …?
– Defines homepage
• Factories for– Session
– RequestCycle
– Security
– …
![Page 15: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/15.jpg)
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>
![Page 16: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/16.jpg)
Wicket’s concepts
• Application
• Session
• RequestCycle
• Components
• Behaviors
• Models
![Page 17: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/17.jpg)
Session
• Abstraction of a user session
• Storage typically in HttpSession
• Stores session specific data– Locale, Client info (browser vendor and
version)– Your own data?
• Logged in user• Shopping cart contents
– Limited page history for back button support
![Page 18: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/18.jpg)
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);
![Page 19: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/19.jpg)
Wicket’s concepts
• Application
• Session
• RequestCycle
• Components
• Behaviors
• Models
![Page 20: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/20.jpg)
RequestCycle
• 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
![Page 21: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/21.jpg)
RequestCycle
• Two types of requests:– Stateful
• Tied to specific user session• Not bookmarkable
– Stateless• Not necessarily tied to specific user session• Bookmarkable
![Page 22: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/22.jpg)
Wicket’s concepts
• Application
• Session
• RequestCycle
• Components
• Behaviors
• Models
![Page 23: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/23.jpg)
Components
• encapsulate the programmatic manipulation of markup
• can receive an event– onClick, onSubmit
• know how and where to render itself
![Page 24: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/24.jpg)
Component
• Ultimate super class wicket.Component– Label– MultiLineLabel– TextField– PasswordTextField– Image– Link– Tree– BookmarkablePageLink– JasperReports
– ListView– Loop– PagingNavigator– ImageMap– Button– Ajax…– Sorting, paging repeaters– Wizard– DatePicker
![Page 25: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/25.jpg)
Components and markup
• A component is identified in markup with wicket:id
Html: <h1 wicket:id=“msg”>Gets replaced</h1>
Java:new Label(“msg”, “Hello, World!”);
Final (wicket tags can be stripped):
<h1>Hello, World!</h1>
![Page 26: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/26.jpg)
Components and markup
• Component can have its own markup file– Page– Panel– Border
• Put java, markup and supporting files in same package on class path
![Page 27: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/27.jpg)
A page: Hello, World!
![Page 28: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/28.jpg)
Wicket’s concepts
• Application
• Session
• RequestCycle
• Components
• Behaviors
• Models
![Page 29: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/29.jpg)
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>
![Page 30: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/30.jpg)
Behaviors
• Change attributes of your component’s markup• Add javascript events• Add Ajax behavior
component.add( new AjaxSelfUpdatingBehavior( Duration.seconds(1)));
![Page 31: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/31.jpg)
Wicket’s concepts
• Application
• Session
• RequestCycle
• Components
• Behaviors
• Models
![Page 32: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/32.jpg)
Models
• Models bind your POJO’s to Wicket components
Label(“name”, model)
<<Person>>
+name : String+city : String
PropertyModel
![Page 33: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/33.jpg)
Models
• Lazy binding in Java sucks– Doesn’t update:
new TextField(“txt”, person.getName())
– Gives null pointers:new Label(“street”, person.getAddress().getStreet())
• Solution: OGNL/EL like expressions
![Page 34: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/34.jpg)
Models
• PropertyModel:– new PropertyModel(person, “name”)– new PropertyModel(person, “address.street”)
• Be aware for nulls when using updates:Person p = new Person();
new TextField(“street”, new PropertyModel(p, “address.street”));
![Page 35: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/35.jpg)
Agenda
• What is Wicket
• Core concepts of Wicket
• Developing a custom component
• Q&A
![Page 36: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/36.jpg)
Why a custom component?
• Eelco Hillenius: « Imagine being told that you can use Java as your
programming language, but at the same time being told not to create your own classes. [...]
I fail to understand why that has to be different for UI development, and Wicket proves it doesn't have to be so. »
![Page 37: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/37.jpg)
Custom components for all
464 pages
20 minutes
![Page 38: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/38.jpg)
Example: Password strength
weak
medium
strong
![Page 39: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/39.jpg)
Components
• Creating custom component is as easy as typing in ‘extends’
• Extends wicket.Component down the line
• Available on application classpath
![Page 40: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/40.jpg)
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
![Page 41: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/41.jpg)
Example: Password strength
![Page 42: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/42.jpg)
Example: Password strength
![Page 43: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/43.jpg)
Example: Password strength
![Page 44: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/44.jpg)
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
![Page 45: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/45.jpg)
Conclusion
• Smart component oriented web framework
• Easy creation and use of custom components
• Enthustiatic community
Join in!http://wicket.apache.org/
![Page 46: Apache Wicket: web applications with just Java](https://reader035.fdocuments.net/reader035/viewer/2022062314/5681475d550346895db49c48/html5/thumbnails/46.jpg)
Agenda
• What is Wicket
• Core concepts of Wicket
• Developing a custom component
• Q&A