Java 如何在playFramework中重用控制器类的功能
假设我在控制器类中有两个函数:Java 如何在playFramework中重用控制器类的功能,java,playframework,playframework-2.2,Java,Playframework,Playframework 2.2,假设我在控制器类中有两个函数: public class Application extends Controller { public static Result method1() { String results; //step1 //step2 //step3 ... return ok(results); } public static Result method2() { String
public class Application extends Controller {
public static Result method1() {
String results;
//step1
//step2
//step3
...
return ok(results);
}
public static Result method2() {
String results;
//step1
//step2
//step3
//step4
return ok(results);
}
}
在这两种方法中,从1到3的步骤在两个函数中完全相同
是否在method2()
中使用method1()
,即在method2()
中调用method1()
注意:
我知道我可以创建一个执行步骤1到步骤3的方法,并在两个控制器的方法中使用该方法,但我不想这样做,因为它会增加复杂性。我认为,“从
method2
调用method1
”将为您带来更大的复杂性,考虑到它们的返回值,而不是通过步骤1到3创建私有方法。因为你需要在method2
中解析Result
以首先提取这些相同的值,然后再执行步骤4(如果method1或Result
中除了ok()
?)之外的其他Result
中存在异常,该怎么办?。@M.Prokhorov你可能是对的,事实上,我不知道这样做有多复杂。然而,如果我们使用playframework作为只返回的服务api,那么这种方法可能会显著降低复杂性