我目前正在使用 dropwizard 和 angularjs 构建一个应用程序。我已经像这样设置了 AssetsBundle:
bootstrap.addBundle(new AssetsBundle("/assets", "/", "index.html"));
我当前的问题是我想要多个页面来服务我的index.html 页面(Angularjs 应用程序主页面)。无论如何,我可以定义一组 url 来为该 index.html 文件提供服务吗?如果我创建更多资源包,这将起作用,但这不是应该这样做的:
bootstrap.addBundle(new AssetsBundle("/assets", "/login", "index.html", "login"));
bootstrap.addBundle(new AssetsBundle("/assets", "/my-leagues", "index.html", "my-leagues"));
bootstrap.addBundle(new AssetsBundle("/assets", "/registering-leagues", "index.html", "registering-leagues"));
bootstrap.addBundle(new AssetsBundle("/assets", "/league-register/*", "index.html", "league-register"));
bootstrap.addBundle(new AssetsBundle("/assets", "/", "index.html", "home"));
我目前的目标是为/login、/my-leagues、/registering-leagues、/league-register/*(其中 * 可以是任何数字)和/提供 index.html 页面。这个 hacky 解决方案不适用于“/league-register/*” Assets ,因为该 Assets 包不支持通配符。
是否有一种简单的方法来指定某些端点以返回我的index.html 文件?
谢谢!
最佳答案
我想出了如何做到这一点,但仍然不确定这是否是最好的解决方案。我创建了一个只为我的index.html 文件提供服务的servlet。在我的运行函数中我添加了:
environment.getApplicationContext().addServlet(new ServletHolder(new BaseServlet()), "/login");
environment.getApplicationContext().addServlet(new ServletHolder(new BaseServlet()), "/my-leagues");
environment.getApplicationContext().addServlet(new ServletHolder(new BaseServlet()), "/registering-leagues");
environment.getApplicationContext().addServlet(new ServletHolder(new BaseServlet()), "/league-register/*");
我的 BaseServlet 看起来像这样:
public class BaseServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
RequestDispatcher view = req.getRequestDispatcher("/index.html");
view.forward(req, resp);
}
}
关于java - Dropwizard 提供静态 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30675879/