4

我的应用程序由几个部分组成。我想通过在我的所有应用程序中更改 URL 来防止用户更改区域设置,除了一个小的 GWT 位置。我需要在 URL 中提供该位置的语言环境,以确保以正确的语言加载该位置。

我能做些什么?

我看到以下选项:

1) 从该位置创建单独的模块,并允许在该模块的 xml 设置文件中使用 queryparam 作为语言环境的来源。据我了解,我需要写下 <set-configuration-property name="locale.searchorder" value="queryparam,cookie"/> 可能会起作用的东西,但是对于这么小的任务来说有点困难。

2) 另一种选择是手动实现所需的功能。我写了以下代码:

String languageCode = Window.Location.getParameter("lang");
Cookies.setCookie(COOKIE_NAME, languageCode, new Date(System.currentTimeMillis() * 1000 * 3600 * 24 * 365 * 100));
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
    @Override
    public void execute()
    {
        Window.Location.reload();
    }
});

它可以工作,但问题在于延迟调用:如果我使用它,页面会加载,然后重新加载信号会在页面显示后开始重新加载。用户观察到奇怪的闪烁。如果我不使用延迟调用,cookie 没有设置,我不知道为什么(你能解释一下吗?)。

那么你将如何解决这个任务呢?

4

2 回答 2

5

我们避免在 GWT 代码中设置 cookie。相反,我们根据用户操作或设置在登录页面中设置 cookie,然后重定向到 GWT 应用程序。

第 1 步 - 登录 - JSP 页面设置 cookie 并进行身份验证。

第 2 步 - 成功时将 URL 指向托管 GWT 应用程序的 html 文件。

第 3 步 - GWT 只需通过模块 xml 文件读取 cookie 信息。

在您的 .gwt.xml 文件中提供 Login jsp 正在设置的 cookie。

<set-configuration-property name="locale.cookie" value="GWT_LOCALE" />

也参考

1) GWT i18n,更改 metaTag 并重新加载应用程序

2) https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nLocale

3) http://learninggwt.blogspot.in/2011/07/gwt-internationalization-and-cookies.html

于 2013-08-22T14:41:42.630 回答
0

上面的答案是正确的,但我想添加有关特定案例的更多信息。

当您使用 Spring-security 并且需要在登录后设置语言环境时(我认为这是因为问题标题中的“onLoad”字样),您可以实现 AuthenticationSuccessHandler。

即在 MyAuthenticationSuccessService.java

@Override
  public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
      Authentication authentication) throws IOException, ServletException {
    String userName = authentication.getName();
    // ... retrieve user info
    String locale = userInfo.getLocale();

    response.sendRedirect("/MyGwtModule.html?locale="+locale+"#HomePlace:");
  }

在 Spring 配置中

<form-login login-page="/login" authentication-failure-url="/loginfailed"
            authentication-success-handler-ref="myAuthenticationSuccessService" />
于 2013-09-09T10:12:52.353 回答