【问题标题】:gwt: malformed URI sequencegwt:格式错误的 URI 序列
【发布时间】:2015-05-12 20:12:57
【问题描述】:

我正在使用 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)

那么,现在如何通过忽略特殊字符从 url 中获取 locale 参数?

【问题讨论】:

    标签: java gwt


    【解决方案1】:

    我想建议您阅读并使用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"/>
    

    en 表示英语,ja 表示日本)

    如何获取客户可用的语言环境?如何获取当前本地?如何保存客户的首选语言环境?下面是用于初始化本地列表框的 sn-p。根据需要展开。

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 2018-04-24
      • 2011-12-09
      • 2012-02-22
      • 2019-08-10
      相关资源
      最近更新 更多