1

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

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

在较新的版本(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;
}
}
4

1 回答 1

0

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

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();
        }
于 2015-10-23T13:45:17.367 回答