73. What is the Action Errors and Action Message?

ActionMessage: A class that encapsulates messages. Messages can be either global or they are specific to a particular bean property.
Each individual message is described by an ActionMessage object, which contains a message key (to be looked up in an appropriate message resources database), and up to four placeholder arguments used for parametric substitution in the resulting message.

data.ok=Data saved successfully

ActionMessages messages = new ActionMessages();
ActionMessage msg = new ActionMessage(“data.ok”);
messages.add(“message1”, msg);

<html:messages id=”msgId” message=”true” property=”message1″>
       <bean:write name=”msgId”/><br>
</html:messages>

msg = new ActionMessage(“data.do”, “stop”);
messages.add(“message2”, msg);

In property file data.do configured as below
data.do=Please {0}

ActionErrors: A class that encapsulates the error messages being reported by the validate() method of an ActionForm. Validation errors are either global to the entire ActionForm bean they are associated with, or they are specific to a particular bean property (and, therefore, a particular input field on the corresponding form).

error.name.required=Please enter the name
ActionErrors errors = new ActionErrors();
errors.add(“name”,new ActionMessage(“error.name.required”));

Advertisements

6 Responses

  1. comment

  2. Action Errors: Generally in the validate() method of the ActionForm class, we simply place the keys for messages into ActionErrors object, like “error.search.criteria.missing”.The error tage will use these keys to load the appropriate messages from the ApplicationResources.properties file

    ActionErrors errors = new ActionErrors()
    if(!isValidAge(age))
    errors.add(“age”,new ActionError(“error.student.age.invalid”))

  3. I read your blog for quite a long time and must tell you that your articles are always valuable to readers.

  4. ActionErrors:
    error.name.required=Please enter the name
    ActionErrors errors = new ActionErrors();
    errors.add(”name”,new ActionMessage(”error.name.required”));

    And how would you now display the Error in the JSP?

    • Re: Andreas

      If you added the following:
      errors.add(”name”,new ActionMessage(”error.name.required”));

      In the jsp, you would have:

      You need a validation.properties (mine was in WEB-INF/classes/messages/) for the error msg:
      =====
      errors.header=
      errors.footer=
      errors.prefix=
      errors.suffix=

      error.name.required=Please enter the name
      =====

      Note: You can make a validate method that verifies import and adds the errors. You can count the errors and have an additional message, depending if one or more errors were found:
      ===
      if (errorCount > 0) {
      if (errorCount == 1) {
      errors.add(“validationFailed”, new ActionError(“SingleErrorFound”));
      }
      else {
      errors.add(“validationFailed”, new ActionError(“MultipleErrorsFound”));
      }
      }
      ===

      If you do this, add the following to the validation.properties:
      ===
      SingleErrorFound=There was an error in one of the values entered.
      MultipleErrorsFound=There were multiple errors in the values entered.
      ====

      I hope this helps.

  5. A series of true different colours is programmed in the display, and the transmission quick through spots

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: