Versions Compared

Key

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

...

Code Block
java
java
titelComment.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();
}

...

Code Block
java
java
titelGuestBook.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 = "";
		        }
	    }
}

When the CommentForm is submitted, the onSubmit() method is called. Notice that nothing gets the value of the TextArea that was added in the CommentForm constructor. This is because the comment is the model and the third parameter to the TextArea constructor specified the property of the model to update. So all onSubmit() has to do is create a new comment from the model that was updated and add it to the comment list. When the page redraws, the new list will be rendered.

...

Code Block
html
html
titelGuestBook.html
borderStylesolid
<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>

...