开发者社区> 问答> 正文

JFinal在controller中加入拦截器后Controller的继承类失效?报错

首先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)



index.html 如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${base}
<br/>
Hello! JFinal
</body>
</html>



展开
收起
爱吃鱼的程序员 2020-06-12 14:29:18 717 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

      单步调试一下程序,看是否执行到了BaseController.render()方法内部,可能是import包弄错成别的jar包中的BaseController了,还有一种可能是因为在某处"base"这个变量被清掉了,如 setAttr("base",null)或removeAttr("base");单步调试发现是拦截器中的问题,已解决。

    2020-06-12 14:29:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载