Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of


The Message class represents a message being viewed or added to the message board. Every message contains the conventional name, email, subject, and body information. In addition to this data (provided by the user), the system provides a unique ID, a parent ID, which is the ID of the parent message to which the post is a reply, and a posted date. The class is as shown in Listing 2.

Listing 2 Message.java—Class Containing the Different Elements of a Message

import java.text.DateFormat;
import java.util.Date;
import org.apache.struts.action.ValidatingActionForm;
import org.apache.struts.util.ErrorMessages;

public class Message implements ValidatingActionForm {
    public final static String SPACE = " ";
    protected static DateFormat dformatter =
    protected static DateFormat tformatter =

    protected String parentId;
    protected String id;
    protected String email;
    protected String name;
    protected String subject;
    protected String body;
    protected String timestamp;

    public Message () {

    public Message (Message els) {

    public Message(String name, String email, String subject, String body) {

    public void inject(Message els) {

    public void setCurrentTimestamp() {
        Date current = new Date();
          dformatter.format(current) + SPACE +

// Provide setter and getter methods for the different properties

    public String toString() {
        StringBuffer s = new StringBuffer();
        s.append("id[" + getId() + "], name[" + getName() + "], ");
        s.append("email[" + getEmail() + "], subject[" + getSubject() + "], ");
        s.append("body[" + getBody() + "], parentId[" + getParentId() +"]");
        return s.toString();

Because this model class implements the ValidatingActionForm class, you will need to implement the validate() method that validates the different properties of the class. A valid message is identified by a non-null name, an email, and a body. The method returns an array of Strings that describe the encountered errors. You can add additional checks to determine whether the email has the format of name@domain.

To internationalize the error messages, you can use the ErrorMessages class provided in the Struts framework. You will need to specify error key-message entries in the application resources bundle for a specific locale, as described in Part 4 of this series: "Developing the Controller for the Application." To add an error message, invoke addError on the ErrorMessages instance, passing only the key. I have added an extra method _validate() that returns an instance of ErrorMessages to aggregate the errors encountered with the form validation with the ones that were encountered while invoking the action.

Listing 3 Message.java—Implementing the validate() Method of the ValidatingActionForm Interface

    public ErrorMessages _validate() {
        ErrorMessages errors = new ErrorMessages();
        if (getName() == null || getName().length() == 0) {
        if (getEmail() == null || getEmail().length() == 0) {
        if (getBody() == null || getBody().length() == 0) {
        return errors;

     * Validate the properties of this form bean, and return an array of
     * message keys for any errors we encounter.
    public String[] validate() {
        return _validate().getErrors();

In addition to this rudimentary class that contains the different elements of the message, we will also create another object-oriented avatar of the class: OOMessage. As shown in Listing 4, this class provides relationships to the parent message and replies.

Listing 4 OOMessage.java—The OO Avatar of Message

import java.util.Vector;

public class OOMessage extends Message {
    protected Message parent = null;
    protected Vector replies = new Vector();

    public OOMessage() {

     public OOMessage(Message els) {

    public void setParent(OOMessage parMsg) {
        parentId = parMsg.getId();
        parent = parMsg;

    public OOMessage getReplyAt(int index) {
        return (OOMessage) replies.elementAt(index);

    public int getReplyCount() {
        return replies.size();

    public OOMessage[] getReplies() {
        OOMessage[] array= new OOMessage[replies.size()];
        return array;

    public void addReply(OOMessage reply) {

    public void setReplies(OOMessage[] children) {
        for (int i = 0; i < children.length; i++) {

    public void setReply(int index, OOMessage reply) {
        replies.insertElementAt(reply, index);
  • + Share This
  • 🔖 Save To Your Account