public void configConstant(Constants me) {
}
PropKit.use("config.properties");使用 PropKit.get("jdbcUrl") 获取
loadPropertyFile("config.properties");使用 getProperty("jdbcUrl") 获取
默认flase
me.setDevMode(PropKit.getBoolean("devMode"));
设置Action Report什么出现 默认true
me.setReportAfterInvocation(false);
默认是Freemarker
me.setViewType(ViewType.JSP);
默认WebRoot
me.setBaseViewPath("/WEB-INF/view"); --BaseViewPath
cos组件有效 jfinal默认有值 相对 绝对都可以 "/"代表绝对路径
默认是 upload
me.setBaseUploadPath("amoqiload"); UploadFile file = getFile("img");
me.setMaxPostSize(1024*1024*20); //设置最大上传大小
cos组件有效 jfinal默认有值 相对 绝对都可以 "/"代表绝对路径
默认是 download
me.setBaseDownloadPath("amoqidownload");
me.setFreeMarkerViewExtension(".ftl");
me.setJspViewExtension(".jtl");
me.setVelocityViewExtension(".vtl");
默认是 -
me.setUrlParaSeparator("~");
me.setI18nDefaultBaseName("i18n");
me.setI18nDefaultLocale("zh_CN"");
me.setError404View("404.html");
me.setErrorRenderFactory(errorRenderFactory);
me.setEncoding("GBK");
设置默认的xml渲染工厂 默认使用Freemarker render渲染
me.setXmlRenderFactory(自定义工厂);
me.setJsonDatePattern("yyyy-mm-dd HH:mm");
me.setJsonFactory(FastJsonFactory.me());
renderJson 和JsonKit底层依赖于JsonManager中设置的JsonFactory
me.setLogFactory(Slf4JLogFactory.me());
public void configRoute(Routes me) {
}
me.add(controllerKey, controllerClass, viewPath);
controllerKey:访问controller需要的路径(key)
controllerClass:需要访问的controller类
viewPath:视图路径
最后一个viewPath可以省略,省略后默认为第一个
me.add("/", IndexController.class);
me.add("/user", UserController.class,"/user");
finalView(视图位置) = baseViewPath(configConstant配置里) + viewPath(this) + view (render里)
当需要添加的路由过多,或者项目由大型团队开发可以拆分路由配置
可以新建一个类(此处为 FrontRoutes) 需要继承 Routes 添加路由
public class FrontRoutes extends Routes {
@Override
public void config() {
add("/",IndexController.class);
add("/user",UserController.class);
}
}
me.add(new FrontRoutes()); //configRoute配置 即可
Plugin都有自己的生命周期,
public void configPlugin(Plugins me) {
//Jfinal配置插件
//输入库连接池
//C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));
C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
//ORM Activerecord
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
arp.setShowSql(true);
arp.addMapping("user", User.class);
me.add(c3p0Plugin);
me.add(arp);
}
**重启程序或者热加载会重新启用插件**
如果需要自定义插件 需要实现IPlugin接口,并实现接口中的两个方法start(); stop();
MyPlugins myPlugins = new MyPlugins();
me.add(myPlugins);
拦截位置
级别
me.addGlobalActionInterceptor(new PhoneInterceptor()); //MainConfig MainConfig配置
public class PhoneInterceptor implements Interceptor { //PhoneInterceptor 自定义拦截器
@Override
public void intercept(Invocation inv) {
Controller c = inv.getController();
boolean isPhone = isPhone(c);
c.getSession().setAttribute("isPhone", isPhone);
inv.invoke();
}
public boolean isPhone(Controller c){
HttpServletRequest request = c.getRequest();
System.out.println(request.getHeader("user-agent"));
String userAgent = request.getHeader("user-agent");
if(userAgent!=null){
userAgent = userAgent.toLowerCase();
return userAgent.indexOf("iphone")!=-1 || userAgent.indexOf("android")!=-1;
}
return false;
}
}
me.add(new MyHandlers()); //MainConfig
public class MyHandlers extends Handler { //MyHandlers
@Override
public void handle(String target, HttpServletRequest request,
HttpServletResponse response, boolean[] isHandled) {
System.out.println("===log:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":"+target);
next.handle(target, request, response, isHandled);
}
}
@Override //MainConfig 中实现即可
public void beforeJFinalStop() {
System.out.println("beforeJFinalStop:JFinal关闭前需要处理的东西");
}