jfinal-4.2 自定hanlder, 拦截url中的指定后缀
[问题描述]
有1个请求是这个样子的,默认jfinal的contrler是不能处理的,因为url中有后缀名,如何将这个请求交个jfinal的contrller处理
http://xxx/ask.action
[解决办法]
定义一个handler将uri中的.actoion删除,获取不包含action的url,向后传递,传递到ActionHanlder,ActionHanlder会交给conroller处理
定义action
package com.litong.jfinal.handler; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jfinal.handler.Handler; import lombok.extern.slf4j.Slf4j; @Slf4j public class ActionSuffixHandler extends Handler { private List<String> suffixList = new ArrayList<>(); public ActionSuffixHandler() { suffixList.add(".action"); suffixList.add(".php"); } public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) { log.info("target:{}",target); for (String suffix : suffixList) { int lastIndexOf = target.lastIndexOf(suffix); if (target.lastIndexOf(suffix) > -1) { target = target.substring(0, lastIndexOf); } } log.info("target:{}",target); next.handle(target, request, response, isHandled); } }
添加action插件
@Override public void configHandler(Handlers me) { me.add(new ActionSuffixHandler()); }
controller的代码十分简单
package com.litong.jfinal.controler; import com.jfinal.core.Controller; import com.jfinal.kit.Kv; /** * @author bill robot * @date 2020年8月17日_下午5:20:56 * @version 1.0 * @desc */ public class IndexController extends Controller { public void index() { Kv kv = getKv(); renderJson(kv); } }
测试请求
http://192.168.0.10:11023/jfinal-4.2-web/index.php?s=/api/order/cart&delivery=20&shop_id=0&coupon_id=0&is_use_points=0&cart_ids=10156_0%2C10155_0%2C10154_0%2C10153_0&wxapp_id=10001&token=e591daec48cad2358dcbe8592bc9e23e
测试结果如下
完成