- Struts 2 教程
- Struts2 - 首頁
- Struts2 - 基本MVC架構
- Struts2 - 概述
- Struts2 - 環境搭建
- Struts2 - 架構
- Struts2 - 示例
- Struts2 - 配置
- Struts2 - Action
- Struts2 - 攔截器
- Struts2 - 結果型別
- Struts2 - 值棧/OGNL
- Struts2 - 檔案上傳
- Struts2 - 資料庫訪問
- Struts2 - 傳送郵件
- Struts2 - 校驗
- Struts2 - 國際化
- Struts2 - 型別轉換
- Struts2 - 主題/模板
- Struts 2 - 異常處理
- Struts2 - 註解
- Struts 2 標籤
- Struts2 - 控制標籤
- Struts2 - 資料標籤
- Struts2 - 表單標籤
- Struts2 - Ajax 標籤
- Struts 2 整合
- Struts2 - Spring
- Struts2 - Tiles
- Struts2 - Hibernate
- Struts 2 有用資源
- Struts2 - 問題與解答
- Struts2 - 快速指南
- Struts2 - 有用資源
- Struts2 - 討論
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://:8080/HelloWorldStruts2/index.jsp。這將產生以下螢幕:
輸入值“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.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。如果您現在重新執行程式,您將看到以下輸出:
除此之外,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>