如何通过 Controller 获取 Routes.add(controllerKey,controllerClass)

需求:做权限拦截

比如 进入产品页面,

    权限url  /product/ 对应 ProductController.index()方法

    页面内有一个Ajax grid,对应方法是ProductController.page()。

    url  /product/ 对应两个方法 index与page方法,目前的实现思路是获取ProductController URL,在page方法注解 ProductController URL。如果 Routes.add修改代码内的ProductController.page()也会随机跟着变。

评论区

JFinal

2016-10-09 19:58

通过 inv.getActionKey() 或者 inv.getControllerKey() 获取到的数据应该可以满足需求,大部分人都是这样做的

还有一种扩展是自定义注解,例如自定义一个 Role 或 Permission 注解,然后在拦截器中可以通过 inv.getMethod().getAnnotation() 获取到 action 方法上的注解,随后再做什么控制随意了

无为

2016-10-10 09:41

@JFinal 是一种方式,注解是一定要用的。
page()的注解@Role(ProductController.class,"index")。
获取注解,通过ProductController获取/product/,拿/product/index去做权限验证,所以想通过ProductController获取url。
通过 List allActionKeys = JFinal.me().getAllActionKeys(); 获取所有的url。
类似这样功能的反向,通过Action 获取url
public Action getAction(String url, String[] urlPara) {
return actionMapping.getAction(url, urlPara);
}

无为

2016-10-10 09:42

通过 通过 List allActionKeys = JFinal.me().getAllActionKeys(); 获取所有的url。但没有与Action的对应关系。

JFinal

2016-10-10 09:45

@无为 拦截器当前拦截的就是某个 action,这就是一种极其明确的对应关系,不需要再使用 actionKey 去获取 Action

当然,如果一定要获取,可以这样:JFinal.me().getAction(url, urlPara);

无为

2016-10-10 10:53

热门反馈

扫码入社