Struts 2 - 异常处理



Struts 提供了一种更简单的处理未捕获异常并将用户重定向到专用错误页面的方法。您可以轻松配置 Struts 为不同的异常设置不同的错误页面。

Struts 通过使用“exception”拦截器简化了异常处理。“exception”拦截器包含在默认堆栈中,因此您无需执行任何额外操作即可配置它。它开箱即用,随时可以使用。

让我们来看一个简单的 Hello World 示例,并在 HelloWorldAction.java 文件中进行一些修改。在这里,我们故意在我们的HelloWorldAction action 代码中引入了 NullPointerException。

package com.tutorialspoint.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;
   }
}

让我们保持HelloWorld.jsp的内容如下:

<%@ 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>

以下是index.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>
      <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>

您的struts.xml应该如下所示:

<?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 = "com.tutorialspoint.struts2.HelloWorldAction" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
      </action>

   </package>
</struts>

现在右键单击项目名称,然后单击导出 > WAR 文件以创建 WAR 文件。然后将此 WAR 部署到 Tomcat 的 webapps 目录中。最后,启动 Tomcat 服务器并尝试访问 URL https://127.0.0.1:8080/HelloWorldStruts2/index.jsp。这将产生以下屏幕:

Hello World Input

输入值“Struts2”并提交页面。您应该会看到以下页面:

Exception Output

如上例所示,默认异常拦截器在处理异常方面做得很好。

现在让我们为我们的异常创建一个专用的错误页面。创建一个名为Error.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>
      <title></title>
   </head>
   
   <body>
      This is my custom error page
   </body>
</html>

现在让我们配置 Struts 以在发生异常时使用此错误页面。让我们修改struts.xml如下:

<?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 = "com.tutorialspoint.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>

如上例所示,现在我们已配置 Struts 使用专用的 Error.jsp 来处理 NullPointerException。如果您现在重新运行程序,您将看到以下输出:

Hello World Output

除此之外,Struts2 框架还带有一个“logging”拦截器来记录异常。通过启用记录器来记录未捕获的异常,我们可以轻松查看堆栈跟踪并找出问题所在。

全局异常映射

我们已经看到了如何处理特定于 action 的异常。我们可以全局设置一个异常,该异常将应用于所有 action。例如,要捕获相同的 NullPointerException 异常,我们可以在<package...>标签内添加<global-exception-mappings...>标签,并且其<result...>标签应添加到struts.xml 文件中的<action...>标签内,如下所示:

<?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 = "com.tutorialspoint.struts2.HelloWorldAction" 
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
         <result name = "error">/Error.jsp</result>
      </action>

   </package>
</struts>
广告