Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取Struts2中已配置操作的列表_Java_Spring_Struts2_Struts Action_Struts2 Config Browser - Fatal编程技术网

Java 获取Struts2中已配置操作的列表

Java 获取Struts2中已配置操作的列表,java,spring,struts2,struts-action,struts2-config-browser,Java,Spring,Struts2,Struts Action,Struts2 Config Browser,在一个使用Struts2(2.3.20)的项目中,我想通过 应用程序启动时配置的操作(名称、类、命名空间、方法) 我正在使用 Struts 2.3.20 struts-spring插件 struts约定插件 供参考:我以前在beans和Struts注入方面做过一些工作,所以对此并不完全了解,但我一直在解决这里提到的问题 任何关于如何获得这一点的建议都将不胜感激 进一步解释 阅读下面Andrea的回答,我发现我需要解释我需要什么 我正在为应用程序构建应用程序菜单生成器功能。我的计划是获取操作配

在一个使用Struts2(2.3.20)的项目中,我想通过 应用程序启动时配置的操作(名称、类、命名空间、方法)

我正在使用

  • Struts 2.3.20
  • struts-spring插件
  • struts约定插件
供参考:我以前在beans和Struts注入方面做过一些工作,所以对此并不完全了解,但我一直在解决这里提到的问题

任何关于如何获得这一点的建议都将不胜感激

进一步解释 阅读下面Andrea的回答,我发现我需要解释我需要什么

我正在为应用程序构建应用程序菜单生成器功能。我的计划是获取操作配置,并根据所选操作类和方法的注释中的信息构建“菜单节点”树

我对配置浏览器中的代码的问题是
配置(xwork)在Struts组件之外似乎不可用。因为这是一个应用程序启动任务,所以它并不真正适合Struts的MVC组件模型。我想将菜单构建初始化放在
ServletContextListener

假例子 这里的每个请求只是连接actionconfig注释my_custom_菜单。由此,我可以生成一个菜单结构,该结构由操作类和方法的注释提供

public class ActionMenuBuilderListener implements ServletContextListener {
  @Override
  public void contextInitialized(ServletContextEvent arg0) {
    List<ActionCfg> actions = Struts.getConfiguredActions(); // thisi is where I'd like some help
    for(ActionCfg action : actions) {
      MenuAnnotation annotation = getAnnotationFromMethodOrClass(action);
      if(annotation != null) {
        addMenuItem(action, annotation);
      }
    }
  }
}
然后,我编写了
DispatcherListener

public class ActionMenuDispatcherListener implements DispatcherListener {

private ServletContext servletContext;

...

@Override
public void dispatcherInitialized(Dispatcher dispatcher) {

    Map<String, PackageConfig> packages = dispatcher
        .getConfigurationManager().getConfiguration()
        .getPackageConfigs();

    Map<String, Map<String, ActionConfig>> runtimeActionConfigs = dispatcher
        .getConfigurationManager().getConfiguration()
        .getRuntimeConfiguration().getActionConfigs();

    for (String packageKey : runtimeActionConfigs.keySet()) {

    Map<String, ActionConfig> actionConfigs = runtimeActionConfigs
        .get(packageKey);

    for (String actionKey : actionConfigs.keySet()) {
        ActionConfig actionConfig = actionConfigs.get(actionKey);
        PackageConfig packageConfig = packages.get(actionConfig
            .getPackageName());

        if (packageConfig != null) {
        String actionName = actionConfig.getName();
        String namespace = packageConfig.getNamespace();
        try {

            ActionMenu methodAnnotation = getMethodAnnotation(actionConfig);

            if (methodAnnotation != null) {
            String annotationInfo = methodAnnotation.value();
            log.debug("[{}, {}, {}]", namespace, actionName,
                annotationInfo);
            }

        } catch (ClassNotFoundException e) {
            log.error("{}: {}", e.getClass().getSimpleName(),
                e.getMessage());
        }
        }
    }
    }
}

protected ActionMenu getMethodAnnotation(ActionConfig actionConfig)
    throws ClassNotFoundException {
    String className = actionConfig.getClassName();
    String methodName = actionConfig.getMethodName();
    Class<?> actionClass = Class.forName(className);
    try {
    Method method = actionClass.getDeclaredMethod(methodName, null);
    ActionMenu annotation = method.getAnnotation(ActionMenu.class);
    return annotation;
    } catch (NoSuchMethodException | SecurityException e) {
    // log.error("{}: {}", e.getClass().getSimpleName(),
    // e.getMessage());
    }
    return null;
}

}
公共类ActionMenuDispatcherListener实现DispatcherListener{
私有ServletContext ServletContext;
...
@凌驾
已初始化公共无效调度程序(调度程序){
映射包=调度程序
.getConfigurationManager().getConfiguration()
.getPackageConfigs();
映射runtimeActionConfigs=调度程序
.getConfigurationManager().getConfiguration()
.getRuntimeConfiguration().getActionConfigs();
对于(字符串packageKey:runtimeActionConfigs.keySet()){
Map actionConfigs=runtimeActionConfigs
.get(packageKey);
for(字符串actionKey:actionConfigs.keySet()){
ActionConfig ActionConfig=actionConfigs.get(actionKey);
PackageConfig PackageConfig=packages.get(actionConfig
.getPackageName());
如果(packageConfig!=null){
字符串actionName=actionConfig.getName();
String namespace=packageConfig.getNamespace();
试一试{
ActionMenu methodAnnotation=getMethodAnnotation(actionConfig);
if(methodAnnotation!=null){
字符串annotationInfo=methodAnnotation.value();
log.debug(“[{},{},{}]”,命名空间,actionName,
注释信息);
}
}catch(classnotfounde异常){
log.error(“{}:{}”,e.getClass().getSimpleName(),
e、 getMessage());
}
}
}
}
}
受保护的ActionMenu getMethodAnnotation(ActionConfig ActionConfig)
抛出ClassNotFoundException{
字符串className=actionConfig.getClassName();
String methodName=actionConfig.getMethodName();
Class actionClass=Class.forName(className);
试一试{
Method=actionClass.getDeclaredMethod(methodName,null);
ActionMenu annotation=method.getAnnotation(ActionMenu.class);
返回注释;
}catch(NoSuchMethodException | SecurityException e){
//log.error(“{}:{}”,e.getClass().getSimpleName(),
//e.getMessage());
}
返回null;
}
}

以防万一其他人也这么想:)

你可以自由地为你的个人成长构建这个东西,但要注意它已经存在了

它被称为(
struts2-config-browser-plugin-2.3.20.jar

默认情况下,它包含在Maven原型中,您必须记住在投入生产之前删除它

导入后,可在URL上使用:

//www.SERVER_NAME.com:8080/WEBAPP_NAME/配置浏览器/actionNames

它为您提供了所需的确切信息:操作、方法、结果、参数、映射等,如下所示:


首先,在加载和解析配置后,您需要挂接到应用程序初始化过程中。其中一种方法是实现
DispatcherListener
,您需要将其添加到
Dispatcher
。您可以在
ServletContextListener#contextInitialized
方法中执行此操作

第二个难题是获得动作配置。这非常简单,因为
Dispatcher
的实例作为参数传递到
dispatcherInitialized
方法中。要获取所有当前操作配置,请获取
RuntimeConfiguration
,它保存
Map
中的数据,其中第一个Map键是包名称空间,第二个Map键是action name,
ActionConfig
保存有关操作的所有信息。因为您需要一个类名,所以请使用它的
getClassName()
方法

公共类ActionMenuBuilderListener实现ServletContextListener、DispatcherListener{
@凌驾
public void contextInitialized(ServletContextEvent sce){
Dispatcher.addDispatcherListener(此);
}
@凌驾
公共无效dispatcherInitialized(Dispatcher du){
映射运行时ActionConfigs=du
.getConfigurationManager().getConfiguration().getRuntimeConfiguration()
.getActionConfigs();
}
//其他方法
}

当然,不要忘记在web.xml中注册您的侦听器。

谢谢。我知道
config插件
,并且我已经研究了它的代码。然而,我会使用kist的操作来寻找一个定制的注释,并根据信息构建应用程序菜单。我可以看出我不清楚我是否需要动作配置的
列表,我将编辑我的问题。你能展示一个真实世界(假的)的例子吗
public class ActionMenuDispatcherListener implements DispatcherListener {

private ServletContext servletContext;

...

@Override
public void dispatcherInitialized(Dispatcher dispatcher) {

    Map<String, PackageConfig> packages = dispatcher
        .getConfigurationManager().getConfiguration()
        .getPackageConfigs();

    Map<String, Map<String, ActionConfig>> runtimeActionConfigs = dispatcher
        .getConfigurationManager().getConfiguration()
        .getRuntimeConfiguration().getActionConfigs();

    for (String packageKey : runtimeActionConfigs.keySet()) {

    Map<String, ActionConfig> actionConfigs = runtimeActionConfigs
        .get(packageKey);

    for (String actionKey : actionConfigs.keySet()) {
        ActionConfig actionConfig = actionConfigs.get(actionKey);
        PackageConfig packageConfig = packages.get(actionConfig
            .getPackageName());

        if (packageConfig != null) {
        String actionName = actionConfig.getName();
        String namespace = packageConfig.getNamespace();
        try {

            ActionMenu methodAnnotation = getMethodAnnotation(actionConfig);

            if (methodAnnotation != null) {
            String annotationInfo = methodAnnotation.value();
            log.debug("[{}, {}, {}]", namespace, actionName,
                annotationInfo);
            }

        } catch (ClassNotFoundException e) {
            log.error("{}: {}", e.getClass().getSimpleName(),
                e.getMessage());
        }
        }
    }
    }
}

protected ActionMenu getMethodAnnotation(ActionConfig actionConfig)
    throws ClassNotFoundException {
    String className = actionConfig.getClassName();
    String methodName = actionConfig.getMethodName();
    Class<?> actionClass = Class.forName(className);
    try {
    Method method = actionClass.getDeclaredMethod(methodName, null);
    ActionMenu annotation = method.getAnnotation(ActionMenu.class);
    return annotation;
    } catch (NoSuchMethodException | SecurityException e) {
    // log.error("{}: {}", e.getClass().getSimpleName(),
    // e.getMessage());
    }
    return null;
}

}