2023年11月29日发(作者:)

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 are like that provide pre-processing of request and post-processing of application response. Struts2 provides

exception handling support through ionMappingInterceptor interceptor. This

interceptor is part of and interceptors stack, so we don’t need to configure them for our

basicStackdefaultStack

application usage.

我们知道器就像 ,提供请求的预处理和应⽤程序响应的后处理。 Struts2通

ionMappingInterceptor拦截器提供异常处理⽀持。 该拦截器

拦截器堆栈的⼀部分,因此我们⽆需为应⽤程序使⽤配置它们。

basicStackdefaultStack

We can provide exception handling at package level through and and we can use

global-exception-mappingsglobal-results

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项⽬。 我们的项⽬结构如下图所⽰。

Struts2Web应⽤程序配置 (Web Application Configuration for Struts2)

Add struts2 dependency in file like below.

如下所⽰在⽂件中添加struts2依赖项。

xmlns="/xml/ns/javaee"

xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_"

version="3.0">

Struts2Exception

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"/dtds/">

/

/runtime_

/

/

runtime_

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

struts2异常处理流程