4

When a page is not found, the DXA framework initiates a redirect to the website's error-404.html page. Instead of that, I want to redirect a static error 404 page which is available in some custom file system path.

5

In case of 404 exception DXA indeed resolves a page error-404 and shows it. Such a case is handled by @ExceptionHandler(NotFoundException.class) annotation in PageController. I don't see any way how to override it properly.

Fortunately, you have MvcData and ability to write your custom PageBuilder. Make sure that your builder is executed after default one (order > Integer.MIN_VALUE), and is a Spring Bean (e.g. annotated by @Component). Check in it if a page name or title has special marker (use any constant marker you like) and substitute page's MvcData.

@Override
public <T extends PageModel> T createPage(Page genericPage, T pageModel, Localization localization, ContentProvider contentProvider) throws ContentProviderException {
    if (pageModel.getName().contains("_Error Page Not Found")) {
        pageModel.setMvcData(MvcDataCreator.creator().fromQualifiedName("Core:NotFound").create());
    }
    return pageModel;
}

PageController will then resolve this MvcData into Core/Page/NotFound meaning that you now can place your view under this Area/Region/View.

Play with different values of MvcData to get view resolved from your module.

2

Thanks @ Tory and Alexey

I am able to override the VIEWS that will be used to show the errors criteria. I could create my VIEWS so that they redirect to the appropriate file system error page.

My custom page builder:

package com.sdl.modules.nationwide;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.sdl.webapp.main.WebAppInitializer;
import com.sdl.webapp.tridion.mapping.PageBuilder;
import com.sdl.webapp.common.api.content.ContentProvider;
import com.sdl.webapp.common.api.content.ContentProviderException;
import com.sdl.webapp.common.api.localization.Localization;
import com.sdl.webapp.common.api.model.PageModel;
import com.sdl.webapp.common.api.model.mvcdata.MvcDataCreator;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CustomPageBuilder implements PageBuilder{

    private static final Logger LOG = LoggerFactory.getLogger(WebAppInitializer.class);

      @Override
      public PageModel createPage(org.dd4t.contentmodel.Page genericPage, PageModel pageModel, Localization localization,
            ContentProvider contentProvider) throws ContentProviderException {
            LOG.debug("CustomPageBuilder::createPage checking pageModel name: {}", pageModel.getName());
            if (pageModel.getName().contains("_Error Page Not Found")) {
                  pageModel.setMvcData(MvcDataCreator.creator().fromQualifiedName("ViewsRootFolderName:NotFoundViewName").create());
            }
            return pageModel;
      }
      @Override
      public int getOrder() {
            return 1000;
      }
}

In Error404.jsp:

<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ taglib prefix="dxa" uri="http://www.sdl.com/tridion-dxa"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="nw" uri="/WEB-INF/tld/NationwideTagLibrary.tld"%>

<%
    RequestDispatcher requestDispatcher; 
    requestDispatcher = request.getRequestDispatcher("/testfolder/error404.jsp");
    requestDispatcher.forward(request, response);   
%>

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.