Struts 2 简明教程

Struts 2 - Exception Handling

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

Struts provides an easier way to handle uncaught exception and redirect users to a dedicated error page. You can easily configure Struts to have different error pages for different exceptions.

Struts 通过使用“exception”拦截器使异常处理变得容易。“exception”拦截器包含在默认堆栈中,因此你不必做任何额外的事情来配置它。它开箱即用,你随时可以使用。

Struts makes the exception handling easy by the use of the "exception" interceptor. The "exception" interceptor is included as part of the default stack, so you don’t have to do anything extra to configure it. It is available out-of-the-box ready for you to use.

我们采用修改后的 HelloWorldAction.java 文件来看看一个简单的 Hello World 示例。在这里,我们在 HelloWorldAction 操作代码中故意引入了一个 NullPointer 异常。

Let us see a simple Hello World example with some modification in HelloWorldAction.java file. Here, we deliberately introduced a NullPointer Exception in our HelloWorldAction action code.

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 的内容如下所示:

Let us keep the content of HelloWorld.jsp 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>

以下是 index.jsp 的内容:

Following is the content of 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 应如下所示:

Your struts.xml should look like −

<?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>

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

Now right click on the project name and click Export > WAR File to create a War file. Then deploy this WAR in the Tomcat’s webapps directory. Finally, start Tomcat server and try to access URL http://localhost:8080/HelloWorldStruts2/index.jsp. This will produce the following screen −

helloworldstruts4

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

Enter a value "Struts2" and submit the page. You should see the following page −

helloworldstruts16

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

As shown in the above example, the default exception interceptor does a great job of handling the exception.

我们现在为异常创建一个专门的错误页面。使用以下内容创建一个名为 Error.jsp 的文件 -

Let us now create a dedicated error page for our Exception. Create a file called Error.jsp with the following contents −

<%@ 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 ,如下所示 -

Let us now configure Struts to use this this error page in case of an exception. Let us modify the 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 = "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。如果您现在重新运行程序,您将看到以下输出 -

As shown in the example above, now we have configured Struts to use the dedicated Error.jsp for the NullPointerException. If you rerun the program now, you shall now see the following output −

helloworldstruts17

除此之外,Struts2 框架还附带一个“日志记录”拦截器用于记录异常。通过启用记录器来记录未捕获的异常,我们可以轻松查看堆栈跟踪并找出出错的原因

In addition to this, Struts2 framework comes with a "logging" interceptor to log the exceptions. By enabling the logger to log the uncaught exceptions, we can easily look at the stack trace and work out what went wrong

Global Exception Mappings

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

We have seen how we can handle action specific exception. We can set an exception globally which will apply to all the actions. For example, to catch the same NullPointerException exceptions, we could add <global-exception-mappings…​> tag inside <package…​> tag and its <result…​> tag should be added inside the <action…​> tag in struts.xml file 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 = "com.tutorialspoint.struts2.HelloWorldAction"
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
         <result name = "error">/Error.jsp</result>
      </action>

   </package>
</struts>