【发布时间】:2013-12-16 21:30:45
【问题描述】:
我使用的是 spring mvc + thymleaf v3,当涉及到 html head 部分中定义的 javascript 中的某些数据时,我遇到了编码问题。这是头中定义的javascript:
function addtab(count) {
var closetab = '<a href="" id="close'+count+'" class="close">×</a>';
$("#tabul").append('<li id="t'+count+'" class="ntabs">Tab '+count+' '+closetab+'</li>');
$("#tabcontent").append('<p id="c'+count+'">Tab Content '+count+'</p>');
问题在于,用于转义字符串的撇号仅用于 html 属性,被 html 编码 ' 替换,并在被浏览器客户端接收时导致以下结果:
function addtab(count) {
var closetab = '<a href="" id="close'+count+'" class="close">×</a>';
$("#tabul").append('<li id="t'+count+'" class="ntabs">Tab '+count+' '+closetab+'</li>');
$("#tabcontent").append('<p id="c'+count+'">Tab Content '+count+'</p>');
请注意,只有html属性值撇号被html编码,html标签的value部分的撇号是可以的。
这是我的 web-servlet 部分,其中包含 thymeleaf 的配置:
<beans:bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".html" />
<beans:property name="characterEncoding" value="UTF-8" />
<beans:property name="templateMode" value="HTML5" />
</beans:bean>
<beans:bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<beans:property name="templateResolver" ref="templateResolver" />
</beans:bean>
<beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<beans:property name="templateEngine" ref="templateEngine" />
<beans:property name="characterEncoding" value="UTF-8" />
<beans:property name="contentType" value="text/html; charset=UTF-8" />
</beans:bean>
关于在 thymleaf 或 spring 中对渲染页面进行编码的设置,我有什么遗漏吗?
我尝试过过滤器,但这些过滤器仅用于传入数据,并且阅读了许多帖子但无法找到解决方案。
谢谢。
【问题讨论】:
-
我创建了一个没有 thymeleaf 的测试 spring-mvc 应用程序,以查看我是否会遇到相同的编码问题而我没有。似乎 thymeleaf 是以某种方式在 javascript 代码中对 html 属性进行 html 编码。继续调查该问题。
标签: javascript spring-mvc encoding thymeleaf