当前位置: 代码迷 >> J2EE >> 在freemaker里写了个嵌套list一直报错,网上查了好多都不管用,请各位大神给看看,很着急
  详细解决方案

在freemaker里写了个嵌套list一直报错,网上查了好多都不管用,请各位大神给看看,很着急

热度:12   发布时间:2016-04-19 22:53:01.0
在freemaker里写了个嵌套list一直报错,网上查了好多都不管用,请各位大神给看看,很着急在线等!!!!
在freemaker里写了个嵌套list一直报错,网上查了好多都不管用,请各位大神给看看,很着急在线等!!!!

freemaker代码:
<#list devicesList as device>
     ${device.ipAddress}
     ${device.hardwareVendor}
      <#list device.ipAddress as pojo2>
             ${pojo2.policyTag}
           ${pojo2.policyName}
      </#list>
</#list>

把device.ipAddress 改成固定的字符串以后不再报错,但在网上看到别人也是这样写的就没事,而且将   <#list device.ipAddress as pojo2>改为<#assign policys = device.ipAddress><#list policys as pojo2>一样报错!!

java 代码:

List<NDeviceLite> deviceLites = new ArrayList<NDeviceLite>();
for (int j = 0; j < 2; j++)
{
NDeviceLite device = new NDeviceLite();
device.setIpAddress("10.1.9.1" + j);
device.setHardwareVendor("cisco");
deviceLites.add(device);

List<ComplianceStatisticsPojo> _pojos = new ArrayList<ComplianceStatisticsPojo>();
for (int i = 1; i < 5; i++)
{
ComplianceStatisticsPojo _pojo = new ComplianceStatisticsPojo();
_pojo.setPolicyName("7.1.2.02.b(Cisco_G)");
_pojo.setPolicyTag("网络访问控制");
_pojos.add(_pojo);
}
dataMap.put("poj", _pojos);
}
dataMap.put("devicesList", deviceLites);



报错信息:
Expected collection or sequence. device.ipAddress evaluated instead to freemarker.template.SimpleScalar on line 1170, column 8 in GradeProtection.ftl.
The problematic instruction:
----------
==> list device.ipAddress as pojo2 [on line 1170, column 1 in GradeProtection.ftl]
----------

Java backtrace for programmers:
----------
freemarker.template.TemplateException: Expected collection or sequence. device.ipAddress evaluated instead to freemarker.template.SimpleScalar on line 1170, column 8 in GradeProtection.ftl.
at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:190)
at freemarker.core.Environment.visit(Environment.java:415)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:208)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:208)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:179)
at freemarker.core.Environment.visit(Environment.java:415)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:208)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:208)
at freemarker.core.Environment.process(Environment.java:188)
at freemarker.template.Template.process(Template.java:237)
at org.netcengfig.compliance.DocumentHandler.createDoc(DocumentHandler.java:61)
at org.netcengfig.compliance.Main.main(Main.java:10)




------解决方案--------------------
我好像经常这么写
没发现问题
你加一个为空判断呢?
<#if  (device.ipAddress)?exists>
<#list device.ipAddress as pojo2>
           ${pojo2.policyTag}
           ${pojo2.policyName}
</#list>
</#if>
------解决方案--------------------
引用:
我好像经常这么写
没发现问题
你加一个为空判断呢?
<#if  (device.ipAddress)?exists>
<#list device.ipAddress as pojo2>
           ${pojo2.policyTag}
           ${pojo2.policyName}
</#list>
</#if>

加了if以后还是报一样的错,是不是在给freemaker传参时要设置什么?
以下是在java中调用freemaker的代码:

		// 要填入模本的数据文件
Map<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap);
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// 这里我们的模板是放在com.havenliu.document.template包下面
configuration.setClassForTemplateLoading(this.getClass(),
"/com/havenliu/document/template");
Template t = null;
try
{
t = configuration.getTemplate("GradeProtection.ftl");
} catch (IOException e)
{
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File("D:/temp/outFile.doc");
Writer out = null;
try
{
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile)));
} catch (FileNotFoundException e1)
  相关解决方案