如何在Play框架2.0(Java)中重定向到外部URL

9

通过控制器中的redirect()方法似乎可以将页面重定向到内部URL。

public static Result index() {
  return redirect(routes.Application.tasks());
}

然而,我希望在控制器中重定向到外部URL。redirect(String)方法只接受内部URL作为参数。

我需要Play框架等同于标准Java servlet重定向的方法:

request.sendRedirect(String url)
1个回答

20
有时候,最简单的解决方案就是有效的:
return redirect("https://dev59.com/nmTWa4cB1Zd3GeqPGsni");

值得一提的是,您也可以使用其他可用的重定向方法,例如:

  • seeOther(String url)
  • movedPermanently(String url)
  • temporaryRedirect(String url)

等等。


嘿,马库斯,谢谢你的回答。你提到的其他三种方法对我来说都很好用。然而,这个简单的解决方案 redirect(String) 给了我这个错误 - Execution exception [StackOverflowError: null] - Manish Singh
@ManishKumar 呃呃呃...这是方法的声明...这意味着您需要在其中插入一些字符串,例如 String url="http://domain.tld"; return seeOther(url); 或者 return seeOther("http://domain.tld"); - biesior
Play框架真的需要停止添加新功能,开始着手文档编写。 - bharal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接