![](/img/trans.png)
[英]Struts 2.3 Store Interceptor : How to pass ActionErrors from Interceptor to Action?
[英]Struts 2.3: runtime add Interceptor
我有一個添加/更改攔截器運行時的任務(使用插件,無權訪問父配置)。
在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;
}
}
我發現並知道,這種解決方案很丑陋,但簡單又有效……也許有人會更好。
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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.