Pages Navigation Menu

Coding is much easier than you think

ActionError & ActionMessage Example in Struts 2

In this tutorial we will learn about ActionError & ActionMessage class and its usage.
ActionError ActionMessage Banner
a) ActionError class is used to send error feedback message to user and it get rendered in jsp by using <s:actionerror/> tag.
b) ActionMessage class – is used to send information feedback message to user, and it get rendered in jsp using <s:actionmessage/> tag.
** UPDATE: Struts 2 Complete tutorial now available here.
In this tutorial we will use the previous tutorials example to implement the functionality of ActionError and ActionMessage class.
Here’s a simple login form, display the error message (actionerror) if the username is empty, Otherwise redirect to another page and display the a welcome message (actionmessage).

1. Folder Structure

ActionError ActionMessage in Struts2

Action Class

The action class, do a simple checking to make sure that the username is not empty, if the userName is not valid then the action class set the error message with addActionError() , if its valid then it set the successful message with addActionMessage().

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 6677091252031583948L;

	private String userName;

	public String execute() {

		return SUCCESS;

	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;

	public void validate() {
		if (userName.isEmpty()) {
		addActionError("Username can't be blanked");
		} else {
		addActionMessage("Welcome " + userName + ", You have been Successfully Logged in");

You might be interested to read:



Two simple JSP pages with css style to customize the error message.

<%@taglib uri="/struts-tags" prefix="s"%>
<h3>ActionError &amp; ActionMessage Example</h3>
	<s:actionerror />
	<s:form action="loginUser">
		<s:textfield name="userName" placeholder="Username" label="Username" />
		<s:submit value="Submit" />


<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Welcome Page</title>
<h3>ActionError &amp; ActionMessage Example</h3>
	<s:actionmessage />



The mapping in struts.xml should be as below

	<package name="default" extends="struts-default">
		<action name="loginUser" class="com.action.LoginAction">
			<result name="success">success.jsp</result>
			<result name="input">login.jsp</result>


Run it


ActionError ActionMessage
When Username is invalid, display error message with <s:actionerror/>
Action Error in struts 2
When Username is valid, display welcome message <s:actionmessage/>
Action Message in struts 2


In struts 2 when you use <s:actionerror /> tag, it displays the errors with bullets, read the article on Change default style of s:actionerror / s:actionmessage tag in order to remove this

About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: