Struts-2-struts-annotations-types

提供:Dev Guides
移動先:案内検索

Struts 2-注釈タイプ

Struts 2アプリケーションは、XMLおよびJavaプロパティ構成の代替としてJava 5アノテーションを使用できます。 これは、さまざまなカテゴリに関連する最も重要な注釈のリストです-

名前空間の注釈(アクションの注釈)

@Namespaceアノテーションを使用すると、ゼロ構成の規則に基づくのではなく、 Action クラスでアクションの名前空間を定義できます。

@Namespace("/content")
public class Employee extends ActionSupport{
  ...
}

結果注釈-(アクション注釈)

@Resultアノテーションを使用すると、XMLファイルではなく、Actionクラスでアクションの結果を定義できます。

@Result(name = "success", value = "/success.jsp")
public class Employee extends ActionSupport{
 ...
}

結果の注釈-(アクションの注釈)

@Resultsアノテーションは、アクションの結果セットを定義します。

@Results({
   @Result(name = "success", value = "/success.jsp"),
   @Result(name = "error", value = "/error.jsp")
})
public class Employee extends ActionSupport{
 ...
}

注釈の後-(インターセプター注釈)

@Afterアノテーションは、メインアクションメソッドと結果の実行後に呼び出す必要があるアクションメソッドをマークします。 戻り値は無視されます。

public class Employee extends ActionSupport{
   @After
   public void isValid() throws ValidationException {
     //validate model object, throw exception if failed
   }
   public String execute() {
     //perform secure action
      return SUCCESS;
   }
}

注釈の前-(インターセプター注釈)

@Beforeアノテーションは、メインアクションメソッドと結果が実行される前に呼び出す必要があるアクションメソッドをマークします。 戻り値は無視されます。

public class Employee extends ActionSupport{
   @Before
   public void isAuthorized() throws AuthenticationException {
     //authorize request, throw exception if failed
   }
   public String execute() {
     //perform secure action
      return SUCCESS;
   }
}

BeforeResultアノテーション-(インターセプターアノテーション)

@BeforeResultアノテーションは、結果の前に実行する必要があるアクションメソッドをマークします。 戻り値は無視されます。

public class Employee extends ActionSupport{
   @BeforeResult
   public void isValid() throws ValidationException {
   //validate model object, throw exception if failed
   }

   public String execute() {
     //perform action
      return SUCCESS;
   }
}

ConversionErrorFieldValidatorアノテーション-(検証アノテーション)

この検証注釈は、フィールドに変換エラーがあるかどうかをチェックし、存在する場合はそれらを適用します。

public class Employee extends ActionSupport{
   @ConversionErrorFieldValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true)
   public String getName() {
      return name;
   }
}

DateRangeFieldValidatorアノテーション-(検証アノテーション)

この検証アノテーションは、日付フィールドに指定された範囲内の値があることを確認します。

public class Employee extends ActionSupport{
   @DateRangeFieldValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true,
      min = "2005/01/01", max = "2005/12/31")
   public String getDOB() {
      return dob;
   }
}

DoubleRangeFieldValidatorアノテーション-(検証アノテーション)

この検証アノテーションは、doubleフィールドに指定された範囲内の値があることを確認します。 minもmaxも設定されていない場合、何も実行されません。

public class Employee extends ActionSupport{

   @DoubleRangeFieldValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true,
      minInclusive = "0.123", maxInclusive = "99.987")
   public String getIncome() {
      return income;
   }
}

EmailValidatorアノテーション-(検証アノテーション)

この検証注釈は、空でない文字列が含まれている場合、フィールドが有効な電子メールアドレスであることを確認します。

public class Employee extends ActionSupport{

   @EmailValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true)
   public String getEmail() {
      return email;
   }
}

ExpressionValidatorアノテーション-(検証アノテーション)

この非フィールドレベルバリデータは、指定された正規表現を検証します。

@ExpressionValidator(message = "Default message", key = "i18n.key",
shortCircuit = true, expression = "an OGNL expression" )

IntRangeFieldValidatorアノテーション-(検証アノテーション)

この検証アノテーションは、数値フィールドに指定された範囲内の値があることを確認します。 minもmaxも設定されていない場合、何も実行されません。

public class Employee extends ActionSupport{

   @IntRangeFieldValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true,
      min = "0", max = "42")
   public String getAge() {
      return age;
   }
}

RegexFieldValidatorアノテーション-(検証アノテーション)

この注釈は、正規表現を使用して文字列フィールドを検証します。

@RegexFieldValidator( key = "regex.field", expression = "yourregexp")

RequiredFieldValidatorアノテーション-(検証アノテーション)

この検証アノテーションは、フィールドがnullでないことを確認します。 アノテーションはメソッドレベルで適用する必要があります。

public class Employee extends ActionSupport{

   @RequiredFieldValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true)
   public String getAge() {
      return age;
   }
}

RequiredStringValidatorアノテーション-(検証アノテーション)

この検証アノテーションは、文字列フィールドが空ではないことを確認します(つまり、 長さが0以外のnull以外。

public class Employee extends ActionSupport{

   @RequiredStringValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true, trim = true)
   public String getName() {
      return name;
   }
}

StringLengthFieldValidatorアノテーション-(検証アノテーション)

このバリデータは、文字列フィールドの長さが正しいことを確認します。 フィールドが文字列であることを前提としています。 minLengthもmaxLengthも設定されていない場合、何も実行されません。

public class Employee extends ActionSupport{

   @StringLengthFieldValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true,
      trim = true, minLength = "5",  maxLength = "12")
   public String getName() {
      return name;
   }
}

UrlValidatorアノテーション-(検証アノテーション)

このバリデータは、フィールドが有効なURLであることを確認します。

public class Employee extends ActionSupport{

   @UrlValidator(message = "Default message",
      key = "i18n.key", shortCircuit = true)
   public String getURL() {
      return url;
   }
}

検証アノテーション-(検証アノテーション)

同じタイプの複数の注釈を使用する場合、これらの注釈は@Validations()注釈内にネストする必要があります。

public class Employee extends ActionSupport{

  @Validations(
      requiredFields =
         {@RequiredFieldValidator(type = ValidatorType.SIMPLE,
            fieldName = "customfield",
            message = "You must enter a value for field.")},
      requiredStrings =
         {@RequiredStringValidator(type = ValidatorType.SIMPLE,
         fieldName = "stringisrequired",
         message = "You must enter a value for string.")}
   )
   public String getName() {
      return name;
   }
}

CustomValidatorアノテーション-(検証アノテーション)

この注釈は、カスタム検証に使用できます。 ValidationParameterアノテーションを使用して、追加のパラメーターを提供します。

@CustomValidator(type ="customValidatorName", fieldName = "myField")

変換アノテーション-(タイプ変換アノテーション)

これは、タイプレベルでのタイプ変換のマーカーアノテーションです。 変換アノテーションは、タイプレベルで適用する必要があります。

@Conversion()
   public class ConversionAction implements Action {
}

CreateIfNullアノテーション-(タイプ変換アノテーション)

この注釈は、型変換のためにCreateIfNullを設定します。 CreateIfNullアノテーションは、フィールドレベルまたはメソッドレベルで適用する必要があります。

@CreateIfNull( value = true )
private List<User> users;

要素注釈-(型変換注釈)

この注釈は、タイプ変換の要素を設定します。 要素注釈は、フィールドまたはメソッドレベルで適用する必要があります。

@Element( value = com.acme.User )
private List<User> userList;

キー注釈-(型変換注釈)

この注釈は、型変換のキーを設定します。 キーアノテーションは、フィールドレベルまたはメソッドレベルで適用する必要があります。

@Key( value = java.lang.Long.class )
private Map<Long, User> userMap;

KeyPropertyアノテーション-(タイプ変換アノテーション)

この注釈は、型変換のためのKeyPropertyを設定します。 KeyProperty注釈は、フィールドレベルまたはメソッドレベルで適用する必要があります。

@KeyProperty( value = "userName" )
protected List<User> users = null;

TypeConversionアノテーション-(タイプ変換アノテーション)

この注釈注釈は、クラスおよびアプリケーション全体の変換規則に使用されます。 TypeConversion注釈は、プロパティレベルおよびメソッドレベルで適用できます。

@TypeConversion(rule = ConversionRule.COLLECTION,
converter = "java.util.String")
public void setUsers( List users ) {
   this.users = users;
}