基于这个问题,我已经尝试了s中提到的所有解决方案。。。然而,没有一个能让人满意。。。
首先,这是我的java配置
webappinitializer.java
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
servletAppContext.setConfigLocation("com.//////my project/////////.config");
DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
enableThrowExceptionIfNoHandlerFound(dispatcher);
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
FilterRegistration.Dynamic filter = servletContext.addFilter("CHARACTER_ENCODING_FILTER", CharacterEncodingFilter.class);
filter.setInitParameter("encoding", "UTF-8");
filter.setInitParameter("forceEncoding", "true");
filter.addMappingForUrlPatterns(null, false, "/*");
}
private void enableThrowExceptionIfNoHandlerFound(ServletRegistration.Dynamic registration) {
registration.setInitParameter("throwExceptionIfNoHandlerFound", "true");
}
}
根配置.java
@Configuration
@EnableWebMvc
public class RootConfig {
Environment env;
@Autowired
public void setEnv(Environment env) {
this.env = env;
}
}
servletconfig.java文件
@Configuration
@ComponentScan("com.////myproject//")
public class ServletConfig extends WebMvcConfigurerAdapter{
@Bean
public FreeMarkerViewResolver freemarkerViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setContentType("text/html; charset=utf-8");
resolver.setCache(true);
resolver.setSuffix(".ftl");
resolver.setExposeSpringMacroHelpers(true);
return resolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig(WebApplicationContext applicationContext) {
freemarker.template.Configuration config = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_28);
config.setDefaultEncoding("UTF-8");
config.setTemplateExceptionHandler(TemplateExceptionHandler.DEBUG_HANDLER);
config.setNumberFormat("#.######");
config.setServletContextForTemplateLoading(applicationContext.getServletContext(), "/WEB-INF/views");
config.setDateFormat("yyyy-MM-dd");
config.setTimeFormat("hh:mm:ss");
config.setDateTimeFormat("yyyy-MM-dd hh:mm:ss");
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setConfiguration(config);
return freeMarkerConfigurer;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
}
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/assets/");
}
}
errorcontrolleradvice.java错误控制器
@ControllerAdvice
public class ErrorControllerAdvice {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView NotFoundPage() {
log.info("No Handler Found Exception");
return new ModelAndView("/error/404");
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(NullPointerException.class)
public ModelAndView NPEPage() {
log.info("NullPointerException");
return new ModelAndView("/error/500");
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ClassNotFoundException.class)
public ModelAndView ClassNotFoundPage() {
log.info("ClassNotFound");
return new ModelAndView("/error/500");
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(InternalServerErrorException.class)
public ModelAndView InternalServerErrorPage() {
log.info("InternalServerError");
return new ModelAndView("/error/500");
}
}
在此配置中,某些与DispatcherServlet无关的异常,例如 NullPointerException
, ClassNotFoundException
自定义错误页运行良好。。
但是,与DispatcherServlet相关的部分显示了tomcat紫色的默认错误页。。。
有趣的是在servletconfig.java中
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/assets/");
}
}
没有这个方法,自定义404错误页就可以工作!但是,如果没有这个方法,js、css资源文件将无法应用。。。。。
再次使用此方法
INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
这个日志很特别,js、css资源也很好地工作。但是,404错误页返回到tomcat基本错误页。。。
不知怎么的 SimpleUrlHandlerMapping
正在启用 DefaultExceptionHandler
... 我试过了 WebMvcConfigurationSupport
而不是 WebMvcConfigurerAdapter
修改特定bean,例如 SimpleUrlHandlerMapping
... 虽然我想知道是否有更好的解决办法。。。。。
暂无答案!
目前还没有任何答案,快来回答吧!