John has developed a web application but for unexpected failure he wants to handle Exception in his spring web application using ExceptionResolver class. Which one of the following code snippets John will use to configure Exception bean in spring configuration file? @BeanApublic ExceptionResolver resolveException() { ExceptionResolver resolver= new ExceptionResolver(); Properties erpProperties= new Properties();erpProperties. setProperty("DataLostException", "globalError"); erpProperties. setProperty("BadDataException", "error");erpProperties. setProperty("NullPointerException", "globalError");resolver.setExceptionMappings(erpProperties); resolver.setDefaultErrorView("404-page"); resolver.setExceptionAttribute("exc");return resolver;
时间: 2024-03-21 14:41:49 浏览: 17
The following code snippet shows how John can configure the Exception bean in the Spring configuration file to handle exceptions in his web application using ExceptionResolver class:
```
@Bean
public ExceptionResolver resolveException() {
ExceptionResolver resolver = new ExceptionResolver();
Properties erpProperties = new Properties();
erpProperties.setProperty("DataLostException", "globalError");
erpProperties.setProperty("BadDataException", "error");
erpProperties.setProperty("NullPointerException", "globalError");
resolver.setExceptionMappings(erpProperties);
resolver.setDefaultErrorView("404-page");
resolver.setExceptionAttribute("exc");
return resolver;
}
```
This code snippet defines a bean named `resolveException` of type `ExceptionResolver`. The `setExceptionMappings` method is used to map specific exception types to error views. In this case, `DataLostException` is mapped to `globalError`, `BadDataException` is mapped to `error`, and `NullPointerException` is mapped to `globalError`. The `setDefaultErrorView` method sets the default error view to `404-page`. Finally, the `setExceptionAttribute` method sets the name of the attribute that will contain the exception object in the error view.