【发布时间】:2016-01-13 19:12:13
【问题描述】:
我的任务是添加/更改拦截器运行时(使用插件,无权访问父配置)。
在以前版本的 Struts (2.0) 中,这非常简单:类 InterceptorStackConfig 和 ActionConfig 有方法addInterceptor 和 addInterceptor。
在较新的版本 (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