Java Spark 框架:直接使用 HTML 模板
Java Spark Framework: Use Straight HTML Template
使用直接 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 文件提供服务,但是有更好的方法吗?
更新答案
借助提供的答案 ,我更新了这个答案。
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());
您不需要模板引擎。您想要的只是获取 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);
}
另一个可以提供帮助的 One Line 解决方案:
get("/", (q, a) -> IOUtils.toString(Spark.class.getResourceAsStream("/path/to/index.html")));
您需要此导入:导入 spark.utils.IOUtils;
使用直接 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 文件提供服务,但是有更好的方法吗?
更新答案
借助提供的答案
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());
您不需要模板引擎。您想要的只是获取 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);
}
另一个可以提供帮助的 One Line 解决方案:
get("/", (q, a) -> IOUtils.toString(Spark.class.getResourceAsStream("/path/to/index.html")));
您需要此导入:导入 spark.utils.IOUtils;