• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Struts 2 Exception Configuration

December 13, 2013 //  by Krishna Srinivasan//  Leave a Comment

Struts 2 simplifies the way we handle the exceptions. In the earlier version of Struts application, handling exceptions needs lot of work. In Struts 2, You can configure the exception type in the configuration file for the each action mapping and redirect to the custom error page. Here I have modified our hello world example to experiment exception handling. I have thrown SQlException from the action class which is configured in the configuration file with appropriate error page. Look at the below code to understand the exception handling.

1. Create Struts 2 Action

package javabeat.net.struts2;

import java.sql.SQLException;

public class Struts2HelloWorldAction {
	private String userName;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String execute() throws SQLException{
		if (true){
			throw new SQLException();
		}
		return "success";
	}
}

2. Configure struts.xml File

exception-mapping element under action element will be used for configuring the exceptions. Note that this configuration is action level, so you can configure exception pages for each action which gives more control to the developers. You can define as many as error pages for your application. However, it is good practice to write a more generic exception page for your application.

<?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>
<constant name="struts.devMode" value="true" />
<package name="hello" extends="struts-default">
<action name="Welcome" class="javabeat.net.struts2.Struts2HelloWorldAction" >
	<exception-mapping exception="java.sql.SQLException" result="error"/>
	<result name="success">Result.jsp</result>
	<result name="error">Error.jsp</result>
</action>
</package>
</struts>

3. Create Error JSP

If there is any error is thrown from the action class, by default this error page will be redirected to the user. You can customize this page to display any custom details which will be meaningful to the users.

Error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error Page</title>
</head>
<body>
This is a sample error page!!
</body>
</html>

4. Configure web.xml File

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <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>/*</url-pattern>
  </filter-mapping>
</web-app>

5. Run The Application

If you access the application http://localhost:8080/Struts2App/Welcome.action. You would see the following output in your screen.

Struts 2 Exception Handling Example Screen

Category: StrutsTag: Struts 2 Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Struts 2 Hello World Example
Next Post: Struts 2 Configuration Files Path »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact