joinfaces: Best practices for error page mapping in joinfaces

Hello,

we’re developing a web application with embedded tomcat, spring-boot (no mvc) and joinfaces. We don’t have a web.xml nor a web-fragment.xml, so error page mapping is a bit difficult. We implemented error mapping as a @Bean annotated method in a @Configuration class. E.g.:

@Bean
    public ErrorPageRegistrar errorPageRegistrar() {
        return new ErrorPageRegistrar() {
            @Override
            public void registerErrorPages(ErrorPageRegistry registry) {
                registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, errorPage));
                registry.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, errorPage));
            }
        };
    }

Where errorPage is a static variable that points to the error file. Classes like FacesExceptionFilter or FullAjaxExceptionHandler from Omnifaces unfortunately do not work (since we do not have a web.xml). So is this approach really the best way to implement error page mapping in joinfaces or is there a better solution available?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 15 (3 by maintainers)

Most upvoted comments

Hi slamalotfi1987,

sure. First set up a configuration class and add a mapping method like the following:

import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.ErrorPageRegistrar;
import org.springframework.boot.web.servlet.ErrorPageRegistry;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.omnifaces.filter.FacesExceptionFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

@Configuration
public class JsfConfig {

private final String yourErrorPage = "/error/error.xhtml";

//FilterRegisBean with FacesExceptionFilter
@Bean
public FilterRegistrationBean filterRegisBean() {
    final FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
    filterRegBean.setFilter(new FacesExceptionFilter());
    filterRegBean.addUrlPatterns("/*");
    filterRegBean.setEnabled(Boolean.TRUE);
    return filterRegBean;
}

//ErrorPageRegistration, mapping method
@Bean
public ErrorPageRegistrar errorPageRegistrar() {
    return new ErrorPageRegistrar() {
        @Override
        public void registerErrorPages(ErrorPageRegistry registry) {
            registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, yourErrorPage));
            registry.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, yourErrorPage));
        }
    };
}

} 

yourErrorPage should point to a XHTML file, which should be displayed, when the error occurred (for example in META-INF/resources/error/error.xhtml). Sorry for the bad formatting of the code.