JFinal automatic routing registration saves development time

1. Implementation of Route Registration

Route auto-registration is easy to implement, just get all the Controller configuration to route, the code is as follows

package com.nmtx.route;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import com.nmtx.utils.ClassUtils;

public class AutoRoute extends Routes {
    public  static Logger log = LoggerFactory. getLogger(AutoRoute.class);
    private String packageName;

    public AutoRoute(String packageName) {
        this.packageName = packageName;

    public List<Class<? extends Controller>> getControllerClass() throws ClassNotFoundException {
        List<Class<?>> allClass = ClassUtils.scanPackage(packageName);
        List<Class<? extends Controller>> controllerClasses = new ArrayList<Class<? extends Controller>>();
        for (Class<?> controllerClass : allClass) {
            controllerClasses.add((Class<? extends Controller>) controllerClass);
        return controllerClasses;

    public void config() {
        try {
            List<Class<? extends Controller>> controllerClassList = getControllerClass();
            for (Class<? extends Controller> controllerClass : controllerClassList) {
                add("/" + controllerClass.getSimpleName().replace("Controller", "").toLowerCase(), controllerClass);
        } catch (ClassNotFoundException e) {
            log.error("auto route exception ",e);

Two, use

     * Configure access routing
    public void configRoute(Routes me) {
        me.add(new AutoRoute("com.nmtx.manager.controller"));

You can concentrate on writing your method and annotate ActionKey to override the configuration of automatic routing

If your Controller is called UserController, the access path is / user+methodName

Is it very cool, combined with automatic table registration plug-ins, parameter verification plug-ins, add and delete one step package, use JFinal to develop applications, really do only focus on business, so that you have more time to accompany your sister, to accompany family members, and finally thank the total JFinal artifact.

Keywords: Java

Added by Boo-urns on Fri, 04 Oct 2019 20:08:31 +0300