4 Star 4 Fork 0

zcltd-btg / btg-autoroute-jfinal

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

btg-autoroute-jfinal

项目说明

jfinal自动路由插件。

项目特点

  • 支持多种配置方式;
  • 支持设置同一个类是否能被多次路由;
  • 支持部分没有源码或不修改源码等特殊情况不使用默认路由规则的手工映射的支持;
  • 支持避免重复路由的算法机制;
  • 支持路由标记(如应用于否验证登录超时,是否检查签名验证等场景);
  • 支持无侵入的(基于正则,只转发不侵入)手工路由(只需配置一个handler),支持但不限于严格的resultful;
  • 手工路由优先级 > jfinal原生路由(使用时注意jfinal原生路由被手工路由规则覆盖,如/my/{param}会覆盖/my/method1);

源码结构

              annotation:路由所使用的注解
               converter:className到controllerName的转换方式,可通过实现ControllerNameConverter自行扩展
                          SuffixControllerNameConverter类:去掉AutoRoutesAnnotation设置的ClsSuffixs(默认实现)
               formatter:controllerName到actionKey的转换方式,可通过实现ControllerNameFormatter自行扩展
                          AllToLowerControllerNameFormatter:controllerName全部转换为小写(默认实现)
                          FirstToLowerControllerNameFormatter:controllerName首字母转换为小写,其余保持不变
                 handler:手工路由需要使用到的handler
  AutoRoutesAnnotation类:核心入口类,提供直接使用的api
     HttpRequestMethod类:http请求协议类型枚举

三方依赖

  1. jfinal:https://gitee.com/jfinal/jfinal
  2. servlet-api:jfinal的handler需要;
  3. slf4j:插件日志框架使用(jdk log、log4j、connons log等自行选择,请自行脑补slf4j正确姿势);
  4. btg-util:开源工具包,https://gitee.com/zcltd-btg/btg-util
  5. junit:测试类使用(生产环境可不添加);

使用说明

(可参见AutoRoutesAnnotation类注释)
/**
 * jfinal支持注解的自动路由
 * <p/>
 * 关于注解:
 * 1、namespace作用范围:globleNamespace > packgeNamespace > classNamespace
 * 2、对于classNamespace:优先使用AutoRouteNamespace和AutoRouteUrl
 * 3、对于classNamespace:未配置AutoRouteNamespace和AutoRouteUrl时,优先使用AutoRoute
 * 4、对于classNamespace:全部未配置时,使用默认
 * 5、若配置了自定义路由,自定义路由优先级最高;
 * 6、当指定了controllerClass可重复注册或AutoRouteRepeat为true时(配置优先级>注解优先级),同一个类可被多次路由;为false时,
 * 自动忽略后续的配置使用第一次路由配置;
 * <p/>
 * 路由规则:
 * 1、默认控制器固定为/;
 * 2、普通控制器最终路由为:globleNamespace+packgeNamespace+classNamespace;
 * 3、当未指定命名空间时,根据添加文件夹(或包名)为起始位置,使用子文件夹作为命名空间
 * 4、当未指定访问路径时,类名转换为小写去掉action或controller作为访问路径
 * <p/>
 * 手动路由:
 * 1、手动路由需在方法上添加注解ManuallyRoute,并指定url和http(可选,默认为空字符串)、method(可选,默认为全部);
 * 2、手工路由原理为通过handler将路由转发到真实jfinal路由,所以与标记无关;
 * 3、url可指定占位参数,最终会通过正则将占位参数转换为request的attribute;
 * 4、手动路由是方法级别的路由,跟前面的action路由无冲突,action路由规则仍然有效;
 * <p/>
 * 关于tag:
 * 1、tag标记目标为jfinal的路由;(重要)
 * 2、支持多个tag,支持注解和手工添加;
 * 3、支持根据url获取tag和验证url是否包含tag;
 * 4、路由时可使用AutoRoutesAnnotation.create(me)初始化实例,可在interceptor中使用AutoRoutesAnnotation.getInstance()获取实例;
 */

1、使用全注解
@AutoRoute(namespace = "/mynamespace", url = "cust")
public class MyController extends Controller {
    public void xxx() {

    }
}
映射结果:/mynamespace/cust => MyController

2、使用子注解
@AutoRouteNamespace("/mynamespace")
public class MyController extends Controller {
    public void xxx() {

    }
}
映射结果:/mynamespace/my => MyController

@AutoRouteUrl("cust")
public class MyController extends Controller {
    public void xxx() {

    }
}
映射结果:/cust => MyController

@AutoRouteNamespace("/mynamespace")
@AutoRouteUrl("cust")
public class MyController extends Controller {
    public void xxx() {

    }
}
映射结果:/mynamespace/cust => MyController

3、混合使用
@AutoRoute(namespace = "/p", url = "pcust")
@AutoRouteNamespace("/s")
public class MyController extends Controller {
    public void xxx() {

    }
}
映射结果:/s/pcust => MyController

@AutoRoute(namespace = "/p", url = "pcust")
@AutoRouteUrl("scust")
public class MyController extends Controller {
    public void xxx() {

    }
}
映射结果:/p/scust => MyController

@AutoRoute(namespace = "/p", url = "pcust")
@AutoRouteNamespace("/s")
@AutoRouteUrl("scust")
public class MyController extends Controller {
    public void xxx() {

    }
}
映射结果:/s/scust => MyController

4、是否可以被重复路由的annotation(默认为true)
@AutoRouteRepeat
public class MyController extends Controller {
    public void xxx() {

    }
}

@AutoRouteRepeat(false)
public class MyController extends Controller {
    public void xxx() {

    }
}

5、手工路由
原理:通过对url的匹配和处理,将基于规则的路由转换为jfinal原始路由,所以对原始路由无侵入
注意:匹配处理使用了正则,需正确使用,避免url规则与正则规则冲突和错误匹配,
      默认参数名占位匹配正则为\{[^\{\}]+\},url参数值匹配正则为[^/\\-\\?=&]+,若不满足要求可通过
      setParamKeyRegStr()、setParamKeyReplaceRegStr()、setParamValueRegStr()自行更改设置
/**
 * @AutoRoute(url = "/ar") //将自动路由controllerKey指定为/ar,详见自动路由说明
 * @ManuallyRoute(url = "/mr") //将手工路由controllerKey指定为/mr,对自动路由不影响,值影响手工路由,不指定时默认为自动路由一样
 */
public class MyController extends Controller {

    /**
     * url:/my/param_value
     * 只能通过GET、POST访问(默认)
     */
    @ManuallyRoute(url = "{param}")
    public void testA() {
        String param = getAttr("path.param");//值为param_value

    }

    /**
     * url:/my/test2/param_value
     * 可以通过POST请求方式访问
     */
    @ManuallyRoute(url = "/test2/{param}", method = HttpRequestMethod.POST)
    public void testB() {
        String param = getAttr("path.param");//值为param_value
    }
}

6、tag标记
public class MyController extends Controller {
    @RouteTag("uncheckSessionTimeout")
    public void xxx() {

    }

    @RouteTag({"uncheckSessionTimeout","uncheckSign"})
    public void yyy() {

    }
}

7、jfinal配置
public void configRoute(Routes me) {
    AutoRoutesAnnotation ara = AutoRoutesAnnotation.create(me);

    //设置需要扫描的路径及包名及包名前缀
    ara.setClassPath(XXX.class.getResource("/").getPath());
    ara.setClassPathPre("/WEB-INF/classes");

    //设置扫描的jar路径及jar包
    ara.setJarPath(new File(XXX.class.getResource("/").getPath()).getParent() + File.separator + "lib" + File.separator);
    ara.addJar("controller-in-jar\\.jar");

    //设置要映射package及namespace
    ara.addPackage("com.axinfu.sf.controller");
    ara.addPackage("com.axinfu.sf.ws","/ws");

    //设置默认控制器(将"/"映射到默认控制器)
    ara.setDefaultController(InitAction.class);

    //设置忽略的类
    ara.addSkip(BaseController.class);

    //添加特殊自定义注解(当无源代码或不修改源代码等特殊情况下可用此方式)
    ara.addRoute("/custnamespace/cust1",Cust1Controller.class);
    ara.addRoute("/custnamespace/cust2",Cust2Controller.class);

    //手工设置Controller是否可以重复映射
    ara.setControllerRepeat(UserAction.class, true);

    //手工设置Controller的method映射
    ara.setManualRoute("/my/{param}", "/my/testA", new HttpRequestMethod[]{HttpRequestMethod.GET, HttpRequestMethod.POST});

    //手工添加tag
    ara.addTag("/mycontroller/myaction","uncheckSessionTimeout");
    ara.addTags("/mycontroller/myaction",new String[]{"uncheckSessionTimeout","uncheckSign"});

    //启动路由
    ara.route();
}

8、tag标记的手工指定和使用(jfinal interceptor)
一、自定义手工标记:
{
    AutoRoutesAnnotation autoRoutesAnnotation = AutoRoutesAnnotation.getInstance();
    autoRoutesAnnotation.addTag("/mycontroller/myaction","uncheckSessionTimeout");
    autoRoutesAnnotation.addTags("/mycontroller/myaction",new String[]{"uncheckSessionTimeout","uncheckSign"});
}
或在autoroute插件处直接手工标记(使用前标记均有效)

二、对标记的使用(如session timeout检查场景)
@Override
public void intercept(Invocation inv) {
    Controller targetController = inv.getController();
    String controllerKey = inv.getControllerKey();
    controllerKey = controllerKey.endsWith("/") ? controllerKey : controllerKey + "/";
    String url = controllerKey + inv.getMethodName();

    if (AutoRoutesAnnotation.getInstance().containsTag(url, "uncheckSessionTimeout")) {
        inv.invoke();
        return;
    }

    //其他session timeout验证逻辑
    //如session过期则跳转到登录页面等
}

版本更新记录

4.0.3:
1、修复tag处理bug;

v4.0.2:
1、addJar()时使用原始正则,不做中间处理;

v4.0.1:
1、转为独立maven依赖,去掉parent;

v3.0.4:
1、btg-parent升级到v2.0.3,主要优化slf4j日志;

v3.0.3:
1、btg-parent升级到v2.0.1;

v3.0.2:
1、加入统计依赖管理;

v3.0.1:
1、统一迁移至公司名下;

v2.0.2:
1、对classPath的默认值进行了优化;

v2.0.1:
1、转为maven项目;

v1.2.0:
1、增加手工路由参数名称重复的验证;

v1.1.10:
1、优化手工路由匹配规则,完全基于正则表达式,抛弃必须用/分隔的规则;
2、手工路由支持手工设置是否缓存,默认为true;

v1.1.9:
1、增加对class级别的手工路由的支持(即支持多个Class中手工路由为同一个ControllerKey);
2、去除session timeout标记,更改为增强的tag标记;

v1.1.8:
1、删除验证规则;

v1.1.7:
1、修改验证规则:手工路由不能与原始路由相同改为日志警告;

v1.1.6:
1、增加验证规则:手工路由不能与原始路由相同;

v1.1.5:
1、修复手工路由缓存放入时的错误;

v1.1.4:
1、更改path匹配后的参数名称url开头为path开头;

v1.1.3:
1、路由协议拆分;
2、AutoRouteUrl多一个/;
3、标记手工路由为过期,强调支持但不推荐使用;

v1.1.2:
1、增加部分没有源码或不修改源码等特殊情况的手工添加方法级别的路由;
2、为避免歧义,将原来getInstance(Route me)更换为create(Route me);
3、完善README.md;

v1.1.1:
1、增加方法级别的路由;
2、支持严格的resultful风格路由;

v1.0.7:
1、增加全局namespace,表示整个项目路由表(默认控制器除外)所使用的namespace,默认为不设置。
   如:全局namespace为"/v1",原始路由为"/sys/user",最终实际路由为"/v1/sys/user";
2、支持对packge单独设置namespace,表示指定packge下统一使用设置的namespace,无需再在Controller类中设置该namespace,默认不设置;
   如:全局namespace为"/v1",packge的namespace为"/ws",原始路由为"/sys/user",最终实际路由为"/v1/ws/sys/user";

v1.0.6:
1、考虑到jar包以后维护可能会比较频繁,特统一版本号命名方式;

v1.0.5:
1、修复多级package路由中的.号问题(将.转换成/);
2、增加控制器类名称转换扩展IControllerNameConverter(默认为去后缀实现并提供增加、删除后缀,内置controller、service、action三种后缀)和
   二次格式化扩展IControllerNameFormatter(提供全部子母转小写实现和首字母转小写实现,默认为全部子母转小写),扩展方式为实现接口并编写
   自己的实现,为AutoRoutesAnnotation注入自己的实现即可(调用set方法);

v1.0.4:
1、增加对是否可以被重复路由的说明及使用配置方式;
2、增加标注session过期验证的支持及annotation;

v1.0.3:
1、增加了Controller Class是否重复的设置和annotation;
2、修复了若干bug;

v1.0.2:
1、增加了自定义路由

v1.0.1:
1、jfinal基于annotation的自动路由;

豆圆

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. <<<<<<< HEAD Copyright 2016 jounzhang ======= Copyright 2018 豆圆 >>>>>>> b204f11d199a2ed0b888e77fa19038abe11951c3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

jfinal自动路由 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/zcltd-btg/btg-autoroute-jfinal.git
git@gitee.com:zcltd-btg/btg-autoroute-jfinal.git
zcltd-btg
btg-autoroute-jfinal
btg-autoroute-jfinal
master

搜索帮助