freeMarker报错freemarker.core.InvalidReferenceException

在用freeMarker模板生成数据的时候报freemarker.core.InvalidReferenceException的异常。

报错代码如下:

<#--儿童费用包含-->
	<#if !TourUtil.isEmpty(childIncludeInfo)>
		<#assign childIncludeInfoMap=TourUtil.json2Map(childIncludeInfo) />
		"childIncludeInfo":{
			<#--儿童交通-->
			<#if !TourUtil.isEmpty(childIncludeInfoMap.trafficInfo)>
				<#assign childTrafficInfoMap= TourUtil.json2Map(childIncludeInfoMap.trafficInfo) />
childTrafficInfoMap= TourUtil.json2Map(childIncludeInfoMap.trafficInfo)

问题:

上面这一行转化失败,无法赋值给childTrafficInfoMap。

我一开始以为是传参的时候缺少这个参数,但是在核查过后发现不是参数问题。

原因:

最后发现是TourUtil.json2Map(childIncludeInfo)这里已经转化过一次了,我以为这个api只能把当层的json字符串转化为map,但实际上会把该json字符串内部所有json字符串的map转化为正常字符串的map,所以再次调用该api时就会失败。

解决:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过在模板中使用Freemarker的自定义指令来实现在表格中增加自定义字段。首先,你需要在Java代码中定义一个Freemarker的自定义指令类,具体实现如下: ```java import java.io.IOException; import java.util.List; import java.util.Map; import freemarker.core.Environment; import freemarker.template.TemplateDirectiveBody; import freemarker.template.TemplateDirectiveModel; import freemarker.template.TemplateException; import freemarker.template.TemplateModel; import freemarker.template.TemplateModelException; import freemarker.template.utility.ObjectWrapper; public class CustomTableDirective implements TemplateDirectiveModel { @Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { // 获取表格数据 List<TableInfo> tableInfoList = (List<TableInfo>) env.getVariable("tableInfoList").getAsList(); // 获取自定义字段参数 String customField = params.get("customField").toString(); // 遍历表格数据,为每个表格添加自定义字段 for (TableInfo tableInfo : tableInfoList) { tableInfo.getFields().add(customField); } // 输出表格 body.render(env.getOut()); } } ``` 然后,在你的Freemarker模板中添加以下代码: ```ftl <#assign customTableDirective = "com.example.CustomTableDirective"?new()> <#assign tableInfoList = configBuilder.getTableInfoList()> <@customTableDirective customField="customField"> <#list tableInfoList as table> <table> <thead> <tr> <#list table.getFields() as field> <th>${field}</th> </#list> </tr> </thead> <tbody> <#list table.getRows() as row> <tr> <#list table.getFields() as field> <td>${row[field]}</td> </#list> </tr> </#list> </tbody> </table> </#list> </@customTableDirective> ``` 这样,你就可以在表格中添加自定义字段了。注意,这里假设你的表格数据是通过变量`tableInfoList`传递给模板的。如果你的表格数据来源不同,需要修改相应的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值