Five abstract methods of JFinal

JFinalConfig

 

      JFinal-based web projects need to create a subclass that inherits from the JFinalConfig class, which is used to  configure the entire web project. A JFinalConfig subclass needs to implement five abstract methods, as follows:                                                                                                                                                                                                                                     

       1.configConstant(Constants me), this method is used to configure JFinal constant values, such as the configuration of the development mode constant devMode, the configuration of the default view type ViewType, the following code configures JFinal to run in development mode and the default view type is JSP:

      

    

      In development mode, JFinal will output a report for each request, such as outputting the Controller, Method and parameters carried by the request. JFinal supports three common views: JSP, FreeMarker, and Velocity.

 

       2.configRoute(Routes me), this method is used to configure JFinal access route, the following code configures the controller that maps "/hello" to HelloController, through the following configuration, http://localhost/hello will access HelloController.index () method, and http://localhost/hello/methodName will access the HelloController.methodName() method.

    

      The Routes class mainly has the following two methods:

        The first parameter controllerKey refers to a string required to access a Controller, the string only corresponds to a Controller, and the controllerKey can only locate the Controller. The second parameter controllerClass is the Controller corresponding to the controllerKey. The third parameter viewPath refers to the relative path of the view returned by the Controller (the details of this parameter will be given in the relevant chapters of Controller). Defaults to controllerKey when viewPath is not specified.

      The JFinal routing rules are as follows: 

      As can be seen from the table, JFinal needs to use controllerKey and method to precisely locate an exact Action (see Section 3.2 for the definition of Action), and the default value is index when method is omitted. urlPara is to carry parameter values ​​in the url. urlPara can carry multiple values ​​in one request at the same time. JFinal uses the minus sign "-" by default to separate multiple values ​​(the separator can be set by constants.setUrlParaSeparator(String)), In the Controller, these values ​​can be retrieved separately through getPara(int index). The three parts of controllerKey, method, and urlPara must be separated by forward slashes "/". Note that the controllerKey itself can also contain forward slashes "/", such as "/admin/article", which essentially implements the namespace function of struts2.

    

      JFinal also provides ActionKey annotations in addition to the above routing rules, which can break the original rules. The following is a code example:

       Assuming that the controllerKey value of UserController is "/user", after using the @ActionKey("/login") annotation, the actionKey changes from "/user/login" to "/login". This annotation also allows characters such as a minus sign or a number to be used in the actionKey, such as "/user/123-456".

       If the default routing rules of JFinal cannot meet the requirements, developers can also use Handler to customize more personalized routing according to their needs. The general idea is to change the value of the first parameter String target in Handler.

      JFinal routing can also be split configuration, which is especially useful for large-scale team development, the following is a code example: 

       As shown in the above three pieces of code, the FrontRoutes class configures the system front-end routing, AdminRoutes configures the system back-end routing, and the MyJFinalConfig.configRoute(…) method combines the two split routes. Using this split configuration can not only make the MyJFinalConfig file more concise, but also facilitate large-scale team development and avoid version conflicts when multiple people modify MyJFinalConfig at the same time.

       

      3. configPlugin (Plugins me) 

      This method is used to configure the Plugin of JFinal. The following code configures the C3p0 database connection pool plug-in and the ActiveRecord database access plug-in. Through the following configuration, ActiveRecord can be used in the application to operate the database very conveniently.

       The JFinal plug-in architecture is one of its main extension methods, which can easily create plug-ins and apply them to projects. 

 

       4.configInterceptor (Interceptors me) 

       This method is used to configure JFinal's global interceptor. The global interceptor will intercept all action requests unless cleared in the Controller using @Clear. The following code configures an interceptor named AuthInterceptor.

       JFinal's Interceptor is very similar to Struts2, but it is more convenient to use. The Interceptor configuration granularity is divided into three levels: Global, Class, and Method. The above code configuration granularity is global. Class and Method-level Interceptor configuration will be described in detail in subsequent chapters.

       5.configHandler (Handlers me)     

       This method is used to configure JFinal's Handler. The following code configures a processor named ResourceHandler. Handler can take over all web requests and has complete control over the application, which can easily implement higher-level functional extensions.

 

 

 

        afterJFinalStart()与 beforeJFinalStop() 

        The afterJFinalStart() and beforeJFinalStop() methods in JFinalConfig are for developers to override in the JFinalConfig inherited class. JFinal will call back the afterJFinalStart() method after the system starts, and the beforeJFinalStop() method before the system is shut down. These two methods conveniently give developers the opportunity to perform additional operations after project startup and before shutdown, such as creating scheduling threads after system startup or writing back to the cache before system shutdown.

 

       

        PropKit   

        The PropKit utility class is used to manipulate external configuration files. PropKit can be very convenient to use at any time and space in the system. The following is the sample code:

 

        

       As shown in the above code, PropKit can load multiple configuration files at the same time. The first loaded configuration file can be directly manipulated using the PorpKit.get(…) method, and the non-first loaded configuration file needs to use PropKit.use( …).get(…) to operate. The use of PropKit is not limited to YourJFinalConfig, it can be used anywhere in the project. The bottom layer of the getProperty method of JFinalConfig depends on the implementation of PropKit.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326625688&siteId=291194637