Strut2查看所有的路由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

转载于:https://my.oschina.net/wower/blog/839998

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值