[英]Supporting Angular 2's PathLocationHandler with Jetty (using a 404 error page)
我正在嘗試研究如何使用嵌入式Jetty服務器支持Angular 2的PathLocationHandler。 要做到這一點,據我所知,我需要將任何404請求重定向到頂級index.html文件( https://stackoverflow.com/a/34104534/797 )
我認為這樣做的方法是給ContextHandler和ErrorHandler,它將所有404請求重定向回/index.html,類似下面的代碼(我實際上是在上下文xml文件中做的,但代碼可能更容易概念化/調試)。
我看到的是我的錯誤處理程序被完全忽略了,我不知道如何解決這個問題,或者我不知道應該如何配置。
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-not-present.html等請求,我看到的是ResourceHandler在其資源庫中找不到匹配的文件,然后打電話給......
//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“請求不存在的資源被允許通過(例如沒有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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.