我创建了一个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框架的新手。
答案 0 :(得分:2)
在Spring上下文中注册你的课程是不够的。
如果你正在使用Spring&gt; 3.1,你应该在某处为你的应用程序配置一个confuguration类。该类应使用@Configuration
注释并扩展WebMvcConfigurationSupport
(或添加@EnableWebMvc
),其中包含Spring MVC webapp的所有基本配置。
要注册自定义HandlerExceptionResolver
,您应该覆盖配置类中的configureHandlerExceptionResolvers
方法。
@Override
public void configureHandlerExceptionResolvers(
List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.add(new CustomHandlerExceptionResolver());
addDefaultHandlerExceptionResolvers(exceptionResolvers);
}