May 15, 2021 Struts2
Struts provides an easier way to handle uncaught exceptions and redirect users to specialized error pages. You can easily configure Struts to display different error pages for different exceptions.
package cn.w3cschool.struts2; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport{ private String name; public String execute(){ String x = null; x = x.substring(0); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Let's keep HelloWorld.jsp content as follows:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Hello World</title> </head> <body> Hello World, <s:property value="name"/> </body> </html>
Here's .jsp index content:
<%@ 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> <title>Hello World</title> </head> <body> <h1>Hello World From Struts2</h1> <form action="hello"> <label for="name">Please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
Your struts .xml as follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="cn.w3cschool.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
Now right-click on the project
name, and
then click "Export" and "WAR File" to create
a WAR
file. T
he WAR file is then deployed in Tomcat's webapps directory. F
inally, start the Tomcat server and try to access the URL http://localhost:8080/HelloWorldStruts2/index.jsp.
The following page is displayed:
Enter the value "Struts2" and submit the page. Y ou should see the following pages:
As the example above shows, the default exception interceptor works very well with exceptions. N ow let's create a dedicated error page for the exception, a file .jsp Error, that contains the following:
<%@ 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> <title></title> </head> <body> This is my custom error page </body> </html>
Now, let's configure Struts to use this error page in exceptional cases, and modify the struts .xml following:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="cn.w3cschool.struts2.HelloWorldAction" method="execute"> <exception-mapping exception="java.lang.NullPointerException" result="error" /> <result name="success">/HelloWorld.jsp</result> <result name="error">/Error.jsp</result> </action> </package> </struts>
As the example above shows, we have now configured Struts to use a dedicated Error tool for NullPointerException .jsp. I f you run the program again now, you'll see the following output page:
In addition, the Struts2 framework comes with a "log" interceptor to record exceptions. B y enabling loggers to record uncaught exceptions, we can easily view stack trace records and find out what went wrong.
We've seen how to handle action-specific exceptions. N ow we can set a global exception, which will apply to all action. F or example, in order to catch the same NullPointerException exception, we can A dd the hashtags to the global-exception-mappings... T ag and post a file .xml the struts... A dd the hashtag to the label... L abels, as follows:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <global-exception-mappings> <exception-mapping exception="java.lang.NullPointerException" result="error" /> </global-exception-mappings> <action name="hello" class="cn.w3cschool.struts2.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> <result name="error">/Error.jsp</result> </action> </package> </struts>