使用 jfinal-3.1_demo_for_maven基础代码测试。
package com.demo.index; import java.util.ArrayList; import java.util.List; import com.jfinal.core.Controller; import com.jfinal.kit.JsonKit; import com.jfinal.plugin.activerecord.Record; /** * 本 demo 仅表达最为粗浅的 jfinal 用法,更为有价值的实用的企业级用法 详见 JFinal 俱乐部: * http://jfinal.com/club * * IndexController */ public class IndexController extends Controller { public void index() { //构造数据 List<Record> databases = new ArrayList<Record>(); Record db=new Record().set("name", "第一层"); databases.add(db); List<Record> tables = new ArrayList<Record>(); Record table=new Record().set("name", "第二层"); tables.add(table); db.set("tables", tables); List<Record> columns = new ArrayList<Record>(); Record column=new Record().set("name", "第三层"); columns.add(column); table.set("columns", columns); System.out.println(JsonKit.toJson(databases)); setAttr("databases", databases); //End render("index.html"); } }
控制台打印数据: [{"tables":[{"columns":[{"name":"第三层"}],"name":"第二层"}],"name":"第一层"}]
格式化后: [ { "tables":[ { "columns":[ { "name":"第三层" } ], "name":"第二层" } ], "name":"第一层" } ]
index.html #@layout() #define main() <h1>JFinal Demo 项目首页</h1> <div class="table_box"> <p>欢迎来到 JFinal极速开发世界!</p> <br><br><br> 本Demo采用 JFinal Template 作为视图文件。 点击<a href="/blog"><b>此处</b></a>开始试用Demo。 <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> <!-- 测试部分 --> #for(d:databases) <h1>#(d) name:#(d.name)</h1> #for(t:d.tables) <h3>#(t) name:#(t.name)</h3> #for(c:t.columns) <p>#(c) name:#(c.name)</p> #end #end #end <!--/ 测试部分 --> </div> #end
页面输出:
控制台打印信息: [{"tables":[{"columns":[{"name":"第三层"}],"name":"第二层"}],"name":"第一层"}] JFinal action report -------- 2017-05-09 22:02:13 ------------------------------ Url : GET / Controller : com.demo.index.IndexController.(IndexController.java:1) Method : index -------------------------------------------------------------------------------- 2017-05-09 22:02:13 [ERROR]-[Thread: qtp1651191114-34]-[com.jfinal.core.ActionHandler.handle()]: / com.jfinal.render.RenderException: com.jfinal.template.TemplateException: Field not found: "c.name" and getter method not found: "c.getName()" Template: "/index/index.html". Line: 20 at com.jfinal.render.TemplateRender.render(TemplateRender.java:63) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:99) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:74) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) at org.eclipse.jetty.server.Server.handle(Server.java:365) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485) at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) at java.lang.Thread.run(Thread.java:745) Caused by: com.jfinal.template.TemplateException: Field not found: "c.name" and getter method not found: "c.getName()" Template: "/index/index.html". Line: 20 at com.jfinal.template.expr.ast.Field.eval(Field.java:111) at com.jfinal.template.expr.ast.ExprList.eval(ExprList.java:67) at com.jfinal.template.stat.ast.Output.exec(Output.java:46) at com.jfinal.template.stat.ast.StatList.exec(StatList.java:48) 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:48) 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:48) 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:48) at com.jfinal.template.stat.ast.Define.call(Define.java:105) at com.jfinal.template.stat.ast.Call.exec(Call.java:49) at com.jfinal.template.stat.ast.StatList.exec(StatList.java:48) at com.jfinal.template.stat.ast.Define.call(Define.java:105) at com.jfinal.template.stat.ast.Call.exec(Call.java:49) at com.jfinal.template.stat.ast.StatList.exec(StatList.java:48) at com.jfinal.template.Template.render(Template.java:49) at com.jfinal.render.TemplateRender.render(TemplateRender.java:61) ... 25 more
前两层能正确输出对象,到第三层c=com.jfinal.template.stat.ast.ForEntry