JFinal学习03——Controller简介
视频来源https://www.bilibili.com/video/BV1Bt411H7J9/?spm_id_from=333.337.search-card.all.click
1、Controller
-
MVC模式中的C
-
接收
和处理
浏览器发送的信息的核心的类
2、JFinal访问流程
web项目的入口:
web.xml中JFinalFilter类
点击进入JFinalFilter类
每次服务器启动,都会调用Filter的init()方法,web容器如Tomcat启动时会被调用一次
@SuppressWarnings("deprecation")
public void init(FilterConfig filterConfig) throws ServletException {
if (jfinalConfig == null) {
createJFinalConfig(filterConfig.getInitParameter("configClass"));
}
jfinal.init(jfinalConfig, filterConfig.getServletContext());
String contextPath = filterConfig.getServletContext().getContextPath();
contextPathLength = (contextPath == null || "/".equals(contextPath) ? 0 : contextPath.length());
constants = Config.getConstants();
encoding = constants.getEncoding();
jfinalConfig.onStart();
jfinalConfig.afterJFinalStart();
handler = jfinal.getHandler(); // 开始接受请求
}
每次发送请求,都会调用doFilter()方法:
只有符合web.xml中定义的filter的规则才能被调用:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
request.setCharacterEncoding(encoding);
String target = request.getRequestURI();
if (contextPathLength != 0) {
target = target.substring(contextPathLength);
}
boolean[] isHandled = {false};
try {
handler.handle(target, request, response, isHandled);
}
catch (Exception e) {
if (log.isErrorEnabled()) {
String qs = request.getQueryString();
log.error(qs == null ? target : target + "?" + qs, e);
}
}
if (isHandled[0] == false) {
// 默认拒绝直接访问 jsp 文件,加固 tomcat、jetty 安全性
if (constants.getDenyAccessJsp() && isJsp(target)) {
com.jfinal.kit.HandlerKit.renderError404(request, response, isHandled);
return ;
}
chain.doFilter(request, response);
}
}
可以看到目前任何请求都可以进来,进入到doFilter()
3、JFinal访问流程 源码解析
按住ctrl,点击handler方法,选择open implementation,选择ActionHandler类
ActionHandler.class中: