【问题标题】:Spring JavaConfig is not catching PageNotFound?Spring JavaConfig 没有捕获 PageNotFound?
【发布时间】:2013-03-22 06:01:27
【问题描述】:

我正在尝试在我的 Spring WebMVCConfig 位中设置一个找不到的页面,但它不起作用..

这是我的配置:

@Configuration
@EnableWebMvc
@PropertySource("classpath:application.properties")
@Import(DatabaseConfig.class)
@ImportResource("/WEB-INF/spring/applicationContext.xml")
public class WebMVCConfig extends WebMvcConfigurerAdapter {

    private static final String MESSAGE_SOURCE = "/WEB-INF/classes/messages";
    private static final Logger logger = LoggerFactory.getLogger(WebMVCConfig.class);

    @Autowired
    Environment env;

    @Bean
    public ViewResolver resolver() {
        UrlBasedViewResolver url = new UrlBasedViewResolver();
        url.setPrefix("/WEB-INF/view/");
        url.setViewClass(JstlView.class);
        url.setSuffix(".jsp");
        return url;
    }

    @Bean(name = "messageSource")
    public MessageSource configureMessageSource() {
        logger.debug("setting up message source");
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename(MESSAGE_SOURCE);
        messageSource.setCacheSeconds(5);
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver lr = new SessionLocaleResolver();
        lr.setDefaultLocale(Locale.ENGLISH);
        return lr;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        logger.debug("setting up resource handlers");
        registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        logger.debug("configureDefaultServletHandling");
        configurer.enable();
    }

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleChangeInterceptor());
    }


    public @Bean HandlerExceptionResolver exceptionResolver() {
        Properties mappings = new Properties();
        mappings.put("org.springframework.web.servlet.PageNotFound", "404");
        mappings.put(DataAccessException.class.getName(), "dataAccessFailure");
        mappings.put(TransactionException.class.getName(), "dataAccessFailure");

        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        resolver.setExceptionMappings(mappings);

        return resolver;
    }


    @Bean
    public DefaultAnnotationHandlerMapping mapping() {
        DefaultAnnotationHandlerMapping m = new DefaultAnnotationHandlerMapping();
        m.setDetectHandlersInAncestorContexts(true);
        return m;
    }
}

现在如果我输入一个未映射的 URL,我会认为它会转到我的 404.jsp 页面?

【问题讨论】:

  • 我不知道,是吗?

标签: java spring spring-mvc


【解决方案1】:

配置自定义错误页面(404或任何其他错误代码)的方式在web.xml中...

<error-page>
    <error-code>404</error-code>
    <location>/my-custom-page-not-found.html</location>
</error-page>

url可以是静态页面、自定义jsp甚至是Spring控制器。

【讨论】:

【解决方案2】:

您可以使用@ExceptionHandler 注释方法或@ControllerAdvice 类。

论坛this post描述了几种方法。

或者,您也可以参考Spring Docs - Handling exceptions 部分。

【讨论】:

  • 我都试过了,但有些不对劲。它在 AS7 中不起作用
猜你喜欢
  • 2017-05-05
  • 2013-01-03
  • 2015-10-21
  • 1970-01-01
  • 2019-09-14
  • 2014-06-17
  • 1970-01-01
  • 2015-11-14
  • 2014-02-26
相关资源
最近更新 更多