The Jakarta Project < Velocity Tools - Struts >

Velocity Tools

VelocityStruts

VelocityStruts Tools

その他のサブプロジェクト

FormTool リファレンスドキュメント
       

Struts has support to parse incoming HTTP requests and populate a Java bean with the submitted request parameters. The same Java bean is used to populate forms with initial values. Additionally, a hook allows the application developer to include automatic form validation code.

Struts は HTTP リクエストの解析と、 サブミットされたリクエストパラメータのJava ビーンへの設定を行います。 このJava ビーンは、初期値を含むフォームの値の設定にも利用されます。 また、自動的なフォームの検証コードをフックさせることもできます。

FormTool provides miscellaneous methods to work with forms and form bean in the context of Struts applications.

FormTool はフォームや、 Struts アプリケーションのコンテキスト内のフォームビーンを扱うさまざまなメソッドを提供します。

Class
 org.apache.velocity.tools.struts.FormTool
Name
 $form (Velocityコンテキスト内での推奨名)
Toolbox設定例
 
<tool>
  <key>form</key>
  <scope>request</scope>
  <class>org.apache.velocity.tools.struts.FormTool</class>
</tool>
作者
 Gabriel Sidler
Method Overview
getBean() Retrieve and return the form bean associated with this request.
getCancelName() Returns the query parameter name under which a cancel button press must be reported if form validation is to be skipped.
getToken() Retrieves and returns the transaction control token for this session.
getTokenName() Returns the query parameter name under which a transaction token must be reported.
Method Overview
getBean() リクエストに関連するフォームビーンを取得し、返却します。
getCancelName() キャンセルボタンが押下された場合の、 フォームの自動バリデーションのスキップを通知するクエリパラメータの名称を返却します。
getToken() 現在のセッションのトランザクション制御トークンを取得し、返却します。
getTokenName() トランザクショントークンを通知するクエリパラメータの名称を返却します。
getBean()
       

Retrieve and return the form bean associated with this request.

ActionForm getBean()

Returns
The ActionForm associated with this request/session or null if there is no form bean associated with this mapping.

This is a convenience method. The form bean is automatically available in the Velocity context under the name defined in the Struts configuration.

If the form bean is used repeatedly, it is recommended to create a local variable referencing the bean rather than calling getBean() multiple times.

## Populating an input field with a default value
<input type="text" name="username" value="$form.getBean().username">

## The same can be written as
<input type="text" name="username" value="$form.bean.username">

## For repeated use create a local reference
#set ($defaults = $form.bean) 
<input type="text" name="username" value="$defaults.username">

## Accessing the form using the form name defined in struts-config.xml
## Velocity searches the request and session attributes for loginForm
<input type="text" name="username" value="$loginForm.username">

リクエストに関連するフォームビーンを取得し、返却します。

ActionForm getBean()

Returns
request/sessionに関連する ActionForm 、 関連するフォームビーンがマッピングに無い場合は null

これは簡易メソッドです。 フォームビーンは、Velocity コンテキストから Struts の設定で定義した名称で自動的に利用できます。

フォームビーンを繰り返し使用する場合、 毎回 getBean() を呼び出すより、ローカル変数を作成して参照すると良いでしょう。

## インプットフィールドにデフォルト値を設定し、値を取得します
<input type="text" name="username" value="$form.getBean().username">

## こう書いても上と同じです
<input type="text" name="username" value="$form.bean.username">

## 繰り返し参照する場合はローカル変数として定義します
#set ($defaults = $form.bean) 
<input type="text" name="username" value="$defaults.username">

## struts-config.xml で定義されたフォームの名前を使用してフォームへアクセスします
## Velocity は loginForm を request や session の属性(attribute)から検索します
<input type="text" name="username" value="$loginForm.username">

getCancelName()
       

Returns the query parameter name under which a cancel button press must be reported if form validation is to be skipped.

int getCancelName()

Returns
The value of org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY

If a request contains a request parameter with key equal to the return value of this method, then automatic form validation is skipped. A typical application case is to use this magic string for the name attribute of a cancel button in a form. If this button is pressed, automatic form validation is skipped. If automatic form validation is not used, this magic string is irrelevant.

## A cancel button
<input type="submit" name="$form.getCancelName()" value="Cancel">

## Can also be written as
<input type="submit" name="$form.cancelName" value="Cancel">

This produces the following output:

<input type="submit" name="org.apache.struts.taglib.html.CANCEL"
value="Cancel">

キャンセルボタンが押下された場合の、 フォームの自動バリデーションのスキップを通知するクエリパラメータの名称を返却します。

int getCancelName()

Returns
org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY の値。

リクエストパラメータのキーにこのメソッドの返却値と同じ値が含まれていた場合、フォームの自動バリデーションがスキップされます。 一般的なアプリケーションでは、フォームのキャンセルボタンの name 属性でこの「おまじない」を使います。 このボタンが押下された場合、フォームの自動バリデーションがスキップされます。 もしフォームの自動バリデーションを行っていない場合、この「おまじない」は何の意味もありません。

## キャンセルボタン
<input type="submit" name="$form.getCancelName()" value="Cancel">

## これはこのようにも書けます
<input type="submit" name="$form.cancelName" value="Cancel">

これは以下の出力を生成します:

<input type="submit" name="org.apache.struts.taglib.html.CANCEL"
value="Cancel">

getToken()
       

Retrieves and returns the transaction control token for this session.

String getToken()

Returns
The token String or null if no token exists.
See Also
Method getTokenName() for examples.

現在のセッションのトランザクション制御トークンを取得し、返却します。

String getToken()

Returns
トークン String 、またはトークンが存在しない場合 null
See Also
getTokenName() メソッドに例があります。

getTokenName()
       

Returns the query parameter name under which a transaction token must be reported.

int getTokenName()

Returns
The value of org.apache.struts.taglib.html.Constants.TOKEN_KEY
See Also
JavaDoc of class org.apache.struts.action.Action for more information on the transaction token mechanism (I don't know of any better documentation of this Struts feature).
## A hidden form field with the transaction token
<input type="hidden" name="$form.getTokenName()"
value="$form.getToken()">

## Can also be written as
<input type="hidden" name="$form.tokenName"
value="$form.token">

This produces output similar to:

<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" 
value="84c29b4dea56ecf69524ef6b965c5e80">

トランザクショントークンを通知するクエリパラメータの名称を返却します。

int getTokenName()

Returns
org.apache.struts.taglib.html.Constants.TOKEN_KEY の値。
See Also
トランザクショントークンの仕組みの詳細については org.apache.struts.action.Action クラスの JavaDoc を参照してください (この Struts の機能のドキュメントとして、これより良いものを私は知りません)。
## トランザクショントークンの hidden フィールド
<input type="hidden" name="$form.getTokenName()"
value="$form.getToken()">

## これはこのようにも書けます
<input type="hidden" name="$form.tokenName()"
value="$form.token()">

このような出力が生成されます:

<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" 
value="84c29b4dea56ecf69524ef6b965c5e80">


[訳注: この文書は 木村 貴由 高橋 達男 が翻訳しました。 日本語訳に対するコメントがあれば、report@jajakarta.orgに送って下さい。]
Copyright © 1999-2003, Apache Software Foundation