2015-05-27 63 views
1

我正在构建一个多语言应用程序,并且存储cookie中使用的最新语言的值。xpages SSJS:无法摆脱日志中的“com.ibm.xsp.acl.RedirectSignal”警告

当用户打开应用程序时,sessionScope变量未设置,代码将查找cookie值,如果不在正确的Locale中,则重新加载页面。

每当页面重新加载到正确的语言环境时,我都会收到“com.ibm.xsp.acl.RedirectSignal”警告,并且我会尽量避免它。

我的代码在我使用的应用程序中ApplicationLayout控制的beforeRenderResponse事件,看起来是这样的:

if(!sessionScope.lang) { //this only happens when the page is opened in browser for the first time 
    var lang = context.getUrlParameter("lang").toLowerCase(); 
    if(!!lang) { 
      sessionScope.lang = lang.toUpperCase(); 
      //set cookie for next time the site is opened by user 
      setCookie("lang", lang.toUpperCase(), 30); 
      context.setLocale(new Locale(lang.toLowerCase())); 
    } else { 
     //set language from cookie 
     var lang = getLangFromCookie(); 
     if(!!lang) { 
      sessionScope.lang = lang; 
      context.setLocale(new Locale(lang.toLowerCase())); 
     } else { 
      sessionScope.lang = Appconfig.defaultLang; 
      context.setLocale(new Locale(Appconfig.defaultLang.toLowerCase())); 
     } 
    } 

    //need to trpa the redirect error thrown here, as it is just a warning - avoid filling log with this 
    //importPackage(com.ibm.xsp.acl.RedirectSignal); 
    importPackage(com.ibm.xsp.acl.RedirectSignal); 

    try { 
     //reload the page so Locale value kicks in 
     context.reloadPage(); 
    } catch (RedirectSignal rs) { 
     //just ignore 
    } 
} 

即使我加了importPackage线,我仍然得到一个错误,当节省代码(这是在脚本库):

在行遇到“‘RS’” ......

我怎样才能使这项工作?

感谢:D

回答

1

在SSJS中,您没有在catch块中定义异常的类型/类。由于您对“异常”没有做任何处理,因此也无需导入RedirectSignal类。

try {  
    context.reloadPage(); 
} catch (redirectSignal) { 
    // Ignore redirect signal "exception" 
} 
+0

工程就像一个魅力,我没有看到日志中的重定向消息了。不知道为什么我收到了这条消息,但是从我读到的内容来看,它似乎可能是一个页面重定向到自己的时候。如果是这种情况,那么这是一个预期的行为,因为我确实在重新加载同一页面后,更改了区域设置,因此页面将以正确的语言重新加载。无法只使用浏览器的语言环境,我需要用户(或URL参数,甚至cookie)来选择他的语言环境,而不依赖于浏览器。 –

2

捕获是一个Throwable,而不是一个RedirectSignal。这里是我在我的handleException函数中使用的代码

try { 
    try { 
     if (t instanceof RedirectSignal) { 
      return; 
     } 
     if ("javax.faces.el.EvaluationException".equals(t.getClass().getName())) { 
      if (t.getCause() instanceof RedirectSignal) { 
       return; 
      } 
     } 
    } catch (Throwable e) { 
     // Error checking cause, skip 
    } 
    // Now log the error 
} catch (Throwable e) { 
    e.printStackTrace(); 
} 
+1

哈克,但我必须收藏这一个,保罗:-) –

+0

我怎么能在SSJS中做到这一点?我似乎无法使用Throwable t作为catch参数。 –

+1

我给的代码是Java - 对不起,我现在不写很多SSJS。从SSJS使用它是有问题的。 SSJS不会抛出Java Exception或Throwable。它是各种各样的Java类,如果您查看XPages OpenLog Logger的PhaseListener中的processUncaughtException方法,您会看到它。 catch块不需要指定Throwable,但我不确定printStackTrace()和“instanceof”是否可用或相关。 getClass()。getName()会工作。 Tommy的代码将用于对context.reloadPage()的特定调用。 –