1

我有一個任務添加/更改攔截器運行時(帶有插件,無權訪問父配置)。Struts 2.3:運行時添加攔截器

在以前版本的Struts(2.0),這是很簡單的:類InterceptorStackConfigActionConfig有方法addInterceptoraddInterceptors

在較新的版本(2.3)中,方法轉移到生成器靜態子類,我不能像以前一樣使用它們。

所以這是一個問題。已經花了好幾天試圖避免它。任何人都可以幫忙

我前面的代碼示例:

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; 
} 
} 
+0

因爲現在interceptorStackConfig.getInterceptors() - 是UnmodifiableCollection :( – Likrant

+0

什麼是次要版本 –

+0

我認爲修改是因爲新版本的行XWork的: http://mvnrepository.com/artifact/org? .apache.struts.xwork/xwork-core – Likrant

回答

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(); 
     }