theymeleaf内联JavaScript框架问题 [英] theymeleaf inline javascript framework issue

查看:145
本文介绍了theymeleaf内联JavaScript框架问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script th:inline="javascript" type="text/javascript">
    //expose list data to javascript
    var listObject = /*[[${listObject}]]*/ [];
</script>

打印到文件中的替换文本与Jackson库的ObjectMapper所做的不同.

the replacement text printed into the file is different than what Jackson library's ObjectMapper does.

在上面的示例中,在Thymeleaf中,listObject将是

With Thymeleaf in above example, listObject will be

{
   "dataType":{
      "$type":"DataType",
      "$name":"STRING"
   },
   "friendlyName":"Customer Key"
}

如果我使用ObjectMapper(也与Spring @ RequestBody/@ ResponseBody一起使用)打印对象,它将为

If I print the object with ObjectMapper(which is also used with Spring @RequestBody/@ResponseBody), it will be

{
   "dataType":"STRING",
   "friendlyName":"Customer Key"
}

有没有办法强迫百里香与ObjectMapper兼容.

Is there a way I can force thymeleaf to be compatible with ObjectMapper.

推荐答案

我认为必须说些关于百里香中的Jackson和JSON内联的内容.
总而言之,考虑了切换到自定义TextInliners的可能性 3.0百里香树的里程碑.

I think this has to say something about Jackson and JSON inlining in thymeleaf.
To summarize, the possibility to switch to custom TextInliners is considered for 3.0 thymeleaf milestone.

因此,当前没有切换到Jackson json序列化的干净"方法.

So, currently there is no "clean" way to switch to Jackson json serialization.

但是,您可以做的是偷偷自己的TextInliner.那就是:

What you can do however, is sneak your own TextInliner. That is:

  1. 创建一个类org.thymeleaf.standard.inliner.StandardJavaScriptTextInliner.
  2. 实施您自己的formatEvaluationResult(Object)方法版本,
    您可以在其中调用Jackson的ObjectMapper.
  3. 将此新的StandardJavaScriptTextInliner类放在适当的位置,以便在原始类之前加载(例如,在tomcat中将其放在正确的包结构下的类dir中).
  1. Create a class org.thymeleaf.standard.inliner.StandardJavaScriptTextInliner.
  2. Implement your own version of formatEvaluationResult(Object) method,
    where you can call the Jackson ObjectMapper .
  3. Put this new StandardJavaScriptTextInliner class in a proper place, so that it is loaded before the original class (f.e. in tomcat put it in classes dir under correct package structure).

这篇关于theymeleaf内联JavaScript框架问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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