Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The Comment POJO model is very straightforward (we have omitted the getter/setter code for brevity in this example):

Code Block
java
java
titleComment.java
borderStylesolid
package wicket.examples.guestbook;

import java.io.Serializable;
import java.util.Date;

public class Comment implements Serializable
{
    public String text;
    public Date date = new Date();
}

...

The implementation below obtains the Comment POJO from the list item and adds label components for the date and text of the Comment. This is all accomplished in just a few lines of code.

Code Block
title
java
javaGuestBook.java
borderStylesolid
package wicket.examples.guestbook;

import java.util.Date;
import java.util.List;
import java.util.Vector;

import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;
import wicket.markup.html.basic.MultiLineLabel;
import wicket.markup.html.form.Form;
import wicket.markup.html.form.TextArea;
import wicket.markup.html.list.ListItem;
import wicket.markup.html.list.ListView;
import wicket.model.PropertyModel;

public final class GuestBook extends WebPage
{
    /** Use a Vector, as it is synchronized. */
    private static final List commentList = new Vector();
    private final ListView commentListView;

    public GuestBook()
    {
        add(new CommentForm("commentForm"));
        add(commentListView = new ListView("comments", commentList)
        {
            public void populateItem(final ListItem listItem)
            {
                final Comment comment = (Comment)listItem.getModelObject();
                listItem.add(new Label("date", comment.date.toString()));
                listItem.add(new MultiLineLabel("text", comment.text));
            }
        });
    }

    public final class CommentForm extends Form
    {
        private final Comment comment = new Comment();

        public CommentForm(final String componentName)
        {
            super(componentName);
            add(new TextArea("text", new PropertyModel(comment, "text")));
        }

        public final void onSubmit()
        {
            final Comment newComment = new Comment();
            newComment.text = comment.text;

            commentList.add(0, newComment);
            commentListView.modelChanged();

            comment.text = "";
        }
    }
}

...

Finally, notice the <wicket:remove> block. This is simply markup that is there for previewing purposes only. When the page renders, it is stripped out.

html
Code Block
titleborderStyle
html
GuestBook.htmlsolid
<html>
<body>
  <form wicket:id = "commentForm">
    Add your comment here:
    <p>
    <textarea wicket:id = "text">This is a comment</textarea>
    <p>
    <input type = "submit" value = "Submit"/>
  </form>
  <p>
  <span wicket:id = "comments">
    <p>
        <span wicket:id = "date">1/1/2004</span><br>
        <span wicket:id = "text">Comment text goes here.</span>
    </p>
  </span>
  <wicket:remove>
    <p>
        1/2/2004<br/>
        More comment text here.
    </p>
  </wicket:remove>
</body>
</html>

...

For completeness, we've included the GuestBookApplication class, and as a final treat the modifications to the web.xml file.

Code Block
java
java
titleGuestBookApplication.java
borderStylesolid
package wicket.examples.guestbook;

import wicket.protocol.http.WebApplication;

public class GuestBookApplication extends WebApplication
{
    public GuestBookApplication()
    {
    }
    
    public Class getHomePage()
    {
        return GuestBook.class;
    }
}

...

Add the following two sections (servlet and servlet-mapping) to your web.xml file for running this application.

xml
Code Block
title
xml
web.xmlborderStylesolid
<servlet>
  <servlet-name>GuestBookApplication</servlet-name>
  <servlet-class>wicket.protocol.http.WicketServlet</servlet-class>
  <init-param>
    <param-name>applicationClassName</param-name>
    <param-value>wicket.examples.guestbook.GuestBookApplication</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>GuestBookApplication</servlet-name>
  <url-pattern>/app/*</url-pattern>
</servlet-mapping>