freemaker里嵌套list报错Expected collection

J2EE 码拜 9年前 (2015-09-28) 1260次浏览
在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)

方案推荐指数:10
你加一个为空判断呢?

<#if  (device.ipAddress)?exists>

<#list device.ipAddress as pojo2>

           ${pojo2.policyTag}

           ${pojo2.policyName}

</#list>

</#if>

方案推荐指数:40

加了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)
		{
			e1.printStackTrace();
		}
		try
		{
			t.process(dataMap, out);
		} catch (TemplateException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
方案推荐指数:50
问题已解决 ,解决办法:

在NDeviceLite 里加一个属性

private List<ComplianceStatisticsPojo> pojos;

获取时这样写:

<#list device.pojos as pojo2>

因为在<#list devicesList as device>这循环里写<#list device.ipAddress as pojo2>是指获取device这个对象中的ipAddress属性的值并遍历,而不是获取map中以device.ipAddress作为key的那个list对象,如果要遍历map中的那个以device.ipAddress作为key的list需要在<#list devicesList as device></#list>以外写list遍历,但这样不是我想要的结果所以在NDeviceLite 加了list属性实现双层遍历!


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明freemaker里嵌套list报错Expected collection
喜欢 (0)
[1034331897@qq.com]
分享 (0)