这篇文章主要介绍“Jfinal框架的原理和用法”,在日常操作中,相信很多人在Jfinal框架的原理和用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Jfinal框架的原理和用法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联建站,为您提供成都网站建设公司、成都网站制作、网站营销推广、网站开发设计,对服务成都生料搅拌车等多个行业拥有丰富的网站建设及推广经验。创新互联建站网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!
Jfinal是个web框架,依赖于web.xml启动,如下:
jfinal com.jfinal.core.JFinalFilter configClass com.xxx.run.ApiConfig jfinal /* /index.html
说明:
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框架的原理和用法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!