Jfinal学习笔记
实习期间,公司使用jfinal做开发,所以就学习了这个框架
Jfinal作为一个极速ORM,使用上非常方便,核心源代码1万多行,压缩后只有200多KB。
从入口开始
作为一个web项目,首先解读web.xml文件,其中配置了一个filter,
<filter-class>com.jfinal.core.JFinalFilter</filter-class>
finalfilter继承了filter。
关于filkter:filter是jsp中的一个接口,需要实现的方法有Init(),doFilter(),和destroy()三个必要的方法。
打开源代码中的JfinalFilter往下解读进入public void init(FilterConfig filterConfig) ,createJFinalConfig函数作用是初始化jfinalConfig,它使用了java反射方式查找,使用class.newInstance()方法实例化一个对象,再将temp对象强制转化成JFinalConfig对象
初始化完成jfinalConfig之后,使用了jfinal.init(jfinalConfig, filterConfig.getServletContext())这一方法。
再次进入JFinal类
public final class JFinal{
.............
boolean init(JFinalConfig jfinalConfig, ServletContext servletContext) {
this.servletContext = servletContext;
this.contextPath = servletContext.getContextPath();
initPathUtil();//获得webroot路径
Config.configJFinal(jfinalConfig); // start plugin and init logger factory in this method
constants = Config.getConstants();
initActionMapping();
initHandler();
initRender();
initActiveRecord();
initOreillyCos();
initI18n();
initTokenManager();
return true;
}
}
Config.configJFinal(jfinalConfig);方法为Config类中的几个静态变量赋值,并启动插件
initActionMapping();initHandler();initRender();initActiveRecord();initOreillyCos();initI18n();initTokenManager();依次做了初始化工作。
private void initActionMapping() {
actionMapping = new ActionMapping(Config.getRoutes(), Config.getInterceptors()); //传入了jfinalconfig中的routs和Interceptors,jfinalConfig中的Interceptors为全局拦截器
actionMapping.buildActionMapping();
}
initActionMapping是jfinal中核心设计,代码比较长
private final Map<String, Action> mapping = new HashMap<String, Action>();
void buildActionMapping() {
mapping.clear();
Set<String> excludedMethodName = buildExcludedMethodName(); //获得Controller自带的一些方法名称
InterceptorBuilder interceptorBuilder = new InterceptorBuilder();
Interceptor[] defaultInters = interceptors.getInterceptorArray();
interceptorBuilder.addToInterceptorsMap(defaultInters);
for (Entry<String, Class<? extends Controller>> entry : routes.getEntrySet()) {//循环遍历routs
Class<? extends Controller> controllerClass = entry.getValue();
Interceptor[] controllerInters = interceptorBuilder.buildControllerInterceptors(controllerClass); //在buildControllerInterceptors方法中,如果有Before声明
Method[] methods = controllerClass.getMethods(); //获取controllerClass所有方法
for (Method method : methods) {
String methodName = method.getName();
if (!excludedMethodName.contains(methodName) && method.getParameterTypes().length == 0) { //无参数函数为action,且不是基类Controller.class包含的方法
Interceptor[] methodInters = interceptorBuilder.buildMethodInterceptors(method);//在此方法中 查找Before
Interceptor[] actionInters = interceptorBuilder.buildActionInterceptors(defaultInters, controllerInters, controllerClass, methodInters, method);//返回整个action的拦截器[Interceptor]
String controllerKey = entry.getKey(); //controllerKey即为path路径
ActionKey ak = method.getAnnotation(ActionKey.class);//获得注解actionkey
if (ak != null) {
String actionKey = ak.value().trim();
if ("".equals(actionKey))
throw new IllegalArgumentException(controllerClass.getName() + "." + methodName + "(): The argument of ActionKey can not be blank.");
if (!actionKey.startsWith(SLASH))
actionKey = SLASH + actionKey;
if (mapping.containsKey(actionKey)) {
warnning(actionKey, controllerClass, method);
continue;
}
Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey));
mapping.put(actionKey, action);
}
else if (methodName.equals("index")) {//index为默认的
String actionKey = controllerKey;
Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey));
action = mapping.put(actionKey, action);
if (action != null) {
warnning(action.getActionKey(), action.getControllerClass(), action.getMethod());
}
}
else {
String actionKey = controllerKey.equals(SLASH) ? SLASH + methodName : controllerKey + SLASH + methodName;
if (mapping.containsKey(actionKey)) {
warnning(actionKey, controllerClass, method);
continue;
}
Action action = new Action(controllerKey, actionKey, controllerClass, method, methodName, actionInters, routes.getViewPath(controllerKey));
mapping.put(actionKey, action);
}
}
}
}
// support url = controllerKey + urlParas with "/" of controllerKey
Action actoin = mapping.get("/");
if (actoin != null)
mapping.put("", actoin);
}
buildExcludedMethodName()首先将Controllerlei中的无参函数取出,放入set集合中
private Set<String> buildExcludedMethodName() {
Set<String> excludedMethodName = new HashSet<String>();
Method[] methods = Controller.class.getMethods();
for (Method m : methods) {
if (m.getParameterTypes().length == 0)
excludedMethodName.add(m.getName());
}
return excludedMethodName;
}
Interceptor[] defaultInters = interceptors.getInterceptorArray();获得全局拦截器,转换成数组后的GlobalInterceptor
然后for循环遍历routs
循环中
Interceptor[] controllerInters = interceptorBuilder.buildControllerInterceptors(controllerClass); 返回controller中的Before声明
Interceptor[] buildControllerInterceptors(Class<? extends Controller> controllerClass) {
Before before = controllerClass.getAnnotation(Before.class);
return before != null ? createInterceptors(before) : NULL_INTERCEPTOR_ARRAY;
}
在循环routs中嵌套循环遍历每一个函数方法
这里就知道了为什么Controller中的无参函数才是一个action
if (!excludedMethodName.contains(methodName) && method.getParameterTypes().length == 0) {
//controller中的无参,并且不是基类Controller的方法
..............................
}
返回的每一个controller中无参的方法即为一个action
Interceptor[] methodInters = interceptorBuilder.buildMethodInterceptors(method);
Interceptor[] actionInters = interceptorBuilder.buildActionInterceptors(defaultInters, controllerInters, controllerClass, methodInters, method);
构建action的所有拦截器,interceptorBuilder.buildActionInterceptors(defaultInters, controllerInters, controllerClass, methodInters, method);中勿个参数就分别对应了全局拦截器,controller拦截器,controller类,action声明的before拦截器和方法体。
action的生成遵循几个规则:
1、如果声明了actionkey注解,就根据actionkey中的声明。
2、若方法名是index(),则action就于controllerkey。
3、否则actionkey等于 controllerkey+“/”+methodName
Jfinal学习笔记的更多相关文章
- JFinal 学习笔记之Handler包分析
HandlerFactory.java HandlerFactory是不可实例化的,因为 它的构造 函数 特意定位 私有 的:它有一个 静态的方法叫做 getHandler,它有两个参数 ,一个是Ha ...
- jfinal框架教程-学习笔记
jfinal框架教程-学习笔记 JFinal 是基于 Java 语言的极速 WEB + ORM 开发框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restfu ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
- JAVA GUI编程学习笔记目录
2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...
- seaJs学习笔记2 – seaJs组建库的使用
原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
随机推荐
- [Hive - LanguageManual] Hive Concurrency Model (待)
Hive Concurrency Model Hive Concurrency Model Use Cases Turn Off Concurrency Debugging Configuration ...
- 第三百零一天 how can i 坚持
其实真搞不大懂自己.不喜欢巴结人,也不喜欢阿谀奉承,但老是感觉自己却又在那么做,是对自己要求太高了,想太多?? 郭娜好像把我拉黑了,拉黑就拉黑吧.或许真不合适. 睡觉.
- Hibernate之QBC查询与本地SQL查询
1. QBC查询: QBC 查询就是通过使用Hibernate提供的QueryByCriteria API 来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口 ...
- keil中编译时出现*** ERROR L107: ADDRESS SPACE OVERFLOW
解决方法: http://zhidao.baidu.com/link?url=DWTVVdALVqPtUt0sWPURD6c1eEppyu9CXocLTeRZlZlhwHOA1P1xdesqmUQNw ...
- [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画
A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮 code s ...
- this指针和m_hWnd的区别
m_hWnd ① m_hWnd这个成员变量,最早是定义在类CWnd中,而且是类CWnd的第一个数据成员, 先看一下MSDN的解析: The handle of the Windows window a ...
- 第十五章 String讲解
package ch15; import java.util.Scanner; public class Test { public static void main(String[] args) { ...
- 我所理解的设计模式(C++实现)——备忘录模式(Memento Pattern)
概述: 我们玩单机游戏的时候总会遇到老婆大人的各位事情,一会去买瓶醋了,一会去打个酱油了,会耽误我们玩游戏的进程,但是此时我们能有“保存游戏”这个宝贝,我们的主基地不会在我们打酱油的时候被对手拆掉. ...
- 一种基于Welch's t检验的二元关系挖掘
现实中常常需要挖掘两种因素之间的关联,Welch's t检验很适合其中的nomial-numerical的关系挖掘.比如天气状况对销量的影响,或者天气情况对交通流量的影响等等.我们可以按照下雨/不下雨 ...
- ios开发——仿新版iBooks书本打开与关闭动画
IOS新版iBooks吸引人的地方除了有干净整洁的界面.方便灵活的操作以及大容量的书籍容量以外.还有其优秀的用户交互,尤其是其动画的使用.打开一本书时书本缓慢放大并打开.关闭一本书后书本关闭并回到原位 ...