您好,登录后才能下订单哦!
这篇文章主要介绍“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。