jfinal-4.9.01 自定义KvGetter失败
KvGetter
package com.litong.jfinal.getter; import com.jfinal.core.Action; import com.jfinal.core.Controller; import com.jfinal.core.paragetter.ParaGetter; import com.jfinal.kit.Kv; import lombok.extern.slf4j.Slf4j; @Slf4j public class KvGetter extends ParaGetter<Kv> { public KvGetter(String parameterName, String defaultValue) { super(parameterName, defaultValue); log.info("执行"); } @Override public Kv get(Action action, Controller c) { log.info("执行"); return c.getKv(); } @Override protected Kv to(String v) { log.info("执行"); return null; } }
在main中注册
package com.litong.jfinal; import com.jfinal.config.Constants; import com.jfinal.config.Handlers; import com.jfinal.config.Interceptors; import com.jfinal.config.JFinalConfig; import com.jfinal.config.Plugins; import com.jfinal.config.Routes; import com.jfinal.core.paragetter.ParaProcessorBuilder; import com.jfinal.kit.Kv; import com.jfinal.server.undertow.UndertowConfig; import com.jfinal.server.undertow.UndertowServer; import com.jfinal.template.Engine; import com.litong.jfinal.getter.KvGetter; import com.litong.jfinal.handler.ActionSuffixHandler; import com.litong.jfinal.route.AdminRoutes; import com.litong.jfinal.route.ApiRoutes; import com.litong.jfinal.route.FrontRoutes; import com.litong.jfinal.route.SystemRoutes; import com.litong.jfinal.utils.PropKitUtil; import com.litong.jfinal.utils.UndertowUtil; import com.litong.utils.dll.LibraryUtil; import com.litong.utils.exec.FrpUtil; import com.litong.utils.ip.IPUtil; public class Application extends JFinalConfig { private static String configFileName = PropKitUtil.configFileName; public static void main(String[] args) { LibraryUtil.addLibary(); long start = System.currentTimeMillis(); UndertowUtil.server = UndertowServer.create(Application.class, configFileName); // 启动Server全局共享 UndertowUtil.server.addSystemClassPrefix("com.litong.jfinal.utils.UndertowUtil"); UndertowUtil.server.start(); UndertowConfig undertowConfig = UndertowUtil.server.getUndertowConfig(); ParaProcessorBuilder.me.regist(Kv.class, KvGetter.class, null); int port = undertowConfig.getPort(); String contextPath = undertowConfig.getContextPath(); long end = System.currentTimeMillis(); System.out.println(IPUtil.getThisUrl(port, contextPath)); System.out.println("启动完成,共使用了" + (end - start) + "ms"); // startFrp(); } public void configConstant(Constants me) { me.setInjectDependency(true); me.setInjectSuperClass(true); } public void configRoute(Routes me) { me.setMappingSuperClass(true); me.add(new FrontRoutes()); // 前端路由 me.add(new AdminRoutes()); // 后端路由 me.add(new SystemRoutes()); // 系统路由 me.add(new ApiRoutes()); // API路由 } @Override public void configEngine(Engine me) { } @Override public void configPlugin(Plugins me) { } @Override public void configInterceptor(Interceptors me) { } @Override public void configHandler(Handlers me) { me.add(new ActionSuffixHandler()); } /* * 使用onStart会拖慢jfinal启动速度 * afterJFinalStart 也会拖慢jfinal启动速度 */ public static void startFrp() { // 默认会阻塞主线程,所以新开一个线程启动 FrpUtil frpUtil = new FrpUtil(); Thread frpClientThread = new Thread(frpUtil, "frp client"); frpClientThread.run(); } }
编写Contrller,传入形参为库kv
package com.litong.jfinal.controler; import com.jfinal.core.Controller; import com.jfinal.kit.Kv; /** * @author bill robot * @date 2020年8月17日_下午7:46:23 * @version 1.0 * @desc */ public class TestController extends Controller { public void kv(Kv kv) { renderJson(kv); } }
在浏览器中测试
http://192.168.0.10:11023/jfinal-4.2-web/test/kv?name=litong&pswd=00000000
返回的是1个空对象,并没有封装成功
我也一直不知道 实现 ParaGetter实现get和to方法的含义,能不能讲解一下
项目:JFinal
public ParaGetter(String parameterName, String defaultValue){
this.parameterName = parameterName;
this.defaultValue = to(defaultValue);
}
在ParaGetter的构造方法中调用的to方法,然后会执行子类的to方法。
比如IntegerGetter这个类,看它的用法:
public IntegerGetter(String parameterName, String defaultValue) {
super(parameterName, defaultValue);
}
@Override
public Integer get(Action action, Controller c) {
// 如果客户端没有传来匹配值,就返回默认值,getDefaultValue在父类
return c.getParaToInt(getParameterName(), getDefaultValue());
}
@Override
protected Integer to(String defaultValue) {
if(StrKit.notBlank(defaultValue)){
return Integer.parseInt(defaultValue);
}
return null;
}
简单来说就是 String defaultValue 》to 转换 》T 任何对象