内联 javascript 与 thymeleaf 转义字符

Inline javascript with thymeleaf escaping character

我使用百里香和 spring。 我尝试做内联 javascript.

<script th:inline="javascript">

    $("#genericTable").bootstrapTable({
        url: /*[[${url}]]*/ 'generic',
        ...
     });    

我在服务器端做

 model.addAttribute("url", "/rest/vehicles");

我明白了

url: "\/rest\/vehicles",

为什么要在字符串中添加一些字符?

编辑

url: /*[[@{${url}}]]*/ 'generic',

第一个/好像去掉了,所以调用无效...

您可以尝试这样的操作:

<script type="text/javascript" th:inline="javascript">
  /*<![CDATA[*/
    $("#genericTable").bootstrapTable({
        url: /*[[${url}]]*/ 'generic',
        ...
    });  
  /*]]>*/
</script>

[(...)] 应该有帮助

我面临的问题示例:

$.getJSON('[[@{/management/users/search/unit/}]]' + value, function(data) {

转化为:

$.getJSON('"\/management\/users\/search\/unit\/"' + value, function(data) {

使用[(...)]:

$.getJSON('[(@{/management/users/search/unit/})]' + value, function(data) {

转化为:

$.getJSON('/management/users/search/unit/' + value, function(data) { 

来自Thymeleaf 3.0 docs

Note that, while [[...]] corresponds to th:text (i.e. result will be HTML-escaped), [(...)] corresponds to th:utext and will not perform any HTML-escaping.

我知道这个问题有点老,但答案并没有解决问题,还是和我遇到的一样。要删除转义,我建议编写这样的代码(这里的双引号仅用于 javascript 目的):

<script type="text/javascript" th:inline="javascript">
  /*<![CDATA[*/
    $("#genericTable").bootstrapTable({
        url: "[(@{ ${url} })]",
        ...
    });  
  /*]]>*/
</script>