Echo Message example in Wicket

In previous section of Wicket tutorials we have been well aware with the way how we can create an application in Wicket framework.

Echo Message example in Wicket

Echo Message example in Wicket

     

In previous section of Wicket tutorials we have been well aware with the way how we can create an application in Wicket framework. Here is the simple example to display message in  Wicket.

 This example of displaying message in Wicket we have used following three file which create a simple message displaying example. These files are as follows:

  1. Echo.java
  2. EchoApplication.java
  3. Echo.html

In this example of Echo message we have created Echo class by extending WebPage and here we have defined three properties for representing three fields of "firstname", "lastname" and "message". For setting properties values we have created their setters and getters method. According to properties PropertyModels are created and then further these models are added to form with the use TextField. Here is the full example code of Echo.java :-

Echo.java

package com.roseindia.wicket;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

public class Echo extends WebPage
{

 private String message = "type your message here";
 private String firstname = "first name";
 private String lastname  = "last name";

  public Echo()
  {
  PropertyModel messageModel = new 
  PropertyModel
(this, "message");
  PropertyModel firstNameModel = new 
 
PropertyModel(this, "firstname");
  PropertyModel lastNameModel = new
    
PropertyModel(this, "lastname");
  add(new Label("msg", messageModel));
  add(new Label("fname", firstNameModel));
    add(new Label("lname", lastNameModel));
  Form form = new Form("form");
  form.add(new TextField("firstInput", firstNameModel));
  form.add(new TextField("lastInput", lastNameModel));
  form.add(new TextField("msgInput", messageModel));
  add(form);
  }
  public String getMessage()
  {
  return message;
  }  
  public void setMessage(String message)
  {
  this.message = message;
  }
  public String getFirstname() {
  return firstname;
  }

  public String getLastname() {
 return lastname;
  }
  public void setFirstname(String firstname) {
  this.firstname = firstname;
  }
  public void setLastname(String lastname) {
  this.lastname = lastname;
  }
}

This Echo class will be called via the EchoApplication and this EchoApplication extends WebApplication. Here is the example code of EchoApplication.java as follows:

EchoApplication.java

package com.roseindia.wicket;
import org.apache.wicket.protocol.http.WebApplication;

public class EchoApplication extends WebApplication
{
  public EchoApplication()
  {   }
  public Class getHomePage()
  {
  return Echo.class;
  }
}

Echo.html

<html>
<head>
  <title>Wicket Examples - echo</title>
</head>
<body>
  <form wicket:id="form">
  <input type="text" wicket:id="firstInput" value="" size="20" />
  <br>

  <input type="text" wicket:id="lastInput" value="" size="20" />
  <br>

  <input type="text" wicket:id="msgInput" value="" size="50" />
  <br>

  <input type="submit" value="set message" />
 </form>
 First Name:
  <span wicket:id=
"fname" id="fname">First name here</span>
 <hr>
 Last  Name:
  <span wicket:id=
"lname" id="lname">Last name here</span>
 <hr>
 Messages :
  <span wicket:id="msg" id="msg">Message goes here</span>
 </body>
</html>

To run this application successfully one more thing we need to do is to make entry of EchoApplication in XML file web.xml. Following are the few lines code to do filter mapping of EchoApplication in web.xml 

<filter>
  <filter-name>
   EchoApplication
 </filter-name>
  <filter-class>
 org.apache.wicket.protocol.http.WicketFilter
   </filter-class>
   <init-param>
  <param-name>
  applicationClassName
  </param-name>
  <param-value>
   com.roseindia.wicket.EchoApplication
   </param-value>
   </init-param>
   <init-param>
  <param-name>
  debug
  </param-name>
   <param-value>
   2
  </param-value>
 </init-param>
</filter>
<filter-mapping>
  <filter-name>
 EchoApplication
  </filter-name>
 <url-pattern>
 /wicket/echo/*
  </url-pattern>
</filter-mapping>

To run this example start your web server and type following URL into browser:
   http://localhost:8080/WicketExample/wicket/echo/

Output:

Download Source code