Action Events

Turbine has a very useful feature that makes handling form submission much more painless for the developer. In order to understand this, you need to be familiar with the way that Turbine handles Actions. What happens is that when a URI has the action= variable defined, a class is executed before all of your other Screen classes by your Page class. So, consider the following URI (I'm using the VelocitySite Howto example):

Turbine には、フォームの実行の扱いをさらにずっと簡単にする、 開発者にとってとても便利な機能があります。 これを理解するには、Turbine がアクションを扱う方法について、 よく知らなければなりません。 URI に action= 変数が定義されたら何が起こるか説明すると、 あなたのページクラスによって、他の全てのスクリーンクラスよりも、 ある一つのクラスが実行されます。 それでは、次の URI ( VerocitySite Howto を例にとります) を考えてみましょう:

http://www.server.com/servlet/Turbine/template/AddUser/action/NewUser What happens is that Turbine will first execute the Java class file Action named NewUser. Then, any class that extends the ActionEvent class instead of the Action class will be able to take advantage of what happens next...

http://www.server.com/servlet/Turbine/template/AddUser/action/NewUser これで起こる事は、Turbine はまず最初に NewUser という名前のアクションの Java クラスファイルを実行します。そして、 Action クラスのかわりに ActionEvent クラスを拡張したクラスが次に起こる事の担い手となります...

public class NewUser extends VelocityAction
{
    public void doAdd (RunData data, Context context) throws Exception
    {
        // put code here to add the user to the system
        // システムにユーザを加えるために、ここにコードを書きます
        context.put ("username", username );
        data.setMessage("User Added!");
    }

    public void doPerform(RunData data, Context context) throws Exception
    {
        data.setMessage("Button not found!");
    }
}

Then, write your HTML tags specially like this:

そして、特別に次のように HTML タグを書きます:

<input type="submit" name="eventSubmit_doAdd" value="Add User">

When your Action is executed, an "event" is sent to it by attempting to execute a "doAdd()" method in your Action. The cool thing about this is that each of your "actions" that are performed within your Action class now are componentized into a single method that can be javadoc'ed individually.

あなたのアクションが実行されたら、あなたのアクション中の "doAdd()" メソッドを実行しようという試みにより、"event" がアクションに送られます。 ここでよくできているのは、 あなたのアクションクラス中で行われるあなたの個々 "action" は、 一つのメソッドにコンポーネント化されており、 個々に Javadoc とすることができるのです。

This new functionality does not mean that you should write all of your actions in one single class, what it means is that if you have a screen with many buttons on it that are very specific to that screen, it might be a good idea to place all those methods into a single class. This allows you to do it easily and also prevents you from having to do a "if then elseif" tree to figure out which button was clicked.

この新しい機能の意味するところは、あなたが一つのクラスの中に全てのアクションを記述しなければならないという事ではありません。 その意味は、スクリーンにたくさんのボタンがあって、 それらがこのスクリーンに固有のものであるならば、 これらメソッドを一つのクラスに入れてしまうのは良い考えかもしれません。 この機能により、実装が簡単になり、どのボタンがクリックされたかを処理するために "if then elseif" ツリーを書く必要が無くなるのです。

For a catchall, the doPerform() method will be executed if no other method or button could be found.

とりまとめとして、他のメソッドやボタンが見つからない場合には、 doPerform() メソッドが実行されます。

Because ParameterParser makes all the key values lowercase, we have to do some work to format the string into a method name. For example, a button name eventSubmit_doDelete gets converted into eventsubmit_dodelete. Thus, we need to form some sort of naming convention so that dodelete can be turned into doDelete.

ParameterParser は全てのキーの値を小文字にするので、 文字列をメソッド名にする作業をしなければなりません。 例えば、eventSubmit_doDelete というボタン名は eventsubmit_dodelete に変換されます。 ですから、dodelete を doDelete に変更するといった、 名前の変更が必要になります。

Thus, the convention is this:
従って、変更は次のようになります:

  • The variable name MUST have the prefix "eventSubmit_".
  • 変数名は "eventSubmit_" の接頭辞を持たなければならない
  • The variable name after the prefix MUST begin with the letters "do".
  • 接頭辞の後の変数名は "do" で始らなければならない
  • The first letter after the "do" will be capitalized and the rest will be lowercase
  • "do" の次の最初の文字は大文字とし、のこりは小文字とする

If you follow these conventions, then you should be ok with your method naming in your Action class.

上述の規則に従えば、アクションクラス中のメソッド名の付け方は大丈夫です。