【问题标题】:Java Spark Framework: Use Straight HTML TemplateJava Spark 框架:使用纯 HTML 模板
【发布时间】:2016-10-27 22:57:28
【问题描述】:

使用直接 HTML 页面作为 Spark 模板的最简单方法是什么(IE,我不想通过 TemplateEngine 实现)。

我可以很好地使用模板引擎,如下所示:

Spark.get("/test", (req, res) -> new ModelAndView(map, "template.html"), new MustacheTemplateEngine());

我尝试只使用没有引擎的 ModelAndView:

Spark.get("/", (req, res) -> new ModelAndView(new HashMap(), "index.html"));

但这只是模型和视图的 toString():spark.ModelAndView@3bdadfd8

我正在考虑编写自己的引擎并实现 render() 来执行 IO 以提供 html 文件,但有更好的方法吗?

【问题讨论】:

    标签: java spark-java


    【解决方案1】:

    您不需要模板引擎。你想要的只是获取 HTML 文件的内容。

    Spark.get("/", (req, res) -> renderContent("index.html"));
    
    ...
    
    private String renderContent(String htmlFile) {
        new String(Files.readAllBytes(Paths.get(getClass().getResource(htmlFile).toURI())), StandardCharsets.UTF_8);
    }
    

    【讨论】:

      【解决方案2】:

      更新答案

      here提供的答案的帮助下,我更新了这个答案。

      get("/", (req, res) -> renderContent("index.html"));
      
      ...
      
      private String renderContent(String htmlFile) {
          try {
              // If you are using maven then your files
              // will be in a folder called resources.
              // getResource() gets that folder
              // and any files you specify.
              URL url = getClass().getResource(htmlFile);
      
              // Return a String which has all
              // the contents of the file.
              Path path = Paths.get(url.toURI());
              return new String(Files.readAllBytes(path), Charset.defaultCharset());
          } catch (IOException | URISyntaxException e) {
              // Add your own exception handlers here.
          }
          return null;
      }
      

      旧答案

      不幸的是,除了在您自己的TemplateEngine 中覆盖render() 方法外,我没有找到其他解决方案。请注意以下使用 Java NIO 读取文件内容:

      public class HTMLTemplateEngine extends TemplateEngine {
      
          @Override
          public String render(ModelAndView modelAndView) {
              try {
                  // If you are using maven then your files
                  // will be in a folder called resources.
                  // getResource() gets that folder 
                  // and any files you specify.
                  URL url = getClass().getResource("public/" + 
                              modelAndView.getViewName());
      
                  // Return a String which has all
                  // the contents of the file.
                  Path path = Paths.get(url.toURI());
                  return new String(Files.readAllBytes(path), Charset.defaultCharset());
              } catch (IOException | URISyntaxException e) {
                  // Add your own exception handlers here.
              }
              return null;
           }
      }
      

      然后,在您的路线中,您调用HTMLTemplateEngine,如下所示:

      // Render index.html on homepage
      get("/", (request, response) -> new ModelAndView(new HashMap(), "index.html"),
       new HTMLTemplateEngine());
      

      【讨论】:

      • 做得很好。您应该向 Spark 项目发送拉取请求。
      • 这看起来不对。如果您只想渲染 HTML 内容,那么您没有使用模板,因此创建 TemplateEngine 没有意义。你的HTMLTemplateEngine 不是一个真正的模板引擎。最后你会得到奇怪的new HashMap(),它只会污染代码。
      • 你的逻辑是合理的,我已经更新了我的答案以反映这一点。
      【解决方案3】:

      另一个可以提供帮助的 One Line 解决方案:

      get("/", (q, a) -> IOUtils.toString(Spark.class.getResourceAsStream("/path/to/index.html")));
      

      你需要这个导入:import spark.utils.IOUtils;

      来源:https://github.com/perwendel/spark/issues/550

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 2022-10-08
        • 1970-01-01
        • 1970-01-01
        • 2021-11-03
        相关资源
        最近更新 更多