有没有办法渲染来自后端的百里香标签?
例如,我有下面显示的json(textObj
)来自后端。
{
"title" : "Page Title",
"text" : "blah blah blah blah <div th:include=\"include::include\">my content</div>"
}
include.html
<div th:fragment="include">
My Actual content
</div>
page.html中
<h2 th:utext="${textObj.title}"/>
<th:block th:utext="${textObj.text}" />
当我渲染页面时,它直接在html源代码中显示百万美元标签,我们有什么方法可以在屏幕上渲染之前处理它们吗?
答案 0 :(得分:0)
我不确定你是如何将对象返回'page.html'的。如果要从spring加载视图,请将java模型添加到modelAndView:
@RequestMapping(value = "/page")
public ModelAndView loadPage() {
ModelAndView response = new ModelAndView();
TextObj textObj = new TextObj();
textObj.setTitle("Test title");
textObj.setText("Some text <div th:include=\"include::include\">my content</div>");
response.addObject("textObj", textObj);
response.setViewName("page");
return response;
}
如下所示:
<h2 th:utext="${textObj.title}">Sample Title</h2>
<p th:utext="${textObj.text}">Sample text</p>
对我来说很好。