Struts2 应用程序可以使用Java5注释来替代XML和Java属性的配置。以下是与不同类别相关的最重要注释的列表:
Namespace注释(Action注释)
@Namespace注释允许在Action类中定义Action的命名空间,而不是基于零配置的约定。
- @Namespace("/content")
- public class Employee extends ActionSupport{
- ...
- }
Result注释(Action注释)
@Result注释允许在Action类中定义Action的结果,而不是XML文件。
- @Result(name="success", value="/success.jsp")
- public class Employee extends ActionSupport{
- ...
- }
Results注释(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注释(验证注释)
此验证注释检查具有指定范围内值的双字段。如果既不设置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字段不为空(即非null,长度大于0)。
- 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注释(验证注释)
如果要使用多个相同类型的注释,这些注释必须嵌套在@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( 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;
- }
转载本站内容时,请务必注明来自W3xue,违者必究。