java - Dropwizard 提供静态 html

标签 java angularjs dropwizard

我目前正在使用 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/

相关文章:

java - 如何停止在 GUI 模式下执行的 jmeter 卡住

java - Dropwizard Graphite 上的度量聚合问题

java - 读取属性文件的 Maven 依赖项

javascript - ionic 内容滚动被禁用

css - 如何更改 angularJS 的默认 css 样式?搜索框等

angularjs - Angular Analytics Splunk日志记录

java - 如何使用 Dropwizard 和 JDBI 为 SQLite 启用外键约束?

java - gui 代码适用于 Windows 和 Linux,但不适用于 Mac

java - 将 C 代码(32 位)与 Java 结合起来

java - 解析 JSON 并指定投影时的 com.mongodb.util.JSON.parse 限制