我正在使用带有freemarker集成的SparkJava构建应用程序。
我正在尝试呈现一个freemarker模板(实际上不包含任何变量):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>hello</p>
</body>
</html>
在我的控制器中,我具有以下配置:
final Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
configuration.setDirectoryForTemplateLoading(new File(contextClassLoader.getResource("www/public").toURI()));
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
configuration.setLogTemplateExceptions(false);
return new FreeMarkerEngine(configuration).render(new ModelAndView(Collections.singletonMap("",""), "index.ftl"));
但是在浏览器中的输出结果如下:
“ \ u003c!DOCTYPE html \ u003e \ n \ u003chtml lang \ u003d \“ en \” \ u003e \ n \ u003chead \ u003e \ n \ u003cmeta 字符集\ u003d \“ UTF-8 \” \ u003e \ n \ u003ctitle \ u003e标题\ u003c /标题\ u003e \ n \ u003c / head \ u003e \ n \ u003cbody \ u003e \ n \ u003cp \ u003ehello \ u003c / p \ u003e \ n \ u003c / body \ u003e \ n \ u003c / html \ u003e”
我在做错什么,如何正确显示页面?
答案 0 :(得分:0)
答案很简单:在这种情况下,route语句如下所示:
get("/hello", aMethodToRenderPage, gson::toJson);
调用toJson
指的是以下方法
@Override
public String render(Object model) {
return gson.toJson(model);
}
在ResponseTransformer中定义的。通过将字符转换为Unicode表示形式,将响应转换为JSON字符串。 要获得所需的输出(带有填充变量的HTML),请删除这样的调用,使其看起来像这样:
get("/hello", aMethodToRenderPage);