在 GWT Activitie 的位置令牌的令牌中对名称/值对进行编码的建议方法

发布于 2024-12-02 11:08:28 字数 526 浏览 2 评论 0原文

在使用 History/Activities/Places api 的 GWT 中,您最终会得到如下 URL

http://192.168.0.104:8888/brokerage.html?gwt.codesvr=192.168.0.104:9997#StartPlace:params

其中单词 params 是将“令牌”放置在可以将参数传递到 StartPlace 对象中的位置。对于我的大多数应用程序,单个字符串足以通过按下刷新来重新加载我的数据。然而,在一些活动/地点/页面上,我需要将该地点令牌拆分为多个名称/值对。

有人对如何处理这个问题有建议吗?我目前正在编写一个类,该类通过使用 &name=value 以 url 方式分隔名称/值对来构建字符串。如果有人知道一个可以处理这个或其他事情的类,那就太好了。

In GWT using the History/Activities/Places apis you end up with urls like this

http://192.168.0.104:8888/brokerage.html?gwt.codesvr=192.168.0.104:9997#StartPlace:params

Where the word params is the place "token" where parameters can be passed into the StartPlace object. For most of my applications a single string is enough to reload my data with refreshed is pressed. On a few Activities/Places/Pages however I need to split that Place token into a number of name/value pairs.

Does anybody have a suggestion for how to handle this? I am currently writing a class that builds a string out of name/value pairs by separating them in a url fashion using &name=value. It would be great if somebody knew of a class that could handle this or something.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停顿的约定 2024-12-09 11:08:28

如何简单地从 Window.Location 复制/粘贴代码进行解析(私有方法 buildListParamMap;您也可以使用 JSNI 来调用它——它允许绕过 Java 可见性——假设它是一个静态方法,没有状态),以及用于序列化的UrlBuilder.buildString

How about simply copy/pasting the code from Window.Location for the parsing (private method buildListParamMap; you can also call it using JSNI –which allows bypassing Java visibility– given that it's a static method with no state), and UrlBuilder.buildString for the serialization?

被你宠の有点坏 2024-12-09 11:08:28
String parameters="name1=value1&name2=value2&name3=value3";

HashMap<String, String> parameterMap = new HashMap<String, String>();

String[] parameterPairs = parameters.split("&");

for (int i = 0; i < parameterPairs.length; i++) {
    String[] nameAndValue = parameterPairs[i].split("=");
    parameterMap.put(nameAndValue[0], nameAndValue[1]);
}

....

String name1Value = parameterMap.get("name1");

这都是未经测试的代码,并且它具有未经检查的数组边界!确保没有多余的“&”或“=”符号,因为它们会搞乱解析。

String parameters="name1=value1&name2=value2&name3=value3";

HashMap<String, String> parameterMap = new HashMap<String, String>();

String[] parameterPairs = parameters.split("&");

for (int i = 0; i < parameterPairs.length; i++) {
    String[] nameAndValue = parameterPairs[i].split("=");
    parameterMap.put(nameAndValue[0], nameAndValue[1]);
}

....

String name1Value = parameterMap.get("name1");

This is all untested code, and it has unchecked array bounds! Make sure you don't have extraneous '&' or '=' signs, since they'll mess up the parsing.

写下不归期 2024-12-09 11:08:28

这是我想出的,它应该对其他人有用。使用哈希映射是一个好主意,我将修改我的代码。

public static String parseNamedParam(String name, String token) {
    if (name == null || "".equals(name) || token == null || "".equals(token)) {
        return null;
    }
    final String[] strNameValuePairs = token.split("&");
    for (String entry : strNameValuePairs) {
        final String[] strNameValuePair = entry.split("=");
        final String decodedName = URL.decodeQueryString(strNameValuePair[0]);
        if (decodedName.equals(name)) {
            return URL.decodeQueryString(strNameValuePair[1]);
        }
    }
    return "";
}

public static String generateTokenString(List<TokenParam> tokenParams) {
    if (tokenParams == null || tokenParams.isEmpty()) {
        return "";
    }
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < tokenParams.size(); i++) {
        final TokenParam tokenParam = tokenParams.get(i);
        final String nameEncoded = URL.encodeQueryString(tokenParam.getName());
        final String valueEncoded = URL.encodeQueryString(tokenParam.getValue());
        if (i != 0) {
            sb.append("&");
        }
        sb.append(nameEncoded);
        sb.append("=");
        sb.append(valueEncoded);
    }
    return sb.toString();
}

This is what I came up with, it should work for others. Using a hash map is a good idea, I'll modify my code.

public static String parseNamedParam(String name, String token) {
    if (name == null || "".equals(name) || token == null || "".equals(token)) {
        return null;
    }
    final String[] strNameValuePairs = token.split("&");
    for (String entry : strNameValuePairs) {
        final String[] strNameValuePair = entry.split("=");
        final String decodedName = URL.decodeQueryString(strNameValuePair[0]);
        if (decodedName.equals(name)) {
            return URL.decodeQueryString(strNameValuePair[1]);
        }
    }
    return "";
}

And

public static String generateTokenString(List<TokenParam> tokenParams) {
    if (tokenParams == null || tokenParams.isEmpty()) {
        return "";
    }
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < tokenParams.size(); i++) {
        final TokenParam tokenParam = tokenParams.get(i);
        final String nameEncoded = URL.encodeQueryString(tokenParam.getName());
        final String valueEncoded = URL.encodeQueryString(tokenParam.getValue());
        if (i != 0) {
            sb.append("&");
        }
        sb.append(nameEncoded);
        sb.append("=");
        sb.append(valueEncoded);
    }
    return sb.toString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文