Over a million developers have joined DZone.

Showing Generic Error Page in Struts2 Web Application

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

The 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"?>
<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>
SearchAction.java
package com.example;

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";
}
}

}
InsertAction.java
package com.example;

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";
}
}

}
home.jsp
<%@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" %>
<html>
<body>
<h3><s:property value="message" /></h3>
</body>
</html>
After deployment, to access the application please use the following URL:
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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}