Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

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

在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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...