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.
 
download
 
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:

 

JSP

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

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

 
success.jsp
 

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

 

struts.xml

 
The mapping in struts.xml should be as below

<struts>
	<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>
		</action>
	</package>
</struts>

 

Run it

 
http://localhost:8089/ActionErrorMessage/

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
 

Note:

 
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


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • Jagannath Kale

    Hi,I used login application and it is validating form if userName and password is blank.

    Now I am sending userName and password like this

    http://localhost:8080/LoginApp/loginAction.action?userName=jagannath&password=123 then also logged in successfully instead of filling login.jsp form page. In this case user should not logged in. How can avoid it using struts2.

  • sagar

    on redirect how can i get this messages

    like

    y

    in this case i am not able to get this action and error messages

  • Sudev Wilson

    How I can show validation messages besides the fields using struts2
    ie, Validation messages next to the field..

  • sarah

    is there a way to pass placeholder values in the error message ??

  • sami

    nice post…..