InterceptorController.java
package com.zhiguo.controller;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.aop.Duang;
import com.jfinal.core.Controller;
import com.zhiguo.injector.DemoInjector;
import com.zhiguo.interceptor.ActionInterceptor;
import com.zhiguo.interceptor.ControllerInterceptor;
//GlobalInterceptor>RouterInterceptor>ControllerInterceptor>ActionInterceptor>InjectorInterceptor
@Before(ControllerInterceptor.class)
public class InterceptorController extends Controller {
public void index() {
renderText("InterceptorController index() ...");
}
@Before(ActionInterceptor.class)
public void actionMethod() {
renderText("InterceptorController actionMethod() ...");
}
@Before(ActionInterceptor.class)
@Clear
public void injectorMethod() {
DemoInjector demoInjector = Duang.duang(DemoInjector.class);
demoInjector.injectorMethod();
renderText("InterceptorController injectorMethod() ...");
}
@Before(ActionInterceptor.class)
@Clear
public void clearMethod() {
renderText("InterceptorController clearMethod() ...");
}
}
FilterConfig.java
package com.zhiguo.filter.config;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.zhiguo.controller.EnjoyController;
import com.zhiguo.controller.IndexController;
import com.zhiguo.controller.UserController;
import com.zhiguo.interceptor.GlobalInterceptor;
import com.zhiguo.model.User;
import com.zhiguo.route.InterceptorRoute;
import com.zhiguo.util.ConfigUtil;
//configConstant>>configRoute>>configPlugin>>configEngine>>configInterceptor>>configHandler>>afterJFinalStart
public class FilterConfig extends JFinalConfig {
static {
PropKit.use("config.txt");
}
// 配置常量
@Override
public void configConstant(Constants me) {
me.setDevMode(PropKit.getBoolean("devMode"));// 开发者模式
// me.setBaseUploadPath("");// 文件上传路径,对应getFile系列方法
// me.setBaseDownloadPath("");//文件下载根目录,对应renderFile系列方法
}
// 配置路由
@Override
public void configRoute(Routes me) {
me.add("/", IndexController.class);// 请求映射关系,"/"是webapp所在目录
me.add("/user", UserController.class);
me.add("/enjoy", EnjoyController.class, "/WEB-INF/enjoy");
me.add(new InterceptorRoute());
}
// 配置插件,例如数据库配置
@Override
public void configPlugin(Plugins me) {
DruidPlugin druidPlugin = new DruidPlugin(ConfigUtil.url, ConfigUtil.username, ConfigUtil.password);
me.add(druidPlugin);
ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
activeRecordPlugin.setBaseSqlTemplatePath(PathKit.getWebRootPath() + "/WEB-INF/sql/");
activeRecordPlugin.addSqlTemplate("t_user.sql");
// 数据库字段转换:true转成小写, false转成大写, 空参数区分大小写
activeRecordPlugin.setContainerFactory(new CaseInsensitiveContainerFactory(true));
activeRecordPlugin.addMapping("t_user", User.class);
me.add(activeRecordPlugin);
}
// 配置引擎
@Override
public void configEngine(Engine me) {
// devMode 配置为 true,将支持模板实时热加载
me.setDevMode(PropKit.getBoolean("devMode"));
// 添加共享函数,等效于在每个页面中添加#include("layout.html")
me.addSharedFunction("/WEB-INF/enjoy/_layout.html");
// 分享静态方法
// me.addSharedStaticMethod(Share.class);
// 分享所有方法
// me.addSharedMethod(Share.class);
// me.addSharedMethod(new Share());
// 分享所有变量和方法
me.addSharedObject("share", new Share());
}
// 配置拦截,全局拦截器
@Override
public void configInterceptor(Interceptors me) {
me.add(new GlobalInterceptor());
}
// 配置处理
@Override
public void configHandler(Handlers me) {
me.add(new ContextPathHandler("ctx"));
}
@Override
public void afterJFinalStart() {
System.out.println("==>> afterJFinalStart");
}
@Override
public void beforeJFinalStop() {
System.out.println("==>> beforeJFinalStop");
}
// 启动后访问地址: http://localhost/maven-jfinal/index.html
public static void main(String[] args) {
JFinal.start("src/main/webapp", 80, "/maven-jfinal", 5);
}
}
DemoInjector.java
package com.zhiguo.injector;
import com.jfinal.aop.Before;
import com.zhiguo.interceptor.InjectorInterceptor;
// @Clear对injector无效
public class DemoInjector {
@Before(InjectorInterceptor.class)
public void injectorMethod() {
System.out.println("DemoInjector injectorMethod() ...");
}
}
ActionInterceptor.java
package com.zhiguo.interceptor;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
public class ActionInterceptor implements Interceptor {
@Override
public void intercept(Invocation invocation) {
System.out.println("ActionInterceptor ...");
invocation.invoke();
}
}
ControllerInterceptor.java
package com.zhiguo.interceptor;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
public class ControllerInterceptor implements Interceptor {
@Override
public void intercept(Invocation invocation) {
System.out.println("ControllerInterceptor ...");
invocation.invoke();
}
}
GlobalInterceptor.java
package com.zhiguo.interceptor;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
public class GlobalInterceptor implements Interceptor {
@Override
public void intercept(Invocation invocation) {
System.out.println("GlobalInterceptor ...");
invocation.invoke();
}
}
InjectorInterceptor.java
package com.zhiguo.interceptor;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
public class InjectorInterceptor implements Interceptor {
@Override
public void intercept(Invocation invocation) {
System.out.println("InjectorInterceptor ...");
invocation.invoke();
}
}
RouteInterceptor.java
package com.zhiguo.interceptor;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
public class RouteInterceptor implements Interceptor {
@Override
public void intercept(Invocation invocation) {
System.out.println("RouterInterceptor ...");
invocation.invoke();
}
}
InterceptorRoute.java
package com.zhiguo.route;
import com.jfinal.config.Routes;
import com.zhiguo.controller.InterceptorController;
import com.zhiguo.interceptor.RouteInterceptor;
public class InterceptorRoute extends Routes {
@Override
public void config() {
addInterceptor(new RouteInterceptor());
add("/interceptor", InterceptorController.class);
}
}