gwt:格式错误的URI序列

时间:2015-03-11 06:16:55

标签: java gwt

我正在使用gwt 2.5.1。我的项目要求很小。我们在项目中支持本地化。如果用户输入无效的语言环境(即..,而不是支持的语言),我们默认使用英语显示。因此,为了从url获取locale参数,我使用方法com.google.gwt.user.client.Window.Location.getParameterMap()。如果用户输入'%'特殊字符作为语言环境参数,getParameterMap()抛出异常,即:

com.google.gwt.core.client.JavaScriptException: (URIError) @com.google.gwt.http.client.URL::decodeQueryStringImpl(Ljava/lang/String;)([string: '%']): malformed URI sequence
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.http.client.URL.decodeQueryStringImpl(URL.java)
    at com.google.gwt.http.client.URL.decodeQueryString(URL.java:117)
    at com.google.gwt.user.client.Window$Location.buildListParamMap(Window.java:310)
    at com.google.gwt.user.client.Window$Location.ensureListParameterMap(Window.java:327)
    at com.google.gwt.user.client.Window$Location.getParameterMap(Window.java:230)
    at srdm.cloud.client.sso.WebUISession.checkSession(WebUISession.java:283)
    at srdm.cloud.client.WebUI.onModuleLoad(WebUI.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:406)
    at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Thread.java:744)

那么,现在我如何通过忽略特殊字符来从网址获取locale参数?

1 个答案:

答案 0 :(得分:0)

我建议您阅读并使用GWT Internationalizationi18n Messages进行GWT项目本地化。在module.xml文件中添加以下语言环境声明

<extend-property name="locale" values="en"/>
<extend-property name="locale" values="ja"/>
<!-- below is default -->
<set-property-fallback name="locale" value="ja"/>

(英语 zh ,日本 ja

如何获得客户的可用区域设置?如何获得当前的本地?如何保存客户端更喜欢区域设置?以下是用于初始化本地列表框的代码段。根据需要展开。

public static void initializeLocaleBox(final ListBox localeBox) {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    if (currentLocale.equals("default")) {
        currentLocale = "ja";
    }
    String[] localeNames = LocaleInfo.getAvailableLocaleNames();
    for (String localeName : localeNames) {
        if (!localeName.equals("default")) {
            String nativeName = LocaleInfo.getLocaleNativeDisplayName(localeName);
            localeBox.addItem(nativeName, localeName);
            if (localeName.equals(currentLocale)) {
                localeBox.setSelectedIndex(localeBox.getItemCount() - 1);
            }
        }
    }
    localeBox.addChangeHandler(new ChangeHandler() {
        public void onChange(final ChangeEvent event) {
            String localeName = localeBox.getValue(localeBox.getSelectedIndex());

            Date now = new Date();
            long sevenDays = now.getTime();
            // seven days
            sevenDays = sevenDays + (1000 * 60 * 60 * 24 * 7);
            now.setTime(sevenDays);
            Cookies.setCookie("locale", localeName, now);
            UrlBuilder builder = Location.createUrlBuilder().setParameter("locale", localeName);
            Window.Location.replace(builder.buildString());
        }
    });
}

检查客户的区域设置

public static boolean isLocaleJapan() {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    return currentLocale != null && currentLocale.equals("ja") ? true : false;
}
public static boolean isLocaleEnglish() {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    return currentLocale != null && currentLocale.equals("en") ? true : false;
}