2

What should be the approach to implement custom 404 error page in dd4t using spring MVC as the framework where the error page is created in Tridion?

The status code needs to be modified to 404 as well when this page is returned. How exactly should the Page Controller be modified to achieve this?

Also the URL in the browser should not change and should remain as is entered by the user.

2

A way to do this is to extend the AbstractPageController in such a way that when an ItemNotFoundException is thrown, you attempt to load a 404 page:

// MUST BE A Tridion Published Page
String 404Url = "/errors/404.html";
String currentUrl = HttpUtils.getCurrentURL(request);

try{

    super.showPage(model, request, response);

} catch (ItemNotFoundException e) {
    if (request.getDispatcherType() == DispatcherType.ERROR) {
        if (currentUrl.equals(404Url)) {
            LOG.error("404 page '{}' missing. Is it published from Tridion?", currentUrl);
            response.getWriter().write(currentUrl + " missing");
        } else {
            try {
                request.getRequestDispatcher(404Url).forward(request, response);
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            } catch (ServletException se) {
                LOG.error("Impossible to get dispatcher for " + 404Url + ". Performing redirect instead", se);
                sendRedirect(request,response,404Url);
            }
        } 
    } else {
        LOG.info("Page with url '{}' could not be found.", url);
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}
| improve this answer | |

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.