3

When a page is not found, the DXA framework initiates a redirect to the website's error-404.html page but I could not find any redirect's when the HTTP code is 500.

After inspecting the source code for the DXA core module I found that the following view is served when there's a 500 response: \META-INF\resources\WEB-INF\Views\Shared\Error\ServerError.jsp

Is there any way that I can provide my own custom view without modifying the dxa core module?

Regard, Kunal

4

Although there is no a normal way to override the view for 500 error, there is a possible workaround to solve the issue.

Since view is resolved with a ViewResolver instance, you can specify your own view resolver. There are two ways to do this:

  1. Create a bean of ViewResolver interface and mark it as primary. Then Spring should use it instead of default one. You can then inject some 'special' logic to catch the ServerError.jsp resolve process, and return your custom. I would prefer the second way though, because I am not sure how Spring will load all beans of ViewResolver, and because this way will break device-specific view resolver coming with DXA 1.5.

  2. Create an additional ViewResolver which will be called before a default one. Although, since default comes without an explicit order I am still not sure whether this will work properly. Anyway you can try something like this:

    @Bean
    public ViewResolver yourViewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver() {    
            @Override
            public View resolveViewName(String viewName, Locale locale) throws Exception {
                if (viewName.contains("ServerError")) {
                    viewName += "-special";
                }
                return super.resolveViewName(viewName, locale);
            }
        };
    
        viewResolver.setViewClass(OptionalJstlView.class);    
        // set highest order or at least higher then the default
        viewResolver.setOrder(org.springframework.core.Ordered.HIGHEST_PRECEDENCE);             
        viewResolver.setPrefix(viewResolverPrefix);
        viewResolver.setSuffix(viewResolverSuffix);
        return viewResolver;
    } 
    
  • This is exactly what I was looking for and +1 for the code sample, thanks! – Kunal Jun 15 '16 at 11:56
  • Nice example @Alexey! – Shiva Jun 15 '16 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.