0

Play フレームワーク 1.2.5 を使用しています。コントローラーから呼び出されるヘルパーメソッドを使用しています(コントローラー内にあります-モデルまたは他の場所に実際に属しているかどうかはわかりません)が、その戻り値の型は無効です。これにより、コントローラーがそのメソッドから戻り、呼び出し元のメソッド (ヘルパーが呼び出された場所) に戻らないと思います。int や boolean などの戻り値の型を渡す以外に、この問題を回避するためにできることはありますか?

public class TestController extends Controller {

public static controllerMethod() {
helperMethod();
}

public static void helperMethod() {
    //some code 
    }

}

ヘルパー メソッドをモデルに移動するか、単純に boolean/int を戻り値の型として渡すことができると思います。他の提案はありますか? ありがとう

4

3 に答える 3

1

では、controllerMethodいくつかのレンダリング メソッドを呼び出す必要があります。

public static controllerMethod() {
  helperMethod();
  render() // or ok() to return http code 200
}

helperMethodそしてあなたのプライベートを宣言してください。メソッドをプライベートにすると、通常のコントローラー メソッドのブラウザー リダイレクト動作が防止されます。

于 2013-04-30T20:28:53.967 に答える
1

You don't want any extra helper methods or variables in your Controller classes! Move it to a Model class (the model class does not even have to extend Model - just don't extend Controller).

Each call to a controller method is not like a normal Java method invocation. It does a lot of extra stuff and forces the browser to redirect to the method. It will never resume the code in your original method!

So code like this in a controller method (where computeSomething is a static method of the controller)

  ...
  computeSomething();
  renderText("this will never be shown")

The only time you want to call another method in the controller is if you want to do a redirect. A typical example is checking authentication and redirecting to a login page.

于 2013-04-30T20:29:46.330 に答える
1

@Util でヘルパー メソッドに注釈を付けることで、ブラウザーがアクセスすることなくヘルパー メソッドを使用することを選択できます。

public class TestController extends Controller {

public static controllerMethod() {
     helperMethod();
}

@Util
protected static String helperMethod() {
    //some code 
    }

}
于 2013-06-19T21:15:05.817 に答える