一、前言
通过struts2依赖的xwork-core包com.opensymphony.xwork2.config.Configuration、com.opensymphony.xwork2.config.entities.ActionConfig及struts2-core包中的org.apache.struts2.dispatcher.Dispatcher类,进行获取Struts2配置信息、获取所有namespace名称、获取所有Action名称、获取所有Action类名称及所有Action类。
二、代码示例
import java.util.HashSet;@b@import java.util.Map;@b@import java.util.Set; @b@import org.apache.commons.lang.StringUtils;@b@import org.apache.struts2.dispatcher.Dispatcher; @b@import com.opensymphony.xwork2.config.Configuration;@b@import com.opensymphony.xwork2.config.ConfigurationManager;@b@import com.opensymphony.xwork2.config.entities.ActionConfig; @b@@b@public class StrutsUtil {@b@@b@ /**@b@ * 获取Struts2配置信息. @b@ * @return Struts2 Configuration@b@ */@b@ public static Configuration getConfiguration() {@b@ Dispatcher dispatcher = Dispatcher.getInstance();@b@ ConfigurationManager configurationManager = dispatcher.getConfigurationManager();@b@ return configurationManager.getConfiguration();@b@ }@b@@b@ /**@b@ * 获取所有namespace名称. @b@ * @return namespace名称的集合@b@ */@b@ public static Set<String> getAllNamespaces() {@b@ Set<String> namespaces = new HashSet<String>();@b@ Configuration configuration = getConfiguration();@b@ Map<String, Map<String, ActionConfig>> actionConfigs = configuration.getRuntimeConfiguration().getActionConfigs();@b@ for (String namespace : actionConfigs.keySet()) {@b@ namespaces.add(namespace);@b@ }@b@ return namespaces;@b@ }@b@@b@ /**@b@ * 获取所有Action名称. @b@ * @return Action名称的集合@b@ */@b@ public static Set<String> getAllActionName() {@b@ Set<String> actionNames = new HashSet<String>();@b@ Configuration configuration = getConfiguration();@b@ Map<String, Map<String, ActionConfig>> actionConfigs = configuration.getRuntimeConfiguration().getActionConfigs();@b@ for (String namespace : actionConfigs.keySet()) {@b@ Map<String, ActionConfig> actionConfigMap = actionConfigs.get(namespace);@b@ for (String actionName : actionConfigMap.keySet()) {@b@ actionNames.add(actionName);@b@ }@b@ }@b@ return actionNames;@b@ }@b@@b@ /**@b@ * 获取所有Action类名称(不包含com.opensymphony.xwork2.ActionSupport类). @b@ * @return Action类名称的集合@b@ */@b@ public static Set<String> getAllActionClassName() {@b@ Set<String> actionClassNames = new HashSet<String>();@b@ Configuration configuration = getConfiguration();@b@ Map<String, Map<String, ActionConfig>> actionConfigs = configuration.getRuntimeConfiguration().getActionConfigs();@b@ for (String namespace : actionConfigs.keySet()) {@b@ Map<String, ActionConfig> actionConfigMap = actionConfigs.get(namespace);@b@ for (String actionName : actionConfigMap.keySet()) {@b@ String actionClassName = actionConfigMap.get(actionName).getClassName();@b@ if (!StringUtils.equals(actionClassName, "com.opensymphony.xwork2.ActionSupport")) {@b@ actionClassNames.add(actionClassName);@b@ }@b@ }@b@ }@b@ return actionClassNames;@b@ }@b@@b@ /**@b@ * 获取所有Action类. @b@ * @return Action类的集合@b@ */@b@ @SuppressWarnings("unchecked")@b@ public static Set<Class> getAllActionClass() {@b@ Set<Class> actionClasss = new HashSet<Class>();@b@ Configuration configuration = getConfiguration();@b@ Map<String, Map<String, ActionConfig>> actionConfigs = configuration.getRuntimeConfiguration().getActionConfigs();@b@ for (String namespace : actionConfigs.keySet()) {@b@ Map<String, ActionConfig> actionConfigMap = actionConfigs.get(namespace);@b@ for (String actionName : actionConfigMap.keySet()) {@b@ Class actionClass = actionConfigMap.get(actionName).getClass();@b@ actionClasss.add(actionClass);@b@ }@b@ }@b@ return actionClasss;@b@ }@b@@b@}