问答

freemarker assign语句中参数为String[][].class 出错

作者:admin 2021-05-08 我要评论

在freemarker中要现把一个string转换成json,所以调用了fastjson的函数,但是函数要入参目标类型,也就是String[][],所以语句如下: #assign temp=com.alibaba.fastj...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

在freemarker中要现把一个string转换成json,所以调用了fastjson的函数,但是函数要入参目标类型,也就是String[][],所以语句如下:

<#assign temp=com.alibaba.fastjson.JSON.parseObject(frame[1],String[][].class)>

这是被转的字符串"[["h","gray_background"],["b"],["i"],["_"],["s"],["c"]]"

错误详情

FreeMarker template error:
Template inclusion failed (for parameter value "./text.ftl"):
There was an error loading the template on an earlier attempt; see cause exception.

----
FTL stack trace ("~" means nesting-related):
    - Failed at: #include "./text.ftl"  [in template "html/macroHeaders.ftl" at line 2, column 1]
    - Reached through: #include "macroHeaders.ftl"  [in template "html/page.ftl" at line 1, column 1]
----

Java stack trace (for programmers):
----
freemarker.core._MiscTemplateException: [... Exception message was already printed; see it above ...]
    at freemarker.core.Include.accept(Include.java:164)
    at freemarker.core.Environment.visit(Environment.java:334)
    at freemarker.core.Environment.visit(Environment.java:340)
    at freemarker.core.Environment.include(Environment.java:2925)
    at freemarker.core.Include.accept(Include.java:171)
    at freemarker.core.Environment.visit(Environment.java:334)
    at freemarker.core.Environment.visit(Environment.java:340)
    at freemarker.core.Environment.process(Environment.java:313)
    at freemarker.template.Template.process(Template.java:383)
    at ljj.utils.FreemarkUtil.generate(FreemarkUtil.java:44)
    at ljj.generaAll.getArticle(generaAll.java:104)
    at ljj.generaAll.main(generaAll.java:55)
Caused by: java.io.IOException: There was an error loading the template on an earlier attempt; see cause exception.
    at freemarker.cache.TemplateCache.newIOException(TemplateCache.java:485)
    at freemarker.cache.TemplateCache.throwLoadFailedException(TemplateCache.java:501)
    at freemarker.cache.TemplateCache.getTemplateInternal(TemplateCache.java:361)
    at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:292)
    at freemarker.template.Configuration.getTemplate(Configuration.java:2822)
    at freemarker.core.Environment.getTemplateForInclusion(Environment.java:2883)
    at freemarker.core.Include.accept(Include.java:162)
    ... 11 more
Caused by: freemarker.core.ParseException: Syntax error in template "html/text.ftl" in line 6, column 89:
Encountered "]", but was expecting one of:
    <STRING_LITERAL>
    <RAW_STRING>
    "false"
    "true"
    <INTEGER>
    <DECIMAL>
    "."
    "+"
    "-"
    "!"
    "["
    "("
    "{"
    <ID>
    at freemarker.core.FMParser.generateParseException(FMParser.java:6120)
    at freemarker.core.FMParser.jj_consume_token(FMParser.java:5979)
    at freemarker.core.FMParser.UnaryExpression(FMParser.java:712)
    at freemarker.core.FMParser.MultiplicativeExpression(FMParser.java:822)
    at freemarker.core.FMParser.AdditiveExpression(FMParser.java:774)
    at freemarker.core.FMParser.RangeExpression(FMParser.java:950)
    at freemarker.core.FMParser.RelationalExpression(FMParser.java:902)
    at freemarker.core.FMParser.EqualityExpression(FMParser.java:865)
    at freemarker.core.FMParser.AndExpression(FMParser.java:1017)
    at freemarker.core.FMParser.OrExpression(FMParser.java:1039)
    at freemarker.core.FMParser.Expression(FMParser.java:552)
    at freemarker.core.FMParser.DynamicKey(FMParser.java:1481)
    at freemarker.core.FMParser.PrimaryExpression(FMParser.java:587)
    at freemarker.core.FMParser.UnaryExpression(FMParser.java:707)
    at freemarker.core.FMParser.MultiplicativeExpression(FMParser.java:822)
    at freemarker.core.FMParser.AdditiveExpression(FMParser.java:774)
    at freemarker.core.FMParser.RangeExpression(FMParser.java:950)
    at freemarker.core.FMParser.RelationalExpression(FMParser.java:902)
    at freemarker.core.FMParser.EqualityExpression(FMParser.java:865)
    at freemarker.core.FMParser.AndExpression(FMParser.java:1017)
    at freemarker.core.FMParser.OrExpression(FMParser.java:1039)
Process finished with exit code -1

ftl模板文件

<#macro text content>
    <#list content as frame>
        <#if frame?size = 2 && !frame[1]?is_string>
            <span style="
                    word-wrap:break-word;
                    <#assign temp=com.alibaba.fastjson.JSON.parseObject(frame[1],String[][].class)>
            <#list temp as eachStyle>
                <#switch eachStyle[0]>
                    <#case "b">
                            font-weight:600;
                        <#break >
                    <#case "i">
                            font-style:italic;
                        <#break >
                </#switch>
            </#list>
                    ">${frame[0]}</span>
        <#else>
            ${frame[0]}
        </#if>
    </#list>
</#macro>

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • freemarker assign语句中参数为String[

    freemarker assign语句中参数为String[

  • 如何在开发环境实现 webpack 4 product

    如何在开发环境实现 webpack 4 product

  • 正则表达式如何获取正确的链接和文字

    正则表达式如何获取正确的链接和文字

  • .NET Core 同步和异步的差别是什么

    .NET Core 同步和异步的差别是什么

腾讯云代理商
海外云服务器