How to show Custom ActionError, ActionMessage and FieldError in Struts 2.x?


Struts2Logo

 ActionError- It is used to show error message (means -ve sense  message) to  users.

ActionMessage- It is used to show informative  message (means +ve sense  message) to  users.

FieldError- It is used to show field error (validation error) to users.

home.jsp


In this page we will check for actionmessage, actionerror and field error .If any of these exist then errorHandling.jsp page will display in dialog box.
<s:if test="hasFieldErrors() || hasActionMessages() || hasActionErrors()">

<jsp:include page="/jsps/errorHandling.jsp"/>

</s:if>

errorHandling.jsp


This page will display custom message to the users. I am using Struts2-jquery library's dialog box and theme for custom message.
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib uri="/struts-jquery-tags" prefix="sj" %>
<div style="visibility:hidden">
<sj:dialog
id="ErrorDialog"
title=" "
modal="true"
width="500"
resizable="false"
buttons="{
'O K':function() {
$(this).dialog('close');
}
}"
>
<h6>
<s:actionmessage theme="jquery"/>
<s:fielderror theme="jquery"/>
<s:actionerror theme="jquery"/>
</h6>
</sj:dialog>
</div>

we add these messages and errors in Action class by overriding following methods:

For ActionMessage-  addActionMessage("Logged Out Successfully");

For ActionError-  addActionError("You are Not a Valid User");

For FieldError-  addFieldError("password", "Please Enter password."); Here first parameter is field name to check for and second parameter  is your  message to display to users.

 

Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

3 comments:

  1. The above example to work properly the sj:head tag is required to be included...

    ReplyDelete
  2. Check out these best things to do in Tampa in November, and have an unforgettable journey in the gorgeous city. Book tickets now!

    ReplyDelete
  3. If you are planning to buy a video surveillance system for your business, then we have shared some of the most important aspects that you should consider. At Revlight Security System we serve best commercial cctv camera system for your business.

    ReplyDelete