Strut2查看所有的路由mapping
Strut2查看所有的路由mapping
喔喔兒 发表于10个月前
Strut2查看所有的路由mapping
  • 发表于 10个月前
  • 阅读 4
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: Strut2 路由 action mapping

Strut2中,需要查看所有的action mapping时,可以使用两种方式,

1是通过继承ServletContextListener在初始化application时,动态反射,查找所有的注解;

2是通过Strut的Dispatcher中的ConfigurationManager查找;

以下是第一种方式:

private void test() {
    ClassScanner scanner = new ClassScanner();
    scanner.scanning("package to scanned", true);
    Map<String, Class<?>> classes = scanner.getClasses();
    for (Map.Entry<String, Class<?>> entry : classes.entrySet()) {
        String entryKey = entry.getKey();
        Class<?> value = entry.getValue();
        Namespace namespace = null;
        if (value.isAnnotationPresent(Namespaces.class)) {
            Namespaces namespacesAnnotation = value.getAnnotation(Namespaces.class);
            Namespace[] namespaces = namespacesAnnotation.value();
            for (int i = 0; i < namespaces.length; i++) {
                namespace = namespaces[i];
                getActionsByNamespace(namespace, value);
            }
        } else if (value.isAnnotationPresent(Namespace.class)) {
            namespace = value.getAnnotation(Namespace.class);
            getActionsByNamespace(namespace, value);
        } else {
            //no annotations
        }
    }
}
 
private void getActionsByNamespace(Namespace namespace, Class<?> type) {
    String apiName = namespace.value();
    Method[] methods = type.getDeclaredMethods();
    for (Method method : methods) {
        Action action = null;
        if (method.isAnnotationPresent(Actions.class)) {
            Actions actionsAnnotation = method.getAnnotation(Actions.class);
            Action[] actions = actionsAnnotation.value();
            for (int i = 0; i < actions.length; i++) {
                action = actions[i];
                getAction(apiName, action);
            }
        } else if (method.isAnnotationPresent(Action.class)) {
            action = method.getAnnotation(Action.class);
            getAction(apiName, action);
        }else {
            //not an action
        }
    }
}
 
private void getAction(String apiName, Action action) {
    System.out.println(apiName + "/" + action.value());
}

以下是第二种方式:

        Map<String, Map<String, ActionConfig>> configs =
                Dispatcher.getInstance().getConfigurationManager().getConfiguration().getRuntimeConfiguration().getActionConfigs();
        for (Map.Entry<String, Map<String, ActionConfig>> entry : configs.entrySet()) {
            String key = entry.getKey();
            Map<String, ActionConfig> value = entry.getValue();
            System.out.println("namespace : " + key);
            for (Map.Entry<String, ActionConfig> actionConfigEntry : value.entrySet()) {
                String actionName = actionConfigEntry.getKey();
                ActionConfig actionConfig = actionConfigEntry.getValue();
                System.out.println("action name : " + actionConfig.getName());
                System.out.println("method name : " + actionConfig.getMethodName());
                System.out.println("package name : " + actionConfig.getPackageName());
                System.out.println("class name : " + actionConfig.getClassName());
            }
        }

参考链接:

http://www.cnblogs.com/caroline/archive/2013/02/03/2890620.html 

http://blog.csdn.net/java2000_wl/article/details/7572828

http://www.programcreek.com/java-api-examples/index.php?api=org.apache.struts2.dispatcher.mapper.ActionMapping

https://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/dispatcher/mapper/DefaultActionMapper.html

http://blog.csdn.net/musa875643dn/article/details/45542305

标签: Struts actionMapping
共有 人打赏支持
粉丝 25
博文 55
码字总数 9584
×
喔喔兒
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: