如何让百万美元解析我的html实体?
我有以下内容:
<input th:name="title" th:value="Wayne’s World" />
只会生成一个带"Wayne’s World"
而不是"Wayne's world"
的输入元素。
任何让百万美元解析html实体的方法吗?
答案 0 :(得分:2)
你有两个选择。
第一的。根本不要使用html实体。只需使用\
转义特殊符号即可。请注意,您已在单引号中指定字符串值:
<input th:name="title" th:value="'Wayne\'s World'" />
第二。使用Thymeleaf的string utility进行转义xml enitites:
<input th:name="title" th:value="${#strings.escapeXml('Wayne’s World')}" />
当您的字符串值来自控制器时,请不要使用__${}__
预处理表达式。它并不需要。只需使用Thymeleaf的标准变量表达式${}
。并且不要将此表达式用单引号括起来。在下一个示例中查看${title}
变量:
<th:block th:include="row::row(attrs='value=${title}, minLength=\'.{1, 16}\', required=true, ... />
在这种情况下,您可以按原样在控制器中添加字符串值,而不进行任何转义:
public String method(ModelMap model){
...
mode.addAttribute("title", "Wayne's world");
...
}