freemarker map问题求解100分

xf_taoran 2011-05-13 03:25:04
程序中定义好的:HashMap<Long,List<News>> newsMap = new HashMap<Long,List<News>>();

ftl页面显示
<#list newsMap.keySet() as ntId>
<#list newsMap.get(ntId) as news>
${news.title?if_exists}
</#if>
</#list>
...全文
532 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sunny_kaka 2011-10-09
  • 打赏
  • 举报
回复
你的写法错了。
下面摘自官方文档。


<#assign h = {"name":"mouse", "price":50}>
<#assign keys = h?keys>
<#list keys as key>${key} = ${h[key]}; </#list>
liuyuhua0066 2011-05-13
  • 打赏
  • 举报
回复
因为你的key是Long型而不是String类,需要先转换。

<#list newsMap.get(ntId?long) as news>
liuyuhua0066 2011-05-13
  • 打赏
  • 举报
回复
不好意思,写错了……囧
[Quote=引用 18 楼 liuyuhua0066 的回复:]
Map的keySet()还是一个Map,你想无限迭代下去?
[/Quote]
liuyuhua0066 2011-05-13
  • 打赏
  • 举报
回复
Map的keySet()还是一个Map,你想无限迭代下去?
Luciferoffans 2011-05-13
  • 打赏
  • 举报
回复
freemarker还真没用过,^_^
xf_taoran 2011-05-13
  • 打赏
  • 举报
回复
郁闷哦
啊的发哦 2011-05-13
  • 打赏
  • 举报
回复

#list newsMap?keys as ntId
luckyzhangye 2011-05-13
  • 打赏
  • 举报
回复

${news.title?if_exists}
换成
<#if news.title?exists>
试下
xf_taoran 2011-05-13
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 pingjiuzhou 的回复:]
${news.title?if_exists}
?if_exists这东西有嘛?先去掉试试,简单点
[/Quote]
在newsMap.keySet() 这一步的时候就出问题了 还没到那一步呢
pingjiuzhou 2011-05-13
  • 打赏
  • 举报
回复
${news.title?if_exists}
?if_exists这东西有嘛?先去掉试试,简单点
xf_taoran 2011-05-13
  • 打赏
  • 举报
回复
我实验过 如果是直接从action返回页面 map用的时候就没问题

但是如果像上面 生成静态页面的时候 放在map中传过去 那么我那个map就不顶用了
xf_taoran 2011-05-13
  • 打赏
  • 举报
回复
<#list ${newsMap}.keySet() as ntId> 哥哥呀 这种写法如果没问题的话 我名字倒过来写

还有你下面说的那些方法都不行的 所有方法我都尝试过了 貌似都不行

说下 我的操作时用ftl生成html

Map rootMap = new HashMap();
rootMap.put("newsMap", newsMap);
TemplateDefinition template = new TemplateDefinition();
template.setGenerateFileName("content.html");
template.setTemplateName("content.ftl");
template.setGenerateFilePath(TemplateDefinition.ROOT_PATH + "index/");
template.setTemplatePath(TemplateDefinition.ROOT_PATH + "WEB-INF/pages/site/template");
new AutoGenerateUtil().generateTemplateJrxml(rootMap, template);
xf_taoran 2011-05-13
  • 打赏
  • 举报
回复
红色部分去掉 语法都有问题了
wen691 2011-05-13
  • 打赏
  • 举报
回复
不行,再试试这个
Action

// Map<String,List<String>> kindsMap = new HashMap<String,List<String>>();
// List<String> aList = new ArrayList<String>();
// aList.add("a1");
// aList.add("a2");
// List<String> bList = new ArrayList<String>();
// bList.add("b1");
// bList.add("b2");
//
//
// kindsMap.put("a",aList);
// kindsMap.pu t("b", bList);

ftl

<#list kindsMap?keys as mKey>

${mKey}
<#assign item = kindsMap[mKey]>
<#list item as itemValue>

${itemValue}
</#list>
</#list>

如果list里还有实体 可直接 ${itemValue.属性}
wen691 2011-05-13
  • 打赏
  • 举报
回复
你这种写法肯定是错的,呵呵
wen691 2011-05-13
  • 打赏
  • 举报
回复
newsMap的使用是不是应该用${newsMap}

<#list ${newsMap}.keySet() as ntId>
<#list ${newsMap}.get(ntId) as news>
${news.title?if_exists}
</#if>
</#list> 试下 ,

我之前是用json处理这种的

Map<String ,Map<String ,String>> namemap =
new HashMap<String ,Map<String ,String>>();
String map = JSONObject.fromObject(namemap).toString()

ftl下

Ext.titansoft.bpm.resources = {


namedate : ${map},

findByid:function(id){

return this.namedate[id];
}


}









luckyzhangye 2011-05-13
  • 打赏
  • 举报
回复
程序中定义好的:HashMap<Long,List<News>> newsMap = new HashMap<Long,List<News>>();
红色部分多了,删去试试~~~
YangWenChaoX 2011-05-13
  • 打赏
  • 举报
回复
首先,ftl 是神马 ?
其次,没见过那些语法
xf_taoran 2011-05-13
  • 打赏
  • 举报
回复
没有人懂吗
xf_taoran 2011-05-13
  • 打赏
  • 举报
回复
Expression newsMap.keySet is undefined on line 63, column 32 in content.ftl.
The problematic instruction:
----------
==> list newsMap.keySet() as ntId [on line 63, column 25 in content.ftl]
----------

Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression newsMap.keySet is undefined on line 63, column 32 in content.ftl.

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧