如何使用groovy代码获取url中的数据?

时间:2016-03-15 05:46:09

标签: groovy rtc tasktop

我想使用groovy代码从url获取缺陷id(在tasktop中构建自定义代码)。

例如:我将生成一个动态网址,例如www.xyz.com/abc/defect_123/现在我想要检索始终从第17个位置开始的那个字母。并返回字符串

请帮忙.. 提前致谢

2 个答案:

答案 0 :(得分:0)

这有两种可能性。请注意"子串"选项非常严格,总是从第16个位置开始(如果域名从www.xyz.com更改为www.xyzw.com,会发生什么?)

def str = 'www.xyz.com/abc/defect_123/';

def pieces = str.tokenize('/'); // prints defect_123
def from16 = str.substring(16); // prints defect_123/

println from16;
println pieces.last();

答案 1 :(得分:0)

您应该在 UrlMappings.groovy 文件中将其定义为动态网址:

"www.xyz.com/abc/$defect_id" (controller: 'YourController', action: 'method_name')

您可以使用defect_id

YourController 访问params.defect_id变量