6

是否有一个 groovy/grails 等效于 PHP 的 parse_url ( http://php.net/manual/en/function.parse-url.php ) 或 python 的 urlparse ( http://docs.python.org/library/urlparse.html ) 将 URL 字符串转换为包含主机、协议、查询字符串、片段、URI 等的结构?

我以为它可能在 grails.org/doc/latest/api/org/codehaus/groovy/grails/web/util/WebUtils.html 中,但什么也没看到。我不认为 HTTPBuilder 或各种 URLMapping 实用程序是我需要的。

我真的只想从路径和 queryString 中拉出一张地图,并正确处理边缘情况(参数数组 /blah/fuzz?foo=bar&foo=baz、片段/blah/fuzz?foo=bar#baz、用于重定向的编码 URL)。

我知道我可以通过巧妙地使用 URLMapping 来处理 PATH 组件,例如:/blah/$code,但我留下了解码参数块......

谢谢

4

2 回答 2

10

如果我理解正确,您真正需要的是普通的旧URI类:

new URI('http://google.com/?q=URL').query
于 2011-07-13T22:11:43.080 回答
2

扩展@Artur Nowak 的答案,也许你需要更多的努力才能得到你想要的。这是一个例子:

URI dbUri = new URI('http://google.com/?q=URL')
def username = dbUri?.getUserInfo()?.split(":")?.getAt(0)
def password = dbUri?.getUserInfo()?.split(":")?.getAt(1)
def host = dbUri?.getHost()
def databaseInstance = dbUri?.getPath()
def url = "jdbc:mysql://" + host + databaseInstance
于 2015-03-12T02:00:26.773 回答