java 查询字符串中 Hash(#) 的行为是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29841137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:00:13  来源:igfitidea点击:

What is the behavior of Hash(#) in query string

javaurlweb-applicationsurl-encoding

提问by Shekhar Khairnar

I am sending the below url with query string. In the query string one parameter "approverCmt" has value with hash(#).

我正在发送带有查询字符串的以下网址。在查询字符串中,一个参数“approverCmt”的值为 hash(#)。

    "/abc/efd/xyz.jas?approverCmt=Transaction Log #459505&batchNm=XS_10APR2015_082224&mfrNm=Timberland"

In server side when I tried to retrieve it from the request I get

在服务器端,当我试图从我得到的请求中检索它时

    approverCmt = Transaction Log  -----> "#459505" is missing
    batchNm = null
    mfrNm = null

And If I remove hash(#) from query string or If I replace # with %23 every thing works fine

如果我从查询字符串中删除 hash(#) 或者如果我用 %23 替换 # 一切正常

I don't understand why I am getting null for one parameter if another parameter contains a hash(#) symbol.

如果另一个参数包含哈希(#)符号,我不明白为什么我会为一个参数获取空值。

Appreciate if any one can explain.

不胜感激,如果有人可以解释。

回答by Prashant

This is known as the "fragment identifier".

这被称为“片段标识符”。

As mentioned in wikipedia:

正如维基百科中提到的:

The fragment identifierintroduced by a hash mark # is the optional last part of a URLfor a document. It is typically used to identify a portion of that document.

fragment identifier由井号#介绍的是一个可选的最后一部分URL为一个文件。它通常用于标识该文档的一部分。

The part after the #is info for the client. It is not sent to the server. Put everything only the browser needs here.

之后的部分#是 的信息client。它不会发送到服务器。把浏览器需要的所有东西都放在这里。

You can use the encodeURIComponent()function in JavaScript to encode special characters in a URL, so that #characters are converted to other characters that way you can be sure your whole URL will be sent to the server.

您可以使用encodeURIComponent()JavaScript 中的函数对 URL 中的特殊字符进行编码,以便将#字符转换为其他字符,这样您就可以确保将整个 URL 发送到服务器。

回答by pdem

The Hash value is for the anchor, so it is only client-side, it is often used in client-side framework like angular for client-side routing.

Hash 值是针对锚点的,所以它只是客户端,它经常用于客户端框架中,例如用于客户端路由的 angular。

The anchor is NOT available server-side.

锚点在服务器端不可用。

In your case you don't need an anchor, but a parameter value with a # break the query string the value is "Transaction Log #459505".

在您的情况下,您不需要锚点,但需要带有#break 查询字符串的参数值,该值是“事务日志#459505”。

EDIT Naive solution that doesn't work, just let it ther for history, See Real solution below

编辑不起作用的天真解决方案,让它成为历史,请参阅下面的真实解决方案

The solution is to encode client-side and decode serveur-side

解决方案是对客户端进行编码并在服务端进行解码

Encoding in javascript

在javascript中编码

   encodeURI("Transaction Log #459505")
   //result value "Transaction%20Log%20#459505"

Decode in Java

用Java解码

 java.net.URLDecoder.decode("Transaction%20Log%20#459505");
  //result  "Transaction Log #459505"

EDIT: But: Javascript doesn't encode in the same way than Java So the correct answer (I hope) is to manually replace all your # with %23, then Java will decode it normally, or to use encodeURIComponent as suggested in comments. For your need the replace solution seem to be enough.

编辑:但是:Javascript 的编码方式与 Java 不同所以正确的答案(我希望)是手动将所有 # 替换为 %23,然后 Java 将对其进行正常解码,或者按照注释中的建议使用 encodeURIComponent。对于您的需要,更换解决方案似乎就足够了。

Encode in Javascript:

在 Javascript 中编码:

encodeURI("yourUrl/Transaction Log #459505").replace(/#/,"%23")
//result: yourUrl/Transaction%20Log%20%23459505

The decode in Java doesn't change

Java 中的解码不会改变

java.net.URLDecoder.decode("Transaction%20Log%20#459505")
// result (java.lang.String) Transaction Log #459505

Sorry for long post, I didn't see the difference bettween Java and the JavaScrip Url encoding

抱歉长篇幅,我没有看到 Java 和 JavaScrip Url 编码之间的区别

回答by Jens

the hash is an anchor:

哈希是一个锚点:

see wikipediafor more information

有关更多信息,请参阅维基百科