Struts 2 - 註解型別



Struts 2 應用程式可以使用 Java 5 註解作為 XML 和 Java 屬性配置的替代方案。以下是與不同類別相關的最重要的註解列表:

名稱空間註解(Action 註解)

@Namespace 註解允許在Action 類中定義 Action 的名稱空間,而不是基於零配置約定。

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

結果註解 -(Action 註解)

@Result 註解允許在 Action 類中定義 Action 結果,而不是在 XML 檔案中。

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

結果集註解 -(Action 註解)

@Results 註解定義了一組 Action 的結果。

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

After 註解 -(攔截器註解)

@After 註解標記需要在主 Action 方法和結果執行後呼叫的 Action 方法。返回值被忽略。

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 註解 -(攔截器註解)

@Before 註解標記需要在主 Action 方法和結果執行前呼叫的 Action 方法。返回值被忽略。

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 註解標記需要在結果執行前執行的 Action 方法。返回值被忽略。

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 註解 -(驗證註解)

此驗證註解檢查雙精度欄位的值是否在指定範圍內。如果既沒有設定最小值也沒有設定最大值,則不會執行任何操作。

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 註解 -(驗證註解)

此驗證註解檢查數值欄位的值是否在指定範圍內。如果既沒有設定最小值也沒有設定最大值,則不會執行任何操作。

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 註解 -(驗證註解)

此驗證註解檢查欄位是否非空。此註解必須應用於方法級別。

public class Employee extends ActionSupport{

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

RequiredStringValidator 註解 -(驗證註解)

此驗證註解檢查字串欄位是否不為空(即非空且長度 > 0)。

public class Employee extends ActionSupport{

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

StringLengthFieldValidator 註解 -(驗證註解)

此驗證器檢查字串欄位的長度是否正確。它假設該欄位為字串。如果既沒有設定最小長度也沒有設定最大長度,則不會執行任何操作。

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 註解 -(驗證註解)

如果要使用幾種相同型別的註解,則這些註解必須巢狀在 @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 註解必須應用於型別級別。

@Conversion()
   public class ConversionAction implements Action {
}

CreateIfNull 註解 -(型別轉換註解)

此註解設定型別轉換的 CreateIfNull。CreateIfNull 註解必須應用於欄位或方法級別。

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

Element 註解 -(型別轉換註解)

此註解設定型別轉換的 Element。Element 註解必須應用於欄位或方法級別。

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

Key 註解 -(型別轉換註解)

此註解設定型別轉換的 Key。Key 註解必須應用於欄位或方法級別。

@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;
}
struts_annotations.htm
廣告

© . All rights reserved.