您好,登錄后才能下訂單哦!
這篇文章主要介紹“Jfinal框架的原理和用法”,在日常操作中,相信很多人在Jfinal框架的原理和用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Jfinal框架的原理和用法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
Jfinal是個web框架,依賴于web.xml啟動,如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="mc" version="3.0"> <filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <init-param> <param-name>configClass</param-name> <param-value>com.xxx.run.ApiConfig</param-value> </init-param> </filter> <filter-mapping> <filter-name>jfinal</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list> </web-app>
說明:
Jfinal通過過濾器Filter啟動相關配置,如上:
1、所有的請求都會被過濾器com.jfinal.core.JFinalFilter攔截;
2、JFinalFilter的init方法會由web容器啟動;
3、init方法會根據init-param中配置的類進行對象初始化,如下完整代碼會獲取param-name為configClass對應的值,即ApiConfig類,然后通過反射得到這個類的相關內容;
核心代碼塊:
public void init(FilterConfig filterConfig) throws ServletException { 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.afterJFinalStart(); handler = jfinal.getHandler(); // 開始接受請求 }
JFinalFilter 完整代碼如下:
package com.jfinal.core; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jfinal.config.Constants; import com.jfinal.config.JFinalConfig; import com.jfinal.handler.Handler; import com.jfinal.log.Log; /** * JFinal framework filter */ public class JFinalFilter implements Filter { private Handler handler; private String encoding; private JFinalConfig jfinalConfig; private Constants constants; private static final JFinal jfinal = JFinal.me(); private static Log log; private int contextPathLength; public void init(FilterConfig filterConfig) throws ServletException { 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.afterJFinalStart(); handler = jfinal.getHandler(); // 開始接受請求 } 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) { chain.doFilter(request, response); } } public void destroy() { handler = null; // 停止接受請求 jfinalConfig.beforeJFinalStop(); jfinal.stopPlugins(); } protected void createJFinalConfig(String configClass) { if (configClass == null) { throw new RuntimeException("Please set configClass parameter of JFinalFilter in web.xml"); } Object temp = null; try { temp = Class.forName(configClass).newInstance(); } catch (Exception e) { throw new RuntimeException("Can not create instance of class: " + configClass, e); } if (temp instanceof JFinalConfig) { jfinalConfig = (JFinalConfig)temp; } else { throw new RuntimeException("Can not create instance of class: " + configClass + ". Please check the config in web.xml"); } } static void initLog() { log = Log.getLog(JFinalFilter.class); } }
Jfinal框架本質上就是web.xml + Lib包;
從功能上看主要分為:
1、處理用戶web請求的lib或類,如ControllerRoutes,configInterceptor,configHandler,啟動端口等;
2、普通的插件,配置等,如configPlugin,loadProp,configConstant等
如果集成不需要使用Jfinal的web相關功能,則主要根據第二點選擇即可,對于我們項目的biz,則主要為了使用Record,那么集成ActiveRecordPlugin類即可。
到此,關于“Jfinal框架的原理和用法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。