JFinal配置说明

JFinal

一.configConstant配置

public void configConstant(Constants me) {
}

1. 读取文件配置 使用loadPropertyFile或者PropKit

PropKit.use("config.properties");使用 PropKit.get("jdbcUrl") 获取
loadPropertyFile("config.properties");使用 getProperty("jdbcUrl") 获取

2. 设置开发模式

默认flase
me.setDevMode(PropKit.getBoolean("devMode"));
设置Action Report什么出现 默认true
me.setReportAfterInvocation(false);

3. 设置 ViewType

默认是Freemarker
me.setViewType(ViewType.JSP); 

4.设置视图路径 –取消

默认WebRoot
me.setBaseViewPath("/WEB-INF/view");   --BaseViewPath 

5.设置上传路径

cos组件有效 jfinal默认有值 相对 绝对都可以 "/"代表绝对路径
默认是 upload
me.setBaseUploadPath("amoqiload");  UploadFile file = getFile("img");
me.setMaxPostSize(1024*1024*20); //设置最大上传大小

6. 设置默认下载路径

cos组件有效 jfinal默认有值  相对 绝对都可以  "/"代表绝对路径
默认是 download
me.setBaseDownloadPath("amoqidownload");

7. 设置默认的Freemarker模板文件后缀名 jfinal默认.html

me.setFreeMarkerViewExtension(".ftl");
me.setJspViewExtension(".jtl");
me.setVelocityViewExtension(".vtl");

8.设置参数分隔符

默认是 -
me.setUrlParaSeparator("~");

9.设置国际化

me.setI18nDefaultBaseName("i18n");
me.setI18nDefaultLocale("zh_CN"");

10.设置Error View

me.setError404View("404.html");
me.setErrorRenderFactory(errorRenderFactory);

11.设置编码

me.setEncoding("GBK");

12.设置渲染工厂

设置默认的xml渲染工厂 默认使用Freemarker render渲染
me.setXmlRenderFactory(自定义工厂);

13设置默认json中时间格式化

me.setJsonDatePattern("yyyy-mm-dd HH:mm");
me.setJsonFactory(FastJsonFactory.me());
renderJson 和JsonKit底层依赖于JsonManager中设置的JsonFactory

14.设置自己Log的工厂实现

me.setLogFactory(Slf4JLogFactory.me());

二.configRoute配置

public void configRoute(Routes me) {

}

1.基本配置说明

me.add(controllerKey, controllerClass, viewPath);
controllerKey:访问controller需要的路径(key)
controllerClass:需要访问的controller类
viewPath:视图路径  
最后一个viewPath可以省略,省略后默认为第一个
me.add("/", IndexController.class);
me.add("/user", UserController.class,"/user");
finalView(视图位置) = baseViewPath(configConstant配置里) + viewPath(this) + view (render里)

2.扩展配置说明

当需要添加的路由过多,或者项目由大型团队开发可以拆分路由配置

可以新建一个类(此处为 FrontRoutes) 需要继承 Routes 添加路由
 public class FrontRoutes extends Routes {
    @Override
    public void config() {
        add("/",IndexController.class);
        add("/user",UserController.class);
    }
}
me.add(new FrontRoutes());  //configRoute配置 即可

三.configPlugin配置

1.基本说明

    Plugin都有自己的生命周期,

    public void configPlugin(Plugins me) {
    //Jfinal配置插件
    //输入库连接池
    //C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));
    C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
    //ORM Activerecord
    ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
    arp.setShowSql(true);
    arp.addMapping("user", User.class);
    me.add(c3p0Plugin);
    me.add(arp);
}

**重启程序或者热加载会重新启用插件**

2.扩展配置说明

如果需要自定义插件 需要实现IPlugin接口,并实现接口中的两个方法start(); stop();
MyPlugins myPlugins = new MyPlugins();
me.add(myPlugins);

四.configInterceptor(拦截器)配置

1.拦截位置和拦截级别

  1. 拦截位置

    • Controller
    • Service
    • 甚至是Jar包中需要拦截处理的对象
  2. 级别

    • 全局拦截器Global
    • Class
    • Method
    • Inject拦截器

2.自定义拦截器配置 (是否为手机登录的验证)

me.addGlobalActionInterceptor(new PhoneInterceptor());    //MainConfig   MainConfig配置

public class PhoneInterceptor implements Interceptor {     //PhoneInterceptor  自定义拦截器
@Override
public void intercept(Invocation inv) {
        Controller c = inv.getController();
        boolean isPhone = isPhone(c);
        c.getSession().setAttribute("isPhone", isPhone);
        inv.invoke();
}
public boolean isPhone(Controller c){
    HttpServletRequest request = c.getRequest();
    System.out.println(request.getHeader("user-agent"));
    String userAgent = request.getHeader("user-agent");
    if(userAgent!=null){
        userAgent = userAgent.toLowerCase();
        return userAgent.indexOf("iphone")!=-1 || userAgent.indexOf("android")!=-1;
    }
    return false;
    }
}

五.处理器配置configHandler

1.处理器说明

  • JFinal的入口是JFinalFilter,下一步接管请求的就是Handler
  • 可以接管所有的web请求
  • 完全掌控整个应用
  • 实现更高层次的功能扩展

2.可实现的功能

  1. 自定义路由规则
  2. URL伪静态处理
  3. 资源控制访问
  4. 操作日志入库
  5. 更高级的权限管理
  6. 等等

3.自定义实现

me.add(new MyHandlers());    //MainConfig   

public class MyHandlers extends Handler {   //MyHandlers
@Override
public void handle(String target, HttpServletRequest request,
        HttpServletResponse response, boolean[] isHandled) {
    System.out.println("===log:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":"+target);
    next.handle(target, request, response, isHandled);
    }
}

六 afterJFinalStart和beforeJFinalStop配置

1.afterJFinalStart基本说明

  1. 调用时间
    • 在JFinal启动(init)之后回调
  2. 可以做什么
    • 系统启动后创建时间调度线程处理调度任务
    • 开启数据同步线程 同步一些数据
    • 缓存数据初始化
    • Application级别的全局变量设置初始化
    • 开启一个webSocket服务

2.beforeJFinalStop基本说明

  1. 调用时间
    • 在JFinal系统关闭之前回调
  2. 可以做什么
    • 关闭一些自定义线程
    • 清空一些数据 关闭一些服务

3.自定义实现

    @Override                   //MainConfig 中实现即可
public void beforeJFinalStop() {
    System.out.println("beforeJFinalStop:JFinal关闭前需要处理的东西");
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是刘奇奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值