|
Advertisement |
ApacheWicket - in NetBeans
Posted On January 11, 2012 by Sneha Latha filed under Miscellaneous
Apache Wicket makes developing web-apps simple and enjoyable. This article will give you a quick introduction to ApacheWicket.
Wicket is a component-oriented Java web framework. Wicket is meant for presentation tier like Struts or JSF and not an application framework for middle tier like Seam or Spring. It is different from action-/request-based frameworks like Struts, WebWork (Struts2), or Spring MVC where request is routed to a single action/controller class –front controller. Wicket is page-centric like Tapestry. That is, developing a dynamic page in Wicket generally involves creating the following two artifacts:
- HTML template
- Java page class
The widgets in the HTML file are mapped to components in the Page class and the components register themselves as listeners and the Wicket calls the corresponding listener method when a widget is clicked. In other words a user action typically triggers an event on one of the form components, which in turn sets in motion an event listener method. Some of the other frameworks that fall in this category are Tapestry, JSF, and ASP.NET. Component-oriented frameworks bring the desktop GUI or event-driven programming to web.
XHTML Template Wicket uses plain XHTML for templating. It does not use JSP and is more like seam-improved JSF or JSF2, which uses only facelets for the view layer. A page is composed from a XHTML template and a Java page class, which provide the dynamic content to the template. The internal Wicket component that is entrusted with the job of locating the HTML markup corresponding to a Page looks for the markup in the same place as the Page class. There needs to be a one-to-one correspondence between the HTML elements with a wicket:id attribute and the Page
components. The HTML template could in fact be termed as a viewwith the actual component hierarchy being described in the Page class. This enforces a clear separation of presentation and business logic and allows templates to be edited with conventional WYSIWYG design tools. Each component instantiated in the page class is bound to a named element in the XHTML and becomes responsible for rendering that element in the final output. In the Java page class the components provided by Wicket framework are used -similar to your “Swing” programming where components from AWT or Swing are used.
OOPs programming and state management As Wicket is built on Servlet API, you can see web.xml but it is the only xml file used and there is no framework specific xml configuration file like strus-config.xml of Struts or faces-config.xml of JSF. Instead a Java class “Application” is used. As Wicket does not use xml configuration files and uses only Java files for the purpose and minimizes configuration information in the above file by following concepts popularized by Rails such as convention over configuration. Wicket provides an object-oriented approach toward developing dynamic Web-based UI applications. Because Wicket is pure Java™ and HTML code, you can leverage your knowledge about Java to write applications based on Wicket, dramatically reducing your development time. In other words, Wicket lets you develop web applications using regular OO Java programming. Because objects are stateful by default (remember: objects = state + behavior), one of Wicket’s main features is state management. You want this to be transparent so you don’t need to worry about managing state all the time. Wicket aims to provide a programming model that shields you from the underlying technology (HTTP) as far as possible so you can concentrate on solving business problems rather than writing plumbing code. Wicket lets you program your components and pages using regular Java constructs and You can create components using the new keyword, create hierarchies by adding child components to parents, and use the extend keyword to inherit the functionality of other components. Wicket isn’t trying to offer a development experience that reduces or eliminates regular programming. On the contrary, it tries to leverage Java programming to the maximum. That enables you to take full advantage of the language’s strengths and the numerous IDEs available for it. You can use OO constructs and rely on static typing, and you can use IDE facilities for things like refactoring, auto-complete, and code navigation.
Wicket Models
Components are closely tied to another important Wicket concept called models. In Wicket, a model (an object implementing the IModel interface) acts as the source of data for a component. It needs to be specified when constructing the component (doing a new). It helps to think about Wicket’s IModel hierarchy as model locators. These classes exist to help the components locate your actual model object; i.e., they act as another level of indirection between Wicket components and the “actual” model object. This indirection is of great help when the actual object is not available at the time of component construction and instead needs to be retrieved from somewhere else at runtime. Wicket extracts the value from the model while rendering the corresponding component and sets its value when the containing HTML form is submitted.
Ajax support
Like any modern web framework that supports web2.0 wicket also supports Ajax. Though integration with such Ajax engines as Dojo or YahooUI is possible it provides its own Ajax engine. When this engine is used, there is no difference in usage between ordinary components and “ajaxified” components.
Wicket plugin for NetBeans
By creating a Wicket project in NetBeans IDE we can understand things better.
The latest wicket plugin for NetBeans is available only for IDE version 6.9.1 and the plugins for earlier versions could not be downloaded. Download the latest version. Unzip it. You can see that it consists of the following modules:
- org-netbeans-modules-web-wicket.nbm. Provides the Wicket-specific functionality that is used in this tutorial.
- org-netbeans-modules-wicket-templates.nbm. Provides Wicket file-level templates for creating typical Wicket artifacts, such as pages and panels.
- org-netbeans-modules-wicket-library.nbm. Provides the Wicket JARs, installs them in the IDE's Library Manager. Therefore, you do not need to download the Wicket distribution from the Wicket site, because the plug-in provides everything you need.
To install the Wicket support plugin into the IDE, go to the Plugin Manager (Tools > Plugin from the main menu) and install the plug-in under the Downloaded tab.
Create a web project Now you can create a new project. In categories choose Java WebàWeb Application. Give the project any name and choose any server. But ensure that among the frameworks Wicket is chosen as shown in Figure 1.As mentioned earlier Application.java is the configuration file of Wicket and the IDE-generated files include the same. You can open and have a look at it. It extends WebApplication class provided by Wicket and contains the method getHomePage(). You will be prompted by the IDE to add @override annotation to the method.
To simplify, you can delete the following fles:
1) BasePage.java
2) HeaderPanel.java
3) HeaderPanel.html
Homepage.java will show error now as it extends BasePage. Modify it to extend WebPage and add:
import org.apache.wicket.markup.html.WebPage;
You can also add:
import org.apache.wicket.markup.html.basic.Label;
Add the following code to the constructor:
add(new Label("message", "Hello GANESH using Wicket!!"));
Modificaton in HomePage.html Wicket mandates that every HTML template be backed by a corresponding Page class of the same name. This tells you that HomePage.java is the page class for HomePage.html This is often referred to as a page-centric approach to web development. Tapestry falls under the same category as well. Modify HomePage.html. Replace <span wicket:id='mainNavigation'/>with <span wicket:id="message">Hello World !</span>
We created in the page class a label “message” and the same is passed in html file in <span wicket:id= > </span>.The text within <span></span> will be replaced with the text in the page class for the id.
Run the application. When you save all the changes and run the application, the server will start and the default browser will open and you can see Figure 2.You can have a look at web.xml also.
|
web.xml |
|
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <filter> <filter-name>WicketApplication</filter-name> <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class> //-------------------------------------------------------------------------1) <init-param> <param-name>applicationClassName</param-name> <param-value>com.myapp.wicket.Application</param-value> //-----2) </init-param> </filter> <filter-mapping> <filter-name>WicketApplication</filter-name> <url-pattern>/wicket/*</url-pattern> //-------------------------------------3) </filter-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file/> </welcome-file-list> </web-app> |
1) request goes to WicketFilter
2) You can compare Wicket development to Swing development. A Swing application will typically have a main class that kicks off the application. Wicket also has one. The init –param applicationClassName informs that our Application class is that class. It should extend WebApplication and it informs Wicket of the home page that users first see when they access the application.
3) The url pattern is /wicket.
Conclusion Wicket uses plain XHTML for templating (which enforces a clear separation of presentation and business logic and allows templates to be edited with conventional WYSIWYG design tools. Double-click the html file, and it will open in your favorite browser. It could come as a surprise to you if you are from JSP that you are able to open your template in a browser and see it render just fine. Wicket provides an object-oriented approach toward developing dynamic Web-based UI applications. Because Wicket is pure Java™ and HTML code, you can leverage your knowledge about Java and various tools available for Java to write applications based on Wicket, dramatically reducing your development time. But as Wicket is a framework for presentation tier, integration with frameworks meant for other layers such as Spring or Seam, JPA or Hibernate is called for. Integration of Seam with Wicket is not that easy and in fact in the books on Wicket there is no reference to such integration and they talk about only Wicket integration with Spring and JPA or Hibernate. We saw earlier that Spring does not have the construct for Optimistic Transaction Processing and its transaction-scoped persistent context in the business layer seriously inhibit the applications from exploiting the full potentials of ORMs. The use of “stateful” Wicket in the presentation tier cannot improve the situation in that respect. To integrate Wicket with Seam3, Seam Wicket module is available in Seam’s site. The goal of Seam Wicket is to provide a fully integrated CDI programming model to the Apache Wicket web framework. Interested readers may refer to the reference guide of the module available at the web site.









Hima commented, on January 15, 2012 at 12:56 p.m.:
this article worked great for? me.