Success/Error Messages in Struts
How to set success/error message?
Have a property (SaveStatus) and set the value to 1(means true) at the end of the method in insert/update functionality (methods). Set the same property to 2 inside the catch block which means we caught an exception.
This method is part of DAO which interacts with the database
public RequestDTO updateRequest(RequestDTO objRequestDTO) {
try{
// code block
objRequestDTO.setSaveStatus(1);
}
catch (Exception ex) {
ABCLogger.ABCLog(CLASS_NAME,
"In updateRequestDetails()->Exception:" + ex.getMessage(),
ABCLogger.ERROR);
ex.printStackTrace();
objRequestDTO.setSaveStatus(2);
} finally {
DBUtility.releaseConnection(objConnection);
}
}
In action class based on the status set in DAO we generate the success/error message.
if (saveStatus != null && saveStatus != 0)
{
if(saveStatus.toString().equals(ABCApplicationConstants.ABCCONSTANTS_ABCSTATUS_ACTIVE)){
ActionMessages messages = getMessages(request);
if(messages == null) messages = new ActionMessages(); ActionMessage msg = new ActionMessage("request.msg.success",objRequestDTO.getRequestDetailsKey());
messages.add("message",msg);
saveMessages(request,messages);
}
else{
objRequestForm.setRequestDetailsKey(null);
throw new ABCExceptionBean("ABC_REQUEST_1000", "AE");
}
}
We got something called MessageResources.properties file where we usually store the name/value pairs.
Struts-config entry for MessageResources.properties:
<message-resources null="false" parameter="resources.MessageResources"/>
Entry in Message Resources.properties file:
# -- Struts Validator Error Messages
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.byte={0} must be a number.
errors.email={0} is an invalid e-mail address.
errors.currency=The '{0}' field should be a dollar amount.
# -- User defined Error Messages
request.msg.success={0} - Request is saved successfully
tbu.errors.tbuName=TBU Name
ActionMessage class’s constructor is overloaded to take multiple values. So if we pass 2 values the second value that we pass will come and sit in {0}. In our example we are passing the request key which will be shown to the user.
Output:
123753 - Request is saved successfully
If we don’t pass a second parameter to the ActionMessage constructor then the message shown to the user will be plain text that’s stored in the properties file.
request.msg.completed.success=Full Return Request is completed successfully
This is how we display the success message on page.
<html:messages id="message" message="true" property="message">
<tr>
<td class="rmt-passMsg">
<B><bean:write name="message" /></B>
</td>
</tr>
</html:messages>
Explanation:
ActionMessages messages = new ActionMessages();
messages.add("message",msg);
As you can see the message (key) we use the same key in the html:messages tag in id and property tags.
Error Messages:
Creating custom exceptions.
Xerces – API:
http://xerces.apache.org/xerces-j/apiDocs/index.html
All our custom exception messages that are shown to the user are stored in an XML file (ABCExceptions.XML).
<?xml version="1.0" encoding="UTF-8"?>
<EXCEPTION_ROOT>
<EXCEPTION CODE="ABC_BENEF_1000">
<EXCEPTION_MESSAGE>
Beneficiary is not saved
</EXCEPTION_MESSAGE>
</EXCEPTION>
<EXCEPTION CODE="ABC_BENEF_1001">
<EXCEPTION_MESSAGE>
No records found
</EXCEPTION_MESSAGE>
</EXCEPTION>
</EXCEPTION_ROOT>
public class ABCExceptionHandlerServlet extends HttpServlet{
public void init() throws ServletException{
new ABCExceptionHandler();
}
}
public class ABCExceptionHandler {
private static String strConfigFile = "ABCExceptions.XML";
private static Document objDocument = null;
private static DOMParser objDOMParser = null;
private static Node objRoot = null;
public ABCExceptionHandler() {
initialize(strConfigFile);
}
public void initialize(String strFileName) {
try {
InputStream is = this.getClass().getResourceAsStream(strConfigFile); org.xml.sax.InputSource isrc = new org.xml.sax.InputSource(is);
objDOMParser = new DOMParser();
objDOMParser.parse(isrc);
objDocument = objDOMParser.getDocument();
objRoot = objDocument.getDocumentElement();
} catch (Exception e) {
System.err.println(e);
}
}
}
We have an exception bean ABCExceptionBean where we have
private String exception_Code = null;
private String exception_Message = null;
private String exception_Type = null;
We pass the exception_Code and exception_Type to the constructor of ABCExceptionBean class from the action classes.
Example:
throw new ABCExceptionBean("ABC_BENEF_1000", "AE");
public class ABCExceptionBean extends Exception {
Constructor:
public ABCExceptionBean(String strExceptionCode,
String strExceptionType) {
super();
if (exception_Code == null exception_Type == null) {
this.exception_Code = strExceptionCode;
this.exception_Type = strExceptionType;
}
}
exception_Type -->The 2 different exception types are “AE” and “SE” meaning Application Exception and System Exception. System Exception is nothing but those coming from database like SQL exceptions.
In the action classes, inside the catch block we call the handleException() method of ABCExceptionHandler class by passing the ABCExceptionBean.
catch (ABCExceptionBean objABCExceptionBean) {
objABCExceptionBean = ABCExceptionHandler
.handleException(objABCExceptionBean);
request.setAttribute("errMsg", objGTEMExceptionBean);
}
Note:
As you can see we are setting the error bean object as a request attribute which will be used in the JSP page.
Implementation of ABCExceptionHandler’s handleException method.
public static ABCExceptionBean handleException(
ABCExceptionBean objABCExceptionBean) {
Node contextNode = null;
String strCodeValue = null;
String strExcType = null;
// Get the type of the exception
strExcType = objABCExceptionBean.getException_Type();
if (strExcType != null && !(strExcType.equals(""))) {
if (strExcType.equalsIgnoreCase("SE")) {
objABCExceptionBean
.setException_Message("Exception Occurred while saving, Please enter correct data and try again!");
}
}
strCodeValue = objGTEMExceptionBean.getException_Code();
if (strExcType != null && !(strExcType.equals(""))) {
if (strExcType.equalsIgnoreCase("AE")) {
objABCExceptionBean = getErrorMessage(strCodeValue, objABCExceptionBean);
}
}
return objABCExceptionBean;
}
public static ABCExceptionBean getErrorMessage(String queryName, ABCExceptionBean objABCExceptionBean) {
String errorMessage = null;
try{
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String expression ="EXCEPTION_ROOT/EXCEPTION[@CODE='"+queryName+"']/EXCEPTION_MESSAGE/text()";
XPathExpression expr = xpath.compile(expression);
NodeList nodes = (NodeList) expr.evaluate(objDocument, XPathConstants.NODESET);
errorMessage = nodes.item(0).getNodeValue();
objABCExceptionBean.setException_Message(errorMessage);
System.out.println(errorMessage);
}
catch(Exception ex) {
ex.printStackTrace();
System.out.println("......Error while parsing.....");
}
return objABCExceptionBean;
}
}
This is how we set the exception bean in request attribute, in the catch block of action class.
request.setAttribute("errMsg", objGTEMExceptionBean);
Error.jsp
<logic:present name="errMsg" scope="request">
<tr>
<td class="rmt-errMsg">
<B> <bean:write name="errMsg" property="exception_Message" /> </B>
</td>
</tr>
</logic:present>
We are specifying the scope in the logic:present tag and the property which holds the error message in the bean:write tag.
How to display the validation.xml error messages.?
<td class="rmt-errMsg"><html:errors /></td>
Including <html:errors /> will display all the error messages from validation.xml to the jsp page.
How to set the right color for Success and Error messages?
Having different colors for success and error messages will help the user to differentiate the messages.
Green color for Success and
Red color for Error messages will be more appropriate.
How to display the desired color to the user?
We can make use of CSS styling to get the color effect.
<td class="rmt-errMsg"><html:errors /></td>
Here we are specifying the class attribute which is defined in a CSS file. We include the CSS file in the JSP page at the beginning of the page like,
<link rel="stylesheet" type="text/css" href="css/rmtPortal_styles.css" />
rmtPortal_styles.css:
.rmt-passMsg{
font-family:verdana,arial;
color:#008000;
font-size:12px;
font-weight:bold;
}
.rmt-errMsg{
font-family:verdana,arial;
font-size:12px;
color:#FF0000;
font-weight:bold;
}
Have a property (SaveStatus) and set the value to 1(means true) at the end of the method in insert/update functionality (methods). Set the same property to 2 inside the catch block which means we caught an exception.
This method is part of DAO which interacts with the database
public RequestDTO updateRequest(RequestDTO objRequestDTO) {
try{
// code block
objRequestDTO.setSaveStatus(1);
}
catch (Exception ex) {
ABCLogger.ABCLog(CLASS_NAME,
"In updateRequestDetails()->Exception:" + ex.getMessage(),
ABCLogger.ERROR);
ex.printStackTrace();
objRequestDTO.setSaveStatus(2);
} finally {
DBUtility.releaseConnection(objConnection);
}
}
In action class based on the status set in DAO we generate the success/error message.
if (saveStatus != null && saveStatus != 0)
{
if(saveStatus.toString().equals(ABCApplicationConstants.ABCCONSTANTS_ABCSTATUS_ACTIVE)){
ActionMessages messages = getMessages(request);
if(messages == null) messages = new ActionMessages(); ActionMessage msg = new ActionMessage("request.msg.success",objRequestDTO.getRequestDetailsKey());
messages.add("message",msg);
saveMessages(request,messages);
}
else{
objRequestForm.setRequestDetailsKey(null);
throw new ABCExceptionBean("ABC_REQUEST_1000", "AE");
}
}
We got something called MessageResources.properties file where we usually store the name/value pairs.
Struts-config entry for MessageResources.properties:
<message-resources null="false" parameter="resources.MessageResources"/>
Entry in Message Resources.properties file:
# -- Struts Validator Error Messages
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.byte={0} must be a number.
errors.email={0} is an invalid e-mail address.
errors.currency=The '{0}' field should be a dollar amount.
# -- User defined Error Messages
request.msg.success={0} - Request is saved successfully
tbu.errors.tbuName=TBU Name
ActionMessage class’s constructor is overloaded to take multiple values. So if we pass 2 values the second value that we pass will come and sit in {0}. In our example we are passing the request key which will be shown to the user.
Output:
123753 - Request is saved successfully
If we don’t pass a second parameter to the ActionMessage constructor then the message shown to the user will be plain text that’s stored in the properties file.
request.msg.completed.success=Full Return Request is completed successfully
This is how we display the success message on page.
<html:messages id="message" message="true" property="message">
<tr>
<td class="rmt-passMsg">
<B><bean:write name="message" /></B>
</td>
</tr>
</html:messages>
Explanation:
ActionMessages messages = new ActionMessages();
messages.add("message",msg);
As you can see the message (key) we use the same key in the html:messages tag in id and property tags.
Error Messages:
Creating custom exceptions.
Xerces – API:
http://xerces.apache.org/xerces-j/apiDocs/index.html
All our custom exception messages that are shown to the user are stored in an XML file (ABCExceptions.XML).
<?xml version="1.0" encoding="UTF-8"?>
<EXCEPTION_ROOT>
<EXCEPTION CODE="ABC_BENEF_1000">
<EXCEPTION_MESSAGE>
Beneficiary is not saved
</EXCEPTION_MESSAGE>
</EXCEPTION>
<EXCEPTION CODE="ABC_BENEF_1001">
<EXCEPTION_MESSAGE>
No records found
</EXCEPTION_MESSAGE>
</EXCEPTION>
</EXCEPTION_ROOT>
public class ABCExceptionHandlerServlet extends HttpServlet{
public void init() throws ServletException{
new ABCExceptionHandler();
}
}
public class ABCExceptionHandler {
private static String strConfigFile = "ABCExceptions.XML";
private static Document objDocument = null;
private static DOMParser objDOMParser = null;
private static Node objRoot = null;
public ABCExceptionHandler() {
initialize(strConfigFile);
}
public void initialize(String strFileName) {
try {
InputStream is = this.getClass().getResourceAsStream(strConfigFile); org.xml.sax.InputSource isrc = new org.xml.sax.InputSource(is);
objDOMParser = new DOMParser();
objDOMParser.parse(isrc);
objDocument = objDOMParser.getDocument();
objRoot = objDocument.getDocumentElement();
} catch (Exception e) {
System.err.println(e);
}
}
}
We have an exception bean ABCExceptionBean where we have
private String exception_Code = null;
private String exception_Message = null;
private String exception_Type = null;
We pass the exception_Code and exception_Type to the constructor of ABCExceptionBean class from the action classes.
Example:
throw new ABCExceptionBean("ABC_BENEF_1000", "AE");
public class ABCExceptionBean extends Exception {
Constructor:
public ABCExceptionBean(String strExceptionCode,
String strExceptionType) {
super();
if (exception_Code == null exception_Type == null) {
this.exception_Code = strExceptionCode;
this.exception_Type = strExceptionType;
}
}
exception_Type -->The 2 different exception types are “AE” and “SE” meaning Application Exception and System Exception. System Exception is nothing but those coming from database like SQL exceptions.
In the action classes, inside the catch block we call the handleException() method of ABCExceptionHandler class by passing the ABCExceptionBean.
catch (ABCExceptionBean objABCExceptionBean) {
objABCExceptionBean = ABCExceptionHandler
.handleException(objABCExceptionBean);
request.setAttribute("errMsg", objGTEMExceptionBean);
}
Note:
As you can see we are setting the error bean object as a request attribute which will be used in the JSP page.
Implementation of ABCExceptionHandler’s handleException method.
public static ABCExceptionBean handleException(
ABCExceptionBean objABCExceptionBean) {
Node contextNode = null;
String strCodeValue = null;
String strExcType = null;
// Get the type of the exception
strExcType = objABCExceptionBean.getException_Type();
if (strExcType != null && !(strExcType.equals(""))) {
if (strExcType.equalsIgnoreCase("SE")) {
objABCExceptionBean
.setException_Message("Exception Occurred while saving, Please enter correct data and try again!");
}
}
strCodeValue = objGTEMExceptionBean.getException_Code();
if (strExcType != null && !(strExcType.equals(""))) {
if (strExcType.equalsIgnoreCase("AE")) {
objABCExceptionBean = getErrorMessage(strCodeValue, objABCExceptionBean);
}
}
return objABCExceptionBean;
}
public static ABCExceptionBean getErrorMessage(String queryName, ABCExceptionBean objABCExceptionBean) {
String errorMessage = null;
try{
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String expression ="EXCEPTION_ROOT/EXCEPTION[@CODE='"+queryName+"']/EXCEPTION_MESSAGE/text()";
XPathExpression expr = xpath.compile(expression);
NodeList nodes = (NodeList) expr.evaluate(objDocument, XPathConstants.NODESET);
errorMessage = nodes.item(0).getNodeValue();
objABCExceptionBean.setException_Message(errorMessage);
System.out.println(errorMessage);
}
catch(Exception ex) {
ex.printStackTrace();
System.out.println("......Error while parsing.....");
}
return objABCExceptionBean;
}
}
This is how we set the exception bean in request attribute, in the catch block of action class.
request.setAttribute("errMsg", objGTEMExceptionBean);
Error.jsp
<logic:present name="errMsg" scope="request">
<tr>
<td class="rmt-errMsg">
<B> <bean:write name="errMsg" property="exception_Message" /> </B>
</td>
</tr>
</logic:present>
We are specifying the scope in the logic:present tag and the property which holds the error message in the bean:write tag.
How to display the validation.xml error messages.?
<td class="rmt-errMsg"><html:errors /></td>
Including <html:errors /> will display all the error messages from validation.xml to the jsp page.
How to set the right color for Success and Error messages?
Having different colors for success and error messages will help the user to differentiate the messages.
Green color for Success and
Red color for Error messages will be more appropriate.
How to display the desired color to the user?
We can make use of CSS styling to get the color effect.
<td class="rmt-errMsg"><html:errors /></td>
Here we are specifying the class attribute which is defined in a CSS file. We include the CSS file in the JSP page at the beginning of the page like,
<link rel="stylesheet" type="text/css" href="css/rmtPortal_styles.css" />
rmtPortal_styles.css:
.rmt-passMsg{
font-family:verdana,arial;
color:#008000;
font-size:12px;
font-weight:bold;
}
.rmt-errMsg{
font-family:verdana,arial;
font-size:12px;
color:#FF0000;
font-weight:bold;
}
1 Comments:
Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Telefone VoIP, I hope you enjoy. The address is http://telefone-voip.blogspot.com. A hug.
Post a Comment
<< Home