Showing Generic Error Page in Struts2 Web Application
Join the DZone community and get the full member experience.
Join For FreeThe error page in any application is very important. It tells the user about the problem and shows him a description of the error. Some applications also show an error code so that the user can mention the error code while raising an incident with support team. This helps in identifying the problem faster and sometimes even Level1 (L1) support is able to resolve the problem.
Here
we are going to discuss how the coding of error page should be done in a
struts2 application. The basic flow is like the user is shown an HTML
page with various fields. The user enters input and presses the submit
button. If there is any network/database/file exception due to which the
operation can not be performed successfully, the error page needs to be
shown.
Usually if
the application is under development and a number of developers are
working on different modules, they tend to create their own version of
error pages (say one error page for search errorSearch.jsp and second
for record insertion employeeError.jsp).
All this
clutter can be avoided by using a single error page which looks for a
bean property for the error code and error message to be displayed.
Here is a
sample application to demonstrate the same. Please note this application
is for demo purposes only. We have search and insert functionality on
the home page (home.jsp). On clicking the search button, the
SearchAction class's execute gets invoked which either forwards to
success.jsp or failure.jsp (with an error message). Similar is the case
with insert record functionality.
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default" >
<action name="index">
<result name="success">/home.jsp</result>
</action>
<action name="search" class="com.example.SearchAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
<action name="insert" class="com.example.InsertAction">
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>SearchAction.java
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping></web-app>
package com.example;InsertAction.java
public class SearchAction {
private String searchText;
private String message;
public String getSearchText() {
return searchText;
}
public void setSearchText(String searchText) {
this.searchText = searchText;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
if(getSearchText().equals("guest")) {
return "success";
} else {
message="Unable to search";
return "failure";
}
}
}
package com.example;home.jsp
public class InsertAction {
private String username;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute() {
if( username != null && username.equals("guest")) {
return "success";
} else {
message="Unable to insert";
return "failure";
}
}
}
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<body>
<s:form name="search" theme="simple" action="search">
Search user <s:textfield name="searchText" />
<s:submit value="Search" />
</s:form>
<s:form name="insert" theme="simple" action="insert">
Username <s:textfield name="username" />
<s:submit value="Insert Record" />
</s:form>
</body>
</html>
success.jsp
Operation was successful
failure.jsp
<%@taglib uri="/struts-tags" prefix="s" %>After deployment, to access the application please use the following URL:
<html>
<body>
<h3><s:property value="message" /></h3>
</body>
</html>
http://localhost:8080//login.action
You can also download the application as zip file and extract the Test folder into webapps folder of your tomcat server and use the application straight away by using the URL:
http://localhost:8080/Test/login.action
The application zip file can be downloaded at:
http://www.fileserve.com/file/3fqTNn7
From http://extreme-java.blogspot.com/2011/05/showing-generic-error-page-in-struts2.html
Web application
Opinions expressed by DZone contributors are their own.
Comments