播放框架:使用参数重定向到控制器方法 [英] Play Framework: Redirect to controller method with arguments

查看:75
本文介绍了播放框架:使用参数重定向到控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a web application with PLAY framework 2.2.1 and am trying to display all available http get query parameters for the requested site in the address bar, even the ones that are not set in the request. In cases where not all http get parameters are set, I want to add the unset parameters with the default values and make a redirect.

我有一个可以通过GET请求的网站:

I have a site that can be requested with GET:

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

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

Here is the method that I'd like to have in 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对象.

The problem is that redirect() takes a String and not a Result object.

我的第一个解决方案是写

My first solution is to write

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

但不幸的是,地址栏不会更新.

But unfortunately the adress bar does not update.

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

My second solution is to build the string manually:

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

这很好用,但是没有别的更优雅的方法了吗?

This works fine, but is there no other way more elegant way to do this?

推荐答案

使用routes对象:

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

来源:官方文档

这篇关于播放框架:使用参数重定向到控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆