Struts 2 Annotations Types
Struts 2应用程序可以使用Java 5注释作为XML和Java属性配置的替代方法。 以下是与不同类别相关的最重要注释列表 -
Namespace Annotation (Action Annotation)
@Namespace注释允许在Action类中定义Action的名称空间,而不是基于Zero Configuration的约定。
@Namespace("/content")
public class Employee extends ActionSupport{
...
}
结果注释 - (动作注释)
@Result注释允许在Action类中定义Action结果而不是XML文件。
@Result(name = "success", value = "/success.jsp")
public class Employee extends ActionSupport{
...
}
结果注释 - (动作注释)
@Results注释定义了一组Action的结果。
@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 Annotation - (拦截器注释)
@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注释 - (验证注释)
此验证注释检查双字段是否具有指定范围内的值。 如果既没有设置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注释 - (验证注释)
此验证注释检查字段是否为空。 必须在方法级别应用注释。
public class Employee extends ActionSupport{
@RequiredFieldValidator(message = "Default message",
key = "i18n.key", shortCircuit = true)
public String getAge() {
return age;
}
}
RequiredStringValidator注释 - (验证注释)
此验证注释检查String字段不为空(即长度> 0的非null)。
public class Employee extends ActionSupport{
@RequiredStringValidator(message = "Default message",
key = "i18n.key", shortCircuit = true, trim = true)
public String getName() {
return name;
}
}
StringLengthFieldValidator注释 - (验证注释)
此验证器检查String字段的长度是否合适。 它假定该字段是一个字符串。 如果既未设置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;
}