我的任务是添加/更改拦截器运行时(使用插件,无权访问父配置)。
在以前的 Struts (2.0) 版本中,这非常简单:InterceptorStackConfig和ActionConfig类具有addInterceptor和addInterceptors方法。
在较新的版本(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;
}
}