2011-02-11 44 views
0

我需要覆蓋默認轉換消息 因此,我試圖爲我的應用程序做一個自定義轉換錯誤攔截器,而不是struts默認攔截器 下面提供的是那如何在struts2中配置自定義轉換錯誤攔截器

public class MyConversionErrorInterceptor extends ConversionErrorInterceptor { 

private static final long serialVersionUID = 1L; 

protected Object getOverrideExpr(ActionInvocation invocation, Object value) { 
    ValueStack stack = invocation.getStack(); 
    return (String)stack.findValue("myproj.item"); 
} 
protected boolean shouldAddError(String propertyName, Object value) { 

    return true; 
}} 

這裏是提到的struts.xml配置。

<interceptors > 

<interceptor name="conversionError" class="com.celtic.cmvs.webapp.interceptor.MyConversionErrorInterceptor" /> 

<interceptor-stack name="myDefaultStack"> 
    <interceptor-ref name="conversionError" /> 
    <interceptor-ref name="defaultStack"/> 
</interceptor-stack> 

但它不工作。 在此先感謝

回答

2

我看到一些可能性。

  1. struts2 conversionError攔截器仍然會被調用。如果你想要你的而不是標準的,你需要自己定義整個堆棧,用你的攔截器代替標準的。
  2. 您可能需要更改自定義攔截器的配置名稱,以使其與標準攔截器不同。否則,interceptor-ref name =「conversionError」可能仍然指向標準攔截器。
  3. 您需要確保使用新堆棧。您可以通過將其聲明爲默認堆棧或通過在特定操作中引用自定義堆棧來實現。

我在我的博客自定義的攔截,可能是有用的教程:http://ddubbya.blogspot.com/2011/01/creating-custom-struts2-interceptors.html

+0

感謝那些幫助了很多:) – Vipin 2011-02-14 07:46:36