Angular 2 PathLocationHandler support with Jetty (using 404 error page)

I am trying to decide how to support Angular 2 PathLocationHandler with an integrated Jetty server. To do this, as I understand it, I need to redirect any 404 request to the top-level index.html file ( https://stackoverflow.com/a/135479/ )

I figured out how to do this in order to provide ContextHandler and ErrorHandler that redirected all 404 requests back to /index.html with something like the code below (I actually do this in a context text file, but it might be easier to conceptualize / debug).

What I see is that my error handler is completely ignored, and I'm not sure how to fix it, or, alternatively, how I should configure things.


import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;

public class JettyTest {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);

        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setResourceBase("/tmp/directory-with-just-an-index.html-file");

        ContextHandler contextHandler = new ContextHandler("/context-path");

        ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
        errorHandler.addErrorPage(404, "/index.html");

        contextHandler.setHandler(resourceHandler);
        contextHandler.setErrorHandler(errorHandler);

        server.setHandler(contextHandler);

        server.start();
        System.out.println("Started!");
        server.join();
    }

}

Jetty http://localhost:8080/context-path/some-file-which-is-not-present.html, , ResourceHandler resourceBase, ...

    //no resource - try other handlers
    super.handle(target, baseRequest, request, response);
    return;

... ContextHandler, HttpChannelOverHttp 404, .

    if (!_response.isCommitted() && !_request.isHandled())
        _response.sendError(404);

, Jetty , ResourceHandler 404 - ? , , - , .

, https://www.eclipse.org/jetty/documentation/9.3.x/resource-handler.html ResourceHandler " , , (, 404s).", , , " ", .

!

+4
1

, , , , , , , ResourceHandler , ...

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class JettyTest {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);

        ServletContextHandler servletContextHandler = new ServletContextHandler();
        servletContextHandler.setContextPath("/context-path");
        servletContextHandler.setResourceBase("/tmp/directory-with-just-an-index.html-file");
        servletContextHandler.addServlet(new ServletHolder(new DefaultServlet()), "/*");

        ErrorPageErrorHandler errorHandler = new ErrorPageErrorHandler();
        errorHandler.addErrorPage(404, "/index.html");

        servletContextHandler.setErrorHandler(errorHandler);

        server.setHandler(servletContextHandler);

        server.start();
        System.out.println("Started!");
        server.join();
    }

}

... xml:)


... , - .

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure class="org.eclipse.jetty.servlet.ServletContextHandler" id="myContext">

    <Set name="contextPath">/context-path</Set>
    <Set name="resourceBase">/tmp/directory-with-just-an-index.html-file</Set>

    <!-- Direct all 404s to index.html (required by Angular PathLocationStrategy) -->
    <Set name="errorHandler">
        <New class="org.eclipse.jetty.servlet.ErrorPageErrorHandler">
            <Call name="addErrorPage">
                <Arg type="int">404</Arg>
                <Arg type="String">/index.html</Arg>
            </Call>
        </New>
    </Set>

    <Call name="addServlet">
        <Arg><New class="org.eclipse.jetty.servlet.ServletHolder">
            <Arg>
                <New class="org.eclipse.jetty.servlet.DefaultServlet"></New>
            </Arg>
        </New></Arg>
        <Arg>/*</Arg>
    </Call>

</Configure>
+2

Source: https://habr.com/ru/post/1657005/


All Articles