Hello World

In this example, we’ll display the famous text “Hello,World!” in a browser and have the text delivered to us by Wicket.

In a Wicket application, each page consists of an HTML markup file and an associated
Java class (Both files should reside in the same package folder).  In our case will create one HTML -Helloworld.html and one java - Helloworld.java

First Create wicket project using quick start.

HomeWorld.html
 
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
   <h1>
        <span wicket:id="message">dynamic message will be replace here </span>
   </h1>
    </body>
</html>
HelloWorld.java
 
package com.javaassist;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.request.mapper.parameter.PageParameters;
 
public class HelloWorld extends WebPage {
 
 private static final long serialVersionUID = 1L;
 
    public HelloWorld(final PageParameters parameters) {
 
        add(new Label("message", "Hello World, java-assist"));
 
    }
}
we’ll add a label component (org.apache.wicket.markup.html.basic.Label) to the page to display the dynamic text.


Application object for web application.  

WebApplication returns a “HelloWorld.class” as the default home page, when Wicket see this “HelloWorld.class“, it know the mark up “html” page should be “HelloWorld.html“, and it should be able to find at the same package directory.

WicketApplication.java
 
package com.javaassist;

import org.apache.wicket.protocol.http.WebApplication;

public class WicketApplication extends WebApplication
{     

 @Override
 public Class<HelloWorld> getHomePage()
 {
  return HelloWorld.class;
 }
 
}
web.xml
Define Application object in wicket filter to make application work.
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_2_5.xsd"
 version="2.5">

 <display-name>helloworld</display-name>

 <filter>
  <filter-name>wicket.helloworld</filter-name>
  <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
  <init-param>
   <param-name>applicationClassName</param-name>
   <param-value>com.javaassist.WicketApplication</param-value>
  </init-param>
 </filter>

 <filter-mapping>
  <filter-name>wicket.helloworld</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

Project Structure

Test the app
Run the Jetty server and use "http://localhost:8080/" url

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...