//以下是Controller代码 获取所有的菜单 然后根据上级id作为Map的主键 按照顺序 //存储一个主菜单和所有的子菜单 public void home() { //获取所有的菜单 List<Module> parentList = DaoFactory.moduleDao().findAllModule(); Map<String, List<Module>> moduleMap = new HashMap<String, List<Module>>(); List<Module> temp = null; //遍历所有的菜单,以主菜单id为主键 保存所有子菜单在一个list里 for (Module m : parentList) { temp = new ArrayList<Module>(); if (m.getPmid() == 0) { temp.add(m); moduleMap.put(m.getId().toString(), temp); } else { temp = moduleMap.get(m.getPmid().toString()); temp.add(m); moduleMap.put(m.getPmid().toString(), temp); } } setAttr("moduleMap", moduleMap); render("/admin/index.html"); }
//以下代码是前台遍历的代码 //遍历Map 获取所有菜单 #for(list:moduleMap) //遍历获取到的List #for(m:list) //获取第一个主菜单 #if(for.first) <li class="layui-nav-item layui-nav-itemed"><a class="" href="javascript:;">#(m.name)</a> --》这里报错 <dl class="layui-nav-child"> #else //获取所有子菜单 <dd> <a class="hp-tab-add" hp-href="#(ctx)#(m.url)" href="javascript:;">#(m.name)</a> </dd> #end //补全剩余tag #if(for.last) </dl></li> #end #end #end
Map 的值 :
{1=[{id:1, iconpath:/upload/module/20180111174509.jpg, pmid:0, name:系统管理, url:null}, {id:2, iconpath:/upload/module/20180111174530.jpg, pmid:1, name:菜单管理, url:/module/manage}, {id:3, iconpath:/upload/module/20180111174541.jpg, pmid:1, name:会员管理, url:/user/manage}, {id:4, iconpath:/upload/module/20180111175116.jpg, pmid:1, name:角色管理, url:/role/manage}]}
后台报错:(78行为第一个 #(m.name)的那行)
[23:28:59|ERROR|(com.jfinal.core.ActionHandler)]=[/admin/home] com.jfinal.render.RenderException: com.jfinal.template.TemplateException: In the class com.jfinal.template.stat.ast.ForEntry can not find getName() method, also can not find "name" field Template: "/admin/index.html". Line: 78 at com.jfinal.render.TemplateRender.render(TemplateRender.java:63) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:99) at com.jfinal.ext.handler.ContextPathHandler.handle(ContextPathHandler.java:48) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:74) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: com.jfinal.template.TemplateException: In the class com.jfinal.template.stat.ast.ForEntry can not find getName() method, also can not find "name" field Template: "/admin/index.html". Line: 78 at com.jfinal.template.expr.ast.Field.eval(Field.java:98) at com.jfinal.template.expr.ast.ExprList.eval(ExprList.java:59) at com.jfinal.template.stat.ast.Output.exec(Output.java:46) at com.jfinal.template.stat.ast.StatList.exec(StatList.java:47) at com.jfinal.template.stat.ast.If.exec(If.java:53) at com.jfinal.template.stat.ast.StatList.exec(StatList.java:47) at com.jfinal.template.stat.ast.For.forIterator(For.java:74) at com.jfinal.template.stat.ast.For.exec(For.java:53) at com.jfinal.template.stat.ast.StatList.exec(StatList.java:47) at com.jfinal.template.stat.ast.For.forIterator(For.java:74) at com.jfinal.template.stat.ast.For.exec(For.java:53) at com.jfinal.template.stat.ast.StatList.exec(StatList.java:47) at com.jfinal.template.Template.render(Template.java:49) at com.jfinal.render.TemplateRender.render(TemplateRender.java:61) ... 19 more