Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 支撑角度为2';带Jetty的s PathLocationHandler(使用404错误页)_Java_Angularjs_Jetty_Embedded Jetty - Fatal编程技术网

Java 支撑角度为2';带Jetty的s PathLocationHandler(使用404错误页)

Java 支撑角度为2';带Jetty的s PathLocationHandler(使用404错误页),java,angularjs,jetty,embedded-jetty,Java,Angularjs,Jetty,Embedded Jetty,我正在尝试解决如何使用嵌入式Jetty服务器支持Angular 2的PathLocationHandler。要做到这一点,据我所知,我需要将任何404请求重定向到顶级index.html文件() 我认为这样做的方法是使用下面的代码(我实际上是在上下文xml文件中这样做的,但是代码可能更容易概念化/调试),将所有404请求重定向回/index.html的ContextHandler和ErrorHandler 我所看到的是,我的错误处理程序被完全忽略了,我不确定如何解决这个问题,或者,我也不确定应该

我正在尝试解决如何使用嵌入式Jetty服务器支持Angular 2的PathLocationHandler。要做到这一点,据我所知,我需要将任何404请求重定向到顶级index.html文件()

我认为这样做的方法是使用下面的代码(我实际上是在上下文xml文件中这样做的,但是代码可能更容易概念化/调试),将所有404请求重定向回/index.html的ContextHandler和ErrorHandler

我所看到的是,我的错误处理程序被完全忽略了,我不确定如何解决这个问题,或者,我也不确定应该如何配置


我看到的是,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错误信号?或者更可能的是,我在配置东西的过程中没有考虑到一些东西

错误配置的暗示可能是,ResourceHandler提到“对不存在的资源的请求将被释放(例如404的请求)”,但这让我不清楚下一步要做什么,除了“编写自己的处理程序”之外,我更愿意避免


非常感谢任何指点

我的头撞到了下面的东西,这确实是我想要的,尽管我仍然肯定会接受一个解释为什么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's 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>

/上下文路径
/tmp/directory-with-just-an-index.html-file
404
/index.html
/*
<?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's 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>