【问题标题】:Play Framework: Redirect to controller method with argumentsPlay Framework:使用参数重定向到控制器方法
【发布时间】:2014-01-20 11:43:58
【问题描述】:

我正在使用 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);

这很好用,但没有其他更优雅的方法可以做到这一点吗?

【问题讨论】:

    标签: java playframework http-get playframework-2.2


    【解决方案1】:

    使用routes 对象:

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

    来源:Official Documentation

    【讨论】:

      【解决方案2】:

      另外这也是有效的

      public static Result index() {
          return redirect("path"); 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-23
        • 2016-10-08
        • 2018-05-19
        • 1970-01-01
        • 2013-06-17
        相关资源
        最近更新 更多