JFinal配置说明
来源:互联网 发布:淘宝抠图 编辑:程序博客网 时间:2024/05/17 16:54
JFinal
一.configConstant配置
public void configConstant(Constants me) {}
1. 读取文件配置 使用loadPropertyFile或者PropKit
PropKit.use("config.properties");使用 PropKit.get("jdbcUrl") 获取loadPropertyFile("config.properties");使用 getProperty("jdbcUrl") 获取
2. 设置开发模式
默认flaseme.setDevMode(PropKit.getBoolean("devMode"));设置Action Report什么出现 默认trueme.setReportAfterInvocation(false);
3. 设置 ViewType
默认是Freemarkerme.setViewType(ViewType.JSP);
4.设置视图路径 –取消
默认WebRootme.setBaseViewPath("/WEB-INF/view"); --BaseViewPath
5.设置上传路径
cos组件有效 jfinal默认有值 相对 绝对都可以 "/"代表绝对路径默认是 uploadme.setBaseUploadPath("amoqiload"); UploadFile file = getFile("img");me.setMaxPostSize(1024*1024*20); //设置最大上传大小
6. 设置默认下载路径
cos组件有效 jfinal默认有值 相对 绝对都可以 "/"代表绝对路径默认是 downloadme.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.拦截位置和拦截级别
拦截位置
- Controller
- Service
- 甚至是Jar包中需要拦截处理的对象
级别
- 全局拦截器Global
- Class
- Method
- Inject拦截器
2.自定义拦截器配置 (是否为手机登录的验证)
me.addGlobalActionInterceptor(new PhoneInterceptor()); //MainConfig MainConfig配置public class PhoneInterceptor implements Interceptor { //PhoneInterceptor 自定义拦截器@Overridepublic 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.可实现的功能
- 自定义路由规则
- URL伪静态处理
- 资源控制访问
- 操作日志入库
- 更高级的权限管理
- 等等
3.自定义实现
me.add(new MyHandlers()); //MainConfig public class MyHandlers extends Handler { //MyHandlers@Overridepublic 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基本说明
- 调用时间
- 在JFinal启动(init)之后回调
- 可以做什么
- 系统启动后创建时间调度线程处理调度任务
- 开启数据同步线程 同步一些数据
- 缓存数据初始化
- Application级别的全局变量设置初始化
- 开启一个webSocket服务
2.beforeJFinalStop基本说明
- 调用时间
- 在JFinal系统关闭之前回调
- 可以做什么
- 关闭一些自定义线程
- 清空一些数据 关闭一些服务
3.自定义实现
@Override //MainConfig 中实现即可public void beforeJFinalStop() { System.out.println("beforeJFinalStop:JFinal关闭前需要处理的东西");}
阅读全文