如何在HTML
问题描述:
编写XML我想使用谷歌代码美化和Thymeleaf我的Web应用程序(W /春季启动),以在页面上显示以下XML:如何在HTML
<?xml version="1.0"?>
<users>
<user uid="user12" name="Matt" mail="[email protected]"/>
</users>
所以我写了下面的HTML:
<pre id="code" class="prettyprint lang-xml">
<?xml version="1.0"?>
<users>
<user uid="user12" name="Matt" mail="user12@example.com"/>
</users></pre>
这工作正常,但HTML代码很脏。任何人都可以告诉我一种编写更多人类可读代码的方法吗?
答
那么,这就是HTML的工作原理,所以你需要这些HTML实体。如果您使用Thymeleaf,您可以执行的操作是将XML内容作为变量提供。如果使用th:text
等属性,Thymeleaf将自动转义这些变量的内容。例如:
<pre id="code" class="prettyprint lang-xml" th:text="${users}">
</pre>
这意味着您必须在某处定义users
变量。这可以从Spring控制器完成。你可以创建一个XML文件,并读取它作为一个字符串将其传递到模型,例如:
@GetMapping
public ModelAndView getPage() throws IOException {
Resource resource = new ClassPathResource("users.xml");
return new ModelAndView("index", "users", FileUtils.readFileToString(resource.getFile(), Charset.defaultCharset()));
}
你也许可以将文件移动阅读代码的地方分开,因为现在你会因为你的一些开销将在每次有人要求页面时读取XML文件。
谢谢!这是我正在寻找的答案。 –