我的申请包含几个部分。我想通过更改我的所有应用程序中的URL来保护用户不要更改区域设置,除了一个小的GWT位置。我需要在URL中为该位置提供区域设置,以确保以正确的语言加载位置。
我该怎么办?
我看到以下选项:
1)从该位置创建单独的模块,并允许使用queryparam作为该模块的xml设置文件中的区域设置源。据我所知,我需要放下像<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就没有设置,我不知道为什么(你可以解释一下吗?)。
那你怎么解决这个任务呢?
答案 0 :(得分:4)
我们避免在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, change metaTag and reload application
2)https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nLocale
3)http://learninggwt.blogspot.in/2011/07/gwt-internationalization-and-cookies.html
答案 1 :(得分: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 config中
<form-login login-page="/login" authentication-failure-url="/loginfailed"
authentication-success-handler-ref="myAuthenticationSuccessService" />