Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle and usage of Jfinal Framework

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the principle and usage of Jfinal framework". In daily operation, I believe that many people have doubts about the principle and usage of Jfinal framework. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the principle and usage of Jfinal framework". Next, please follow the editor to study!

I. start-up principle

Jfinal is a web framework that relies on web.xml to start, as follows:

Jfinal com.jfinal.core.JFinalFilter configClass com.xxx.run.ApiConfig jfinal / * / index.html

Description:

Jfinal starts the relevant configuration through the filter Filter, as shown above:

1. All requests will be intercepted by the filter com.jfinal.core.JFinalFilter

2. The init method of JFinalFilter will be started by the web container

3. The init method initializes the object according to the class configured in init-param. The following complete code gets the value corresponding to param-name as configClass, that is, the ApiConfig class, and then gets the relevant content of this class through reflection.

Core code block:

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 (); / / start accepting request}

The complete code of JFinalFilter is as follows:

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 (); / / start accepting requests} 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 / / stop accepting the request 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 instanceof class:" + configClass + ". Please check the config in web.xml ");}} static void initLog () {log = Log.getLog (JFinalFilter.class);}} II. Other frameworks integrate Jfinal framework

The Jfinal framework is essentially a web.xml + Lib package

Functionally, it is mainly divided into:

1. Lib or class that handles user web requests, such as ControllerRoutes,configInterceptor,configHandler, startup port, etc.

2. Common plug-ins, configurations, such as configPlugin,loadProp,configConstant, etc.

If the integration does not need to use the web-related features of Jfinal, then the choice is mainly based on the second point, for the biz of our project, then mainly to use Record, then integrate the ActiveRecordPlugin class.

At this point, the study of "the principle and usage of the Jfinal framework" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report