在play中没有Servlet API forward 的等价物。每一个HTTP request只能调用一个action。如果我们需要调用另一个,必须通过重定向,让浏览器访问另一个URL来访问它。这样的话,浏览器的URL始终与被执行的action保持一致,实现 Back/Forward/Refresh 的管理就容易多了。

你可以发送到任何一个action的Redirect,只需要直接在Java中调用该action即可。该调用将会自动被Play拦截,并生成一个HTTP重定向。

举例:

public class Clients extends Controller {  
 
public static void show(Long id) { 
Client client = Client.findById(id);
render(client);
}
public static void create(String name) {
Client client = new Client(name);
client.save();
show(client.id);  //调用上面的show(Long id)方法
}
}  

With these routes:

GET /clients/{id} Clients.show  
POST /clients Clients.create  
  • 浏览器向 /clients URL发送一个POST。
  • Router调用 Clients controller的 create action.
  • Action方法直接调用 show 方法
  • 该Java调用被拦截,Router根据它产生一个调用Clients.show(id)所需要的新URL。
  • HTTP响应为 302 Location:/clients/3132.
  • 浏览器接着发送 GET /clients/3132.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-25
  • 2021-11-07
猜你喜欢
  • 2022-03-06
  • 2021-12-18
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2021-12-12
  • 2021-09-18
相关资源
相似解决方案