首先Controller的继承类BaseController重写render方法。
package com.myc.controller; import com.jfinal.core.Controller; public abstract class BaseController extends Controller { protected static String root; public String getroot() { return root; } @Override public void render(String view) { if (root == null) root = this.getRequest().getContextPath(); this.setAttr("base", root); super.render(view); } }
然后所有controller都继承BaseController
package com.myc.controller.jfinal; import com.myc.controller.BaseController; import com.jfinal.ext.route.ControllerBind; @ControllerBind(controllerKey = "/test") public class TestController extends BaseController { public void index() { render("test/index"); } }
没有加入拦截器,调试成功。
然后我加入了一个拦截器Flash。http://my.oschina.net/myaniu/blog/136340
package com.myc.controller.jfinal; import com.jfinal.aop.Before; import com.jfinal.ext.route.ControllerBind; import com.jfinal.flash.Flash; @ControllerBind(controllerKey = "/test") @Before(Flash.class) public class TestController extends BaseController { public void index() { render("test/index"); } }
2015-08-18 17:11:34 [ERROR]-[Thread: http-8080-1]-[freemarker.log.Log4JLoggerFactory$Log4JLogger.error()]: Template processing error: "Expression base is undefined on line 8, column 3 in WEB-INF/template/test/index.html." Expression base is undefined on line 8, column 3 in WEB-INF/template/test/index.html. The problematic instruction: ---------- ==> ${base} [on line 8, column 1 in WEB-INF/template/test/index.html] ---------- Java backtrace for programmers: ---------- freemarker.core.InvalidReferenceException: Expression base is undefined on line 8, column 3 in WEB-INF/template/test/index.html. at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125) at freemarker.core.Expression.getStringValue(Expression.java:118) at freemarker.core.Expression.getStringValue(Expression.java:93) at freemarker.core.DollarVariable.accept(DollarVariable.java:76) 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.Environment.process(Environment.java:199) at freemarker.template.Template.process(Template.java:259) at com.jfinal.render.FreeMarkerRender.render(FreeMarkerRender.java:128) at com.jfinal.core.ActionHandler.handle(ActionHandler.java:93) at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> ${base} <br/> Hello! JFinal </body> </html>
单步调试一下程序,看是否执行到了BaseController.render()方法内部,可能是import包弄错成别的jar包中的BaseController了,还有一种可能是因为在某处"base"这个变量被清掉了,如 setAttr("base",null)或removeAttr("base");单步调试发现是拦截器中的问题,已解决。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。