struts2异常处理流程_Struts2异常处理示例教程

本文详细介绍了Struts2框架的异常处理机制,包括如何在不同级别配置异常映射,以及如何通过定制响应来处理应用中抛出的异常。

struts2异常处理流程

Exceptions are common in any application. We try to catch it and process it but sometimes our application is not able to process it and it’s thrown to the container. Struts2 provides a robust mechanism through which we can provide customized response to client whenever any exception is thrown by the application.

异常在任何应用程序中都很常见。 我们尝试捕获它并对其进行处理,但是有时我们的应用程序无法对其进行处理并将其扔到容器中。 Struts2提供了一种健壮的机制,通过该机制,我们可以在应用程序抛出任何异常时向客户端提供自定义响应。

We know that Struts2 interceptors are like servlet filters that provide pre-processing of request and post-processing of application response. Struts2 provides exception handling support through com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor interceptor. This interceptor is part of basicStack and defaultStack interceptors stack, so we don’t need to configure them for our application usage.

我们知道Struts2拦截器就像servlet过滤器一样 ,提供请求的预处理和应用程序响应的后处理。 Struts2通过com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor拦截器提供异常处理支持。 该拦截器是basicStackdefaultStack拦截器堆栈的一部分,因此我们无需为应用程序使用配置它们。

We can provide exception handling at package level through global-exception-mappings and global-results and we can use exception-mapping in action mappings for action level exception handling. We need to provide result pages to use for different exception scenarios. Let’s try to understand it’s usage with a simple application.

我们可以通过global-exception-mappingsglobal-results在程序包级别提供异常处理,并且可以在操作映射中使用异常映射进行操作级别的异常处理。 我们需要提供结果页面以用于不同的异常情况。 让我们尝试通过一个简单的应用程序来了解它的用法。

We will create a dynamic web project and then configure it as Maven project. Our project structure looks like below image.

我们将创建一个动态Web项目,然后将其配置为Maven项目。 我们的项目结构如下图所示。

Struts2的Web应用程序配置 (Web Application Configuration for Struts2)

Add struts2 dependency in pom.xml file like below.

如下所示在pom.xml文件中添加struts2依赖项。

<dependencies>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-core</artifactId>
		<version>2.3.15.1</version>
	</dependency>
</dependencies>

Struts2 filter configuration in deployment descriptor file.

部署描述符文件中的Struts2过滤器配置。

web.xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns="https://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>Struts2Exception</display-name>
	
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

动作班 (Action Classes)

We will create few action classes whose only purpose will be to throw some exceptions.

我们将创建几个动作类,它们的唯一目的是引发一些异常。

package com.journaldev.struts2.exception;

import com.opensymphony.xwork2.ActionSupport;

public class MyAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Override
	public String execute(){
		throw new NullPointerException("Mandatory data missing");
	}
}
package com.journaldev.struts2.exception;

import java.io.IOException;

import com.opensymphony.xwork2.ActionSupport;

public class MySpecialAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws IOException{
		throw new IOException("IOException occured");
	}
}

Struts2配置 (Struts2 Configuration)

We are configuring package level exception handling and action level exception handling in struts.xml configuration file.

我们正在struts.xml配置文件中配置程序包级别的异常处理和操作级别的异常处理。

struts.xml

struts.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"https://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>

<constant name="struts.convention.result.path" value="/"></constant>

<package name="user" namespace="/" extends="struts-default">

<global-results>
	<result name="exception">/exception.jsp</result>
	<result name="runtime_exception">/runtime_exception.jsp</result>
	<result name="error">/error.jsp</result>
</global-results>

<global-exception-mappings>
	<exception-mapping exception="java.lang.Exception" result="exception"></exception-mapping>
	<exception-mapping exception="java.lang.Error" result="error"></exception-mapping>
	<exception-mapping exception="java.lang.RuntimeException" result="runtime_exception"></exception-mapping>
</global-exception-mappings>

	<action name="myaction" class="com.journaldev.struts2.exception.MyAction">
	</action>
	<action name="myspecialaction" class="com.journaldev.struts2.exception.MySpecialAction">
	<exception-mapping exception="java.io.IOException" result="login"></exception-mapping>
	<result name="login">/error.jsp</result>
	</action>
</package>


</struts>

Notice that we need to define global result pages for global level exception mapping to result pages. For action level exception handling, we need to define result page in action mapping.

注意,我们需要定义全局结果页面,以将全局级别的异常映射到结果页面。 对于操作级别的异常处理,我们需要在操作映射中定义结果页面。

Since there is no exception result pages mapped for MyAction and it’s throwing NullPointerException, the closest base class with exception mapping is RuntimeException, so it should return runtime_exception.jsp page.

由于没有为MyAction映射任何异常结果页面,并且抛出NullPointerException ,所以具有异常映射关系的最接近的基类是RuntimeException ,因此它应该返回runtime_exception.jsp页面。

For MySpecialAction throwing IOException, the result page is defined and it should return error.jsp page.

对于抛出IOException MySpecialAction ,将定义结果页面,并且该页面应返回error.jsp页面。

Let’s write these JSP pages and confirm this behavior.

让我们编写这些JSP页面并确认这种行为。

JSP页面 (JSP Pages)

runtime_exception.jsp

runtime_exception.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Runtime Exception Page</title>
</head>
<body>
Some Runtime Exception Occured!!<br><br>

Runtime Exception Name: <s:property value="exception"/><br><br>

Runtime Exception Stack Trace: <br>
<s:property value="exceptionStack"/>
</body>
</html>

error.jsp

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error Page</title>
</head>
<body>
Some Error Occured!!<br><br>

Error Name: <s:property value="exception"/><br><br>

Error Stack Trace: <br>
<s:property value="exceptionStack"/>
</body>
</html>

Notice that we have defined exception.jsp as result page for any java.lang.Exception but since there was no exception that is the closest match to this, it’s not used.

请注意,我们已经将exception.jsp定义为任何java.lang.Exception结果页,但是由于没有异常与之最接近,因此未使用它。

运行应用程序 (Run the Application)

Now when we run the application, we get following pages as response confirming the exception handling behavior.

现在,当我们运行该应用程序时,我们将获得以下页面作为响应,以确认异常处理行为。

That’s all for Exception Handling in Struts2 applications, you can download the project from below link and play around with it for more learning.

这就是Struts2应用程序中异常处理的全部内容,您可以从下面的链接下载该项目并进行试用以获取更多信息。

翻译自: https://www.journaldev.com/2421/struts2-exception-handling-example-tutorial

struts2异常处理流程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值