1. 带参数(这不是重定向!)
您可以Result
从其他方法返回而无需重定向:
public static Result someMethod(){
DynamicForm dynamicForm = form().bindFromRequest();
return otherMethod(dynamicForm);
}
public static Result otherMethod(DynamicForm dataFromPrevRequest) {
String someField = dataFromPrevRequest.get("some_field");
Logger.info("field from request is: " + someField);
return ok("Other method's Result");
}
2. 带缓存(可能是这些解决方案中 POST 数据的最佳替换)
您还可以将来自传入请求的数据存储到数据库,甚至“更便宜”到缓存,然后以其他方法获取它:
public static Result someMethod(){
DynamicForm dynamicForm = form().bindFromRequest();
Cache.set("df.from.original.request", dynamicForm, 60);
return redirect(routes.Application.otherMethod());
}
public static Result otherMethod() {
DynamicForm previousData = (DynamicForm) Cache.get("df.from.original.request");
if (previousData == null) {
return badRequest("No data received from previous request...");
}
// Use the data somehow...
String someData = previousData.get("someField");
// Clear cache entry, by setting null for 0 seconds
Cache.set("df.from.original.request", null, 0);
return ok("Previous field value was " + someData);
}
3. 作为普通的 GET
最后,您可以只创建带有所需参数的方法并将它们传递给第一个方法(接收请求)。
public static Result someMethod(){
DynamicForm df = form().bindFromRequest();
return redirect(routes.Application.otherMethod(df.get("action"), df.get("id")));
}
public static Result otherMethod(String action, Long id) {
return ok("The ID for " + action +" action was" + id);
}