2013-03-21 53 views
3

我想建立一個頁面沒有找到抓在我的春節WebMVCConfig位其沒有工作..Spring JavaConfig不捕獲PageNotFound?

這裏是我的配置:

@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; 
    } 
} 

現在如果我把在未映射我一個網址會認爲它會轉到我的404.jsp頁面?

+0

我不知道,是嗎? – 2013-03-21 15:14:18

回答

0

配置自定義錯誤頁面(404或任何其他錯誤代碼)的方式是在web.xml ...

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

URL可以是靜態頁面,定製JSP甚至一個Spring控制器。

+3

我不能在JavaConfig中做到這一點? – SJS 2013-03-21 15:43:13

+1

看起來像有一張票可以打開,但它尚未完成。 https://java.net/jira/browse/SERVLET_SPEC-50 – leojh 2013-12-19 04:28:36

0

您可以使用@ExceptionHandler註釋方法或@ControllerAdvice類。

在論壇的this post中描述了幾種方法。

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

+0

我嘗試了他們兩個,並且是不對的。它不在AS7中工作 – SJS 2013-03-22 13:06:00

相關問題