Struts-2-struts-exception-handling

提供:Dev Guides
移動先:案内検索

Struts 2-例外処理

*Struts* は、キャッチされない例外を処理し、専用のエラーページにユーザーをリダイレクトする簡単な方法を提供します。 例外ごとに異なるエラーページを持つようにStrutsを簡単に構成できます。

Strutsは、「例外」インターセプターを使用することにより、例外処理を容易にします。 「例外」インターセプターはデフォルトスタックの一部として含まれているため、特別な設定を行う必要はありません。 すぐに使用でき、すぐに使用できます。

HelloWorldAction.javaファイルに変更を加えた簡単なHello Worldの例を見てみましょう。 ここでは、 HelloWorldAction アクションコードにNullPointer例外を意図的に導入しました。

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

   </package>
</struts>

次に、プロジェクト名を右クリックし、[エクスポート]> [WARファイル] をクリックして、WARファイルを作成します。 次に、このWARをTomcatのwebappsディレクトリにデプロイします。 最後に、Tomcatサーバーを起動して、URL *http://localhost:8080/HelloWorldStruts2/index.jsp にアクセスしてみます。 これにより、次の画面が生成されます-

Hello World Input

値「Struts2」を入力して、ページを送信します。 次のページが表示されます-

例外出力

上記の例に示されているように、デフォルトの例外インターセプターは例外を処理する上で素晴らしい仕事をします。

ここで、例外用の専用エラーページを作成しましょう。 次の内容で 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.finddevguides.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>

上記の例に示すように、NullPointerException専用のError.jspを使用するようにStrutsを構成しました。 ここでプログラムを再実行すると、次の出力が表示されます-

Hello World出力

これに加えて、Struts2フレームワークには、例外をログに記録するための「ロギング」インターセプターが付属しています。 ロガーがキャッチされなかった例外をログに記録できるようにすることで、スタックトレースを簡単に確認して、何が問題だったかを突き止めることができます。

グローバル例外マッピング

アクション固有の例外を処理する方法を見てきました。 すべてのアクションに適用される例外をグローバルに設定できます。 たとえば、同じNullPointerException例外をキャッチするには、<package …​>タグ内に <global-exception-mappings …​> タグを追加し、その<result …​>タグを<action内に追加する必要があります…​> 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">
      <global-exception-mappings>
         <exception-mapping exception = "java.lang.NullPointerException"
         result = "error"/>
      </global-exception-mappings>

      <action name = "hello"
         class = "com.finddevguides.struts2.HelloWorldAction"
         method = "execute">
         <result name = "success">/HelloWorld.jsp</result>
         <result name = "error">/Error.jsp</result>
      </action>

   </package>
</struts>