Struts 2 provide a lot of custom tags for development and we have already looked into Data Tags, Control Tags and UI Tags. Today we will look into two tags that are related to action class response that we can use in the result pages.

Struts 2 Action Error and Action Message

  1. actionerror tag: This tag is used in conjunction with Action class validation for form fields. If validation fails for any form fields, we can add action errors and then Struts 2 API forwards the request to “input” result page where we can use this tag to show the error messages. This tag is helpful in server side validation of form fields and then returning the input page with error message. Syntax of this tag is:<s:actionerror/>We will look it’s usage with a simple project to explain how easy s:actionerror is to use.
  2. actionmessage tag: This tag is used to show some custom message added by Action classes in the result page. For example, we can use this tag to welcome a user and show them last login time at the top of the page. Syntax of this tag is:<s:actionmessage/>

Both these tags generated an unordered list of action errors or messages added in the action class. Let’s create a simple project to show their usage. Our final project will look like below image.

Struts2-Action-Errors-Messages-Project

Struts 2 Configuration Files

web.xml

pom.xml

struts.xml

Configuration files are self understood and used to configure maven web application project to use Struts2 framework. The important point to note is the struts.custom.i18n.resources where we are providing property file name for global messages. We will use this file for labels in result pages for internationalization.

Another point to notice is the “input” result page for Welcome action, it’s used incase of any form field validation failure.

global.properties

A simple property file that will be used in result pages for labels.

Struts 2 Action Class

WelcomeAction.java

We are extending ActionSupport class and overriding validate method where we are adding action errors and action messages to ValueStack. Other part of action classes include execute() method and java bean properties with getter setter methods.

Struts 2 Result Pages

login.jsp

We are using Struts2 if conditional tag to check if there are any action error messages are present or not. hasActionErrors() method is defined in ActionSupport class that returns true if any action errors exists in the ValueStack.

Notice that we are using CSS for styling of error messages and using key attributes of UI tags to generate the label from property file configured in struts configuration file. We are not using actionmessage tag here because if validation doesn’t fail, we are not returning to this page.

welcome.jsp

A simple result page where we are utilizing actionmessage tag. Since this tag generates list with class name as actionMessage, we can use it for styling purpose. hasActionMessages() method is defined in ActionSupport class and returns true if there are any action messages present.

When we run above application, we get following response pages.

Struts2-ActionError-example-450x241

Generated HTML snippet of actionerror tag is:

Struts2-ActionMessages-Example-450x212

Generated HTML snippet of actionmessage tag is:

As you can see it’s very easy to use action errors and action messages tags in result pages and since generated html contains class for tags, we can easily add CSS rules for specific styling purposes. Download project from below link and play around with it for better understanding.

By admin

Leave a Reply

%d bloggers like this: