JFinal 自動路由註冊,節省開發時間

1、路由註冊的實現java

       路由自動註冊實現起來挺簡單的,只需拿到全部的Controller配置到路由就行,代碼以下ide

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;
	}

	@SuppressWarnings("unchecked")
	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;
	}

	@Override
	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);
		}
	}
}

2、使用this

/**
	 * 配置訪問路由
	 */
	@Override
	public void configRoute(Routes me) {
		me.add(new AutoRoute("com.nmtx.manager.controller"));
	}

就能夠專心寫你的方法了,註解ActionKey可覆蓋自動路由的配置插件

若是你的Controller叫UserController,訪問路徑就是/user+methodNamecode

是否是很爽,結合自動錶註冊插件,參數校驗插件,增刪改的一步封裝,用JFinal開發應用,真正的作到了只關注業務,讓你有更多時間,陪妹子,陪家人,最後感謝波總的JFinal神器路由

相關文章
相關標籤/搜索