DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Showing Generic Error Page in Struts2 Web Application

Sandeep Bhandari user avatar by
Sandeep Bhandari
·
May. 18, 11 · Interview
Like (0)
Save
Tweet
Share
8.14K Views

Join the DZone community and get the full member experience.

Join For Free

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

Web application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best CI/CD Tools for DevOps: A Review of the Top 10
  • Fixing Bottlenecks in Your Microservices App Flows
  • Choosing the Right Framework for Your Project
  • Using GPT-3 in Our Applications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: