转摘:http://www.javaweb1024.com/java/JavaWebzhongji/2015/04/08/528.html
freemarker 直接使用List来遍历set集合,可能会报错! 错误如下
Java代码
private Set<Article> articles = new HashSet<Article>();
Freemarker中遍历
[#list category.articles?chunk(5) as article1] <div> <ul> [#list article1 as article ] [#assign articleDesc=i18n_description(article.articleDescriptions,true)/] [#if articleDesc!=null] ${abbreviate(articleDesc.title, 80, "...")} [/#if] [/#list] </ul> </div> [/#list]
报错信息
Underlying collection is not a list, it's org.hibernate.collection.internal.PersistentSet
freemarker.template.TemplateModelException: Underlying collection is not a list, it's org.hibernate.collection.internal.PersistentSet
at freemarker.ext.beans.CollectionModel.get(CollectionModel.java:127)
at freemarker.core.SequenceBuiltins$chunkBI$ChunkedSequence$1.get(SequenceBuiltins.java:792)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:176)
at freemarker.core.Environment.visit(Environment.java:428)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:179)
at freemarker.core.Environment.visit(Environment.java:428)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
at freemarker.core.Environment.visit(Environment.java:428)
at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
at freemarker.core.Environment.visit(Environment.java:221)
at freemarker.core.Environment$1.render(Environment.java:246)
at com.eecart.template.directive.BaseDirective.setLocalVariable(BaseDirective.java:184)
at com.eecart.template.directive.ArticleCategoryRootListDirective.execute(ArticleCategoryRootListDirective.java:51)
at freemarker.core.Environment.visit(Environment.java:274)
at freemarker.core.UnifiedCall.accept(UnifiedCall.java:126)
at freemarker.core.Environment.visit(Environment.java:221)
解决方案:
不直接使用 articles
@Transient
public List<Article> getMyArticles(){
List<Article> list = new ArrayList<Article>(articles);
return list;
}