Play Framework:使用参数重定向到控制器方法
我正在使用PLAY framework 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)); }
资料来源: 官方文件
另外这也是有效的
public static Result index() { return redirect("path"); }