Saturday, 24 October 2015

Struts2: Exception mapping

If you don’t catch any exception in action method, struts2 provides a facility to catch and handle uncatched exceptions. By using exception-mapping element you can map uncatched exceptions to specific views.

<action name="login" class="strutstutorial.actions.LoginAction">
         <exception-mapping result="error" exception="java.lang.Exception" />

         <result name="success">/success.jsp</result>
         <result name="input">/login.jsp</result>
         <result name="error">/error.jsp</result>
</action>

In above case, java.lang.Exception mapped to result error, which in turn mapped to error.jsp file. It catch all kind of exceptions which are subclasses of Exception.

In following application, action method throws ArithmeticException, which is caught by exception-mapping element.


error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <h1>Sorry, We can't process your request now</h1>
</body>
</html>


success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <h1>Login successfull</h1>
</body>
</html>


LoginAction.java

package strutstutorial.actions;

public class LoginAction{
  public String execute() throws Exception{
    int j = 10/0;
    return "success";
  }
}


struts.xml

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <package name="default" extends="struts-default">
    <action name="login" class="strutstutorial.actions.LoginAction">
      <exception-mapping result="error" exception="java.lang.Exception" />

      <result name="success">/success.jsp</result>
      <result name="error">/error.jsp</result>
    </action>

  </package>
</struts>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>example</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <display-name>Struts2 Demo App</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>/*</url-pattern>
    </filter-mapping>
</web-app>


Total project structure looks like following (Ignore additional files in project structure, to run this example, above files are fine).



Run following url, you will get error page, since action method returns ArithmeticException (int j = 10/0 causes ArithmeticException)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment