如何在Java Play框架中获取查询字符串参数? [英] How to get query string parameters in java play framework?
问题描述
我对Java Play框架很陌生.我已经设置了所有普通的路由,例如/something/:somthingValue以及所有其他路由.现在,我想创建route接受
I'm very new to java play framework. I've set up all the normal routes like /something/:somthingValue and all the others. Now I want to create route the accepts query parameters like
/something?x = 10& y = 20& z = 30
/something?x=10&y=20&z=30
我想在?"之后获取所有参数.作为键==>值对.
Here I want to get all the params after "?" as key==>value pair.
推荐答案
您可以将查询参数连接到路由文件中:
You can wire in your query parameters into the routes file:
http://www.playframework.com/documentation/2.0.4/JavaRouting 在具有默认值的参数"部分中
http://www.playframework.com/documentation/2.0.4/JavaRouting in section "Parameters with default values"
或者您可以在操作"中要求他们:
Or you can ask for them in your Action:
public class Application extends Controller {
public static Result index() {
final Set<Map.Entry<String,String[]>> entries = request().queryString().entrySet();
for (Map.Entry<String,String[]> entry : entries) {
final String key = entry.getKey();
final String value = Arrays.toString(entry.getValue());
Logger.debug(key + " " + value);
}
Logger.debug(request().getQueryString("a"));
Logger.debug(request().getQueryString("b"));
Logger.debug(request().getQueryString("c"));
return ok(index.render("Your new application is ready."));
}
}
例如http://localhost:9000/?a=1&b=2&c=3&c=4
在控制台上打印:
[debug] application - a [1]
[debug] application - b [2]
[debug] application - c [3, 4]
[debug] application - 1
[debug] application - 2
[debug] application - 3
请注意,c
在网址中是两次.
Note that c
is two times in the url.
这篇关于如何在Java Play框架中获取查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!