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.
 

 
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

 

 

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"%>



Login


ActionError & ActionMessage Example

 
success.jsp
 

<%@ taglib prefix="s" uri="/struts-tags"%>



Welcome Page


ActionError & ActionMessage Example

 

struts.xml

 
The mapping in struts.xml should be as below


	
		
			success.jsp
			login.jsp
		
	

 

Run it

 
http://localhost:8089/ActionErrorMessage/


 
When Username is invalid, display error message with <s:actionerror/>
 

 
When Username is valid, display welcome message <s:actionmessage/>
 

 

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
 

5 Comments

  1. 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.

  2. on redirect how can i get this messages

    like

    y

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

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

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

  5. nice post…..