gpt4 book ai didi

java - 自定义错误页面的基于 Spring Java 的配置

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:19:29 26 4
gpt4 key购买 nike

我创建了一个 CustomErrorHandler bean,它扩展了 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver 作为基于 xml 的配置的替代品,用于处理自定义错误页面.bean 类在 WebInitializer 中注册这是一个 web.xml 等价物。问题是我无法查看我在发生“404”异常时设计的自定义错误页面或我已配置为处理的任何错误页面在 bean 类中。

这是我的 CustomErrorHandler bean 类和 WebInitializer 的代码:

CustomErrorHandler.java:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

public class CustomHandlerExceptionResolver extends DefaultHandlerExceptionResolver {


private static final Logger logger = LoggerFactory
.getLogger(CustomHandlerExceptionResolver.class);

@Override
protected ModelAndView doResolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {
try {

if (ex instanceof java.lang.Throwable) {

return new ModelAndView("redirect:/uncaughtException");
} else if (ex instanceof java.lang.Exception) {

return new ModelAndView("redirect:/uncaughtException");
} else if (response.getStatus()==404) {

return new ModelAndView("redirect:/resourceNotFound");
} else if (response.getStatus()==500) {

return new ModelAndView("redirect:/resourceNotFound");
} //end webflow
//error 500
else if (ex instanceof org.springframework.context.ApplicationContextException) {
logger.warn("applicationcontextexception");
return new ModelAndView("redirect:/resourceNotFound");
}
//end error 500

//default
return super.doResolveException(request, response, handler, ex);
}
catch (Exception handlerException) {
logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
}
return null;
}


}

WebInitializer.java:

import java.util.EnumSet;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.SessionTrackingMode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.web.session.HttpSessionEventPublisher;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

import com.knowesis.sift.service.util.PropertyFileReader;

public class WebInitializer implements WebApplicationInitializer {


@Autowired
PropertyFileReader propertyfilereader;



@Override
public void onStartup(ServletContext servletContext) throws ServletException {

Set<SessionTrackingMode> modes = EnumSet.noneOf(SessionTrackingMode.class);
modes.add(SessionTrackingMode.COOKIE);

AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
// ctx.register(PropertyFileReaderConfig.class);
// ctx.register(RootContext.class);
ctx.register(SecurityConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));
servletContext.addListener(new HttpSessionEventPublisher());
//servletContext.addFilter("springExceptionFilter",ExceptionInterceptor.class);
servletContext.addFilter("springSecurityFilterChain",DelegatingFilterProxy.class);
servletContext.setSessionTrackingModes(modes);
AnnotationConfigWebApplicationContext dispatcherContext=new AnnotationConfigWebApplicationContext();
dispatcherContext.register(ServletContextInitializer.class);

/**
here i have registered the custom handler
*/
dispatcherContext.register(CustomHandlerExceptionResolver.class);

Dynamic servlet=servletContext.addServlet("appServlet",new DispatcherServlet(dispatcherContext));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);


}

}

我是否需要使用 spring 提供的任何其他 Handler 类或更改我当前的配置?。我也可能犯了一个愚蠢的错误,所以请原谅我是 spring 框架的新手。

最佳答案

在 Spring 上下文中注册您的类是不够的。

如果您使用的是 Spring > 3.1,您应该在某处为您的应用程序配置一个配置类。此类应使用 @Configuration 注释并扩展 WebMvcConfigurationSupport(或添加 @EnableWebMvc),它具有 Spring MVC webapp 的所有基本配置。

要注册您的自定义 HandlerExceptionResolver,您应该覆盖配置类中的 configureHandlerExceptionResolvers 方法。

@Override
public void configureHandlerExceptionResolvers(
List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.add(new CustomHandlerExceptionResolver());
addDefaultHandlerExceptionResolvers(exceptionResolvers);
}

关于java - 自定义错误页面的基于 Spring Java 的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30523710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com