Thymeleaf编码问题与javascript(使用弹簧mvc) [英] Thymeleaf encoding issues with javascript (using spring mvc)

查看:154
本文介绍了Thymeleaf编码问题与javascript(使用弹簧mvc)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring mvc + thymleaf v3,当涉及html头部分定义的javascript中的某些数据时,会出现编码问题。这是在头部定义的javascript:

  function addtab(count){

var closetab ='< a href =id =close+ count +'class =close>& times;< / a>';
$(#tabul)。append('< li id =t'+ count +'class =ntabs> Tab'+ count +'& nbsp;''+ closetab + '< /立GT;');
$(#tabcontent)。append('< p id =c'+ count +'> Tab Content'+ count +'< / p>');

问题是用于仅转义html属性的字符串的撇号被替换为html encoding',并在浏览器客户端收到以下结果:

  function addtab(count){

var closetab ='< a href =id =close&#39; + count +&#39;类= 关闭 >&安培;倍;< / A>;
$(#tabul)。append('< li id =t&#39; + count +&#39;class =ntabs> Tab'+ count +'& ;&安培; NBSP; '+ closetab +' < /立GT;');
$(#tabcontent)。append('< p id =c&#39; + count +&#39;> Tab Content'+ count +'< / p>') ;

注意,只有html属性值撇号已被html编码,撇号在html标签是确定的。



这是我的web-servlet部分,其中包含thymeleaf的配置:

 < beans:bean id =templateResolverclass =org.thymeleaf.templateresolver.ServletContextTemplateResolver> 
< beans:property name =prefixvalue =/ WEB-INF / views //>
< beans:property name =suffixvalue =。html/>
< beans:property name =characterEncodingvalue =UTF-8/>
< beans:property name =templateModevalue =HTML5/>
< / beans:bean>

< beans:bean id =templateEngineclass =org.thymeleaf.spring3.SpringTemplateEngine>
< beans:property name =templateResolverref =templateResolver/>
< / beans:bean>

< beans:bean class =org.thymeleaf.spring3.view.ThymeleafViewResolver>
< beans:property name =templateEngineref =templateEngine/>
< beans:property name =characterEncodingvalue =UTF-8/>
< beans:property name =contentTypevalue =text / html; charset = UTF-8/>
< / beans:bean>

有没有什么我在thymleaf或spring中设置渲染页面的编码? / p>

我已尝试过滤器,但这些仅适用于传入的数据,并且已阅读了许多帖子,但无法找到解决方案。



谢谢。

解决方案

啊ben tbnk。我只是在一个单独的文件中外部化了JavaScript,并将其包含在通过百里香的HTML文件中,从而解决了这个问题。



ouch,很多小时失去了这样一个简单的解决方案。希望这可以帮助可能遇到这个问题的其他人。


I am using spring mvc + thymleaf v3 and have an encoding issue when it comes to some data in the javascript defined in the html head section. Here is the javascript as defined in the head :

function addtab(count) {

            var closetab = '<a href="" id="close'+count+'" class="close">&times;</a>';
            $("#tabul").append('<li id="t'+count+'" class="ntabs">Tab '+count+'&nbsp;&nbsp;'+closetab+'</li>');
            $("#tabcontent").append('<p id="c'+count+'">Tab Content '+count+'</p>');

The problem is that the apostrophes used to escape the string for the html attributes only, get replaced by the html encoding ' and results in the following when received by browser client :

function addtab(count) {

            var closetab = '<a href="" id="close&#39;+count+&#39;" class="close">&times;</a>';
            $("#tabul").append('<li id="t&#39;+count+&#39;" class="ntabs">Tab '+count+'&nbsp;&nbsp;'+closetab+'</li>');
            $("#tabcontent").append('<p id="c&#39;+count+&#39;">Tab Content '+count+'</p>');

notice that only the html attribute values apostrophes have been html encoded, the apostrophes in the value section of the html tag are ok.

Here is my web-servlet section containing the config for 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>

Is there something I am missing about the setup in thymleaf or spring for encoding of the rendered pages ?

I have tried filters but these are only for incoming data and have read many posts but am unable to find a solution.

Thanks.

解决方案

ah ben tbnk. I just externalized the javascript in a seperate file and included it in the html file which is being passed through thymeleaf and this solved the problem.

ouch, many hours lost to such a simple solution. hope this can help someone else that may come across this issue.

这篇关于Thymeleaf编码问题与javascript(使用弹簧mvc)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆