gpt4 book ai didi

java - Play Framework : Redirect to controller method with arguments

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:59:00 25 4
gpt4 key购买 nike

我正在使用 PLAY 框架 2.2.1 构建一个 Web 应用程序,并试图在地址栏中显示所请求站点的所有可用 http get 查询参数,即使是那些未在请求中设置的参数。在这种情况下并非所有 http get 参数都已设置,我想添加具有默认值的未设置参数并进行重定向。

我有一个可以使用 GET 请求的站点:

GET /test controllers.Application.test(q:String, w:String ?= null, f:String ?= null, o:String ?= null)

这是我希望在 controllers.Application 中使用的方法:

public static Result test(String q, String w, String f, String o){

...

// In case not all parameters where set
if (reload == 1){
return redirect(controllers.Application.test(qDefault, wDefault, fDefault, oDefault));
}
else {
ok(...);
}
}

问题是 redirect() 接受一个 String 而不是 Result 对象。

我的第一个解决方案是写

return controllers.Application.test(qDefault, wDefault, fDefault, oDefault);

但不幸的是地址栏没有更新。

我的第二个解决方案是手动构建字符串:

return redirect("/test?q=" + query + "&f=" + f + "&w=" + w + "&o=" + showOptions);

这很好用,但是没有其他更优雅的方式来做到这一点吗?

最佳答案

使用routes对象:

public static Result index() {
return redirect(controllers.routes.Application.test(qDefault, wDefault, fDefault, oDefault));
}

来源:Official Documentation

关于java - Play Framework : Redirect to controller method with arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21233156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com