【问题标题】:Struts 2.3: runtime add InterceptorStruts 2.3:运行时添加拦截器
【发布时间】:2016-01-13 19:12:13
【问题描述】:

我的任务是添加/更改拦截器运行时(使用插件,无权访问父配置)。

在以前版本的 Struts (2.0) 中,这非常简单:类 InterceptorStackConfigActionConfig 有方法addInterceptoraddInterceptor

在较新的版本 (2.3) 中,方法移入 Builder 静态子类,我不能像以前那样使用它们。

所以这是一个问题。已经花了几天时间试图避免它。有人可以帮忙吗?

我之前的代码示例:

public class IpLoggingInterceptorConfiguration implements ConfigurationProvider {

private Interceptor interceptor;
private Configuration configuration;

@Override
public void init(Configuration configuration) throws ConfigurationException {
    this.configuration = configuration;
}

@Override
public void loadPackages() throws ConfigurationException {

    for (Object packageConfigName : configuration.getPackageConfigNames()) {
        try {
            String name = (String) packageConfigName;
            PackageConfig packageConfig = configuration.getPackageConfig(name);
            updatePackage(packageConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

public void updatePackage(PackageConfig packageConfig) {
    Map interceptorConfigs = packageConfig.getInterceptorConfigs();

    for (Object stack : interceptorConfigs.keySet()) {

        if (!(interceptorConfigs.get(stack) instanceof InterceptorStackConfig)) continue;

        InterceptorStackConfig interceptorStackConfig = (InterceptorStackConfig) interceptorConfigs.get(stack);

        InterceptorMapping interceptorMapping = new InterceptorMapping("iplogging", getInterceptor());

        List<InterceptorMapping> list = new ArrayList<InterceptorMapping>();
        list.addAll(interceptorStackConfig.getInterceptors());
        interceptorStackConfig.getInterceptors().clear();
        interceptorStackConfig.addInterceptor(interceptorMapping);
        interceptorStackConfig.addInterceptors(list);
    }

    for (String key : packageConfig.getActionConfigs().keySet()) {
        ActionConfig actionConfig = packageConfig.getActionConfigs().get(key);

        InterceptorMapping interceptorMapping = new InterceptorMapping("iplogging", getInterceptor());

        List<InterceptorMapping> list = new ArrayList<InterceptorMapping>();
        list.addAll(actionConfig.getInterceptors());
        actionConfig.getInterceptors().clear();
        actionConfig.addInterceptor(interceptorMapping);
        actionConfig.addInterceptors(list);
    }
}


@Override
public void destroy() {
}

@Override
public boolean needsReload() {
    return false;
}

@Override
public void register(ContainerBuilder arg0, LocatableProperties arg1)
        throws ConfigurationException {
}

public Interceptor getInterceptor() {
    return interceptor;
}

public void setInterceptor(Interceptor interceptor) {
    this.interceptor = interceptor;
}
}

【问题讨论】:

  • 因为现在 interceptorStackConfig.getInterceptors() - 是 UnmodifiableCollection :(
  • 什么是次要版本?
  • 我认为修改是因为 xwork 的新版本行:mvnrepository.com/artifact/org.apache.struts.xwork/xwork-core
  • 如果您无权访问父配置,您如何运行项目?
  • 没有想太多你真正想要做什么,你知道你可以看看 Spring AOP。如果您想要应用程序级别的日志记录,那几乎就是横切关注点的定义,因此它是用于 AOP 的规范示例。

标签: java struts2 interceptor struts2-interceptors interceptorstack


【解决方案1】:

我发现并知道,这个解决方案很丑陋,但很简单,而且很有效……也许有人有更好的解决方案。

try {
            Field interceptorsListField=InterceptorStackConfig.class.getDeclaredField("interceptors");
            interceptorsListField.setAccessible(true);
            List<InterceptorMapping> interceptorsList= (List<InterceptorMapping>) interceptorsListField.get(interceptorStackConfig);

            List<InterceptorMapping> list = new ArrayList<>();
            list.add(interceptorMapping);
            list.addAll(interceptorStackConfig.getInterceptors());
            interceptorsListField.set(interceptorStackConfig,list);
        } catch (Exception e) {
            e.printStackTrace();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多