课程表

Struts2 教程

Struts2 标签

Struts2 集成

工具箱
速查手册

Struts2 注释类型

当前位置:免费教程 » Java相关 » Struts2

Struts2 应用程序可以使用Java5注释来替代XML和Java属性的配置。以下是与不同类别相关的最重要注释的列表:

Namespace注释(Action注释)

@Namespace注释允许在Action类中定义Action的命名空间,而不是基于零配置的约定。

  1. @Namespace("/content")
  2. public class Employee extends ActionSupport{
  3. ...
  4. }

Result注释(Action注释)

@Result注释允许在Action类中定义Action的结果,而不是XML文件。

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

Results注释(Action注释)

@Results注释定义了一个Action的一组结果。

  1. @Results({
  2. @Result(name="success", value="/success.jsp"),
  3. @Result(name="error", value="/error.jsp")
  4. })
  5. public class Employee extends ActionSupport{
  6. ...
  7. }

After注释(拦截器注释)

@After注释标记需要在执行主action方法和结果后调用的action方法(忽略返回值)。

  1. public class Employee extends ActionSupport{
  2. @After
  3. public void isValid() throws ValidationException {
  4. // validate model object, throw exception if failed
  5. }
  6. public String execute() {
  7. // perform secure action
  8. return SUCCESS;
  9. }
  10. }

Before注释(拦截器注释)

@Before注释标记需要在执行主action方法和结果之前调用的action方法(忽略返回值)。

  1. public class Employee extends ActionSupport{
  2. @Before
  3. public void isAuthorized() throws AuthenticationException {
  4. // authorize request, throw exception if failed
  5. }
  6. public String execute() {
  7. // perform secure action
  8. return SUCCESS;
  9. }
  10. }

BeforeResult注释(拦截器注释)

@BeforeResult注释标记需要在结果之前执行的action方法(忽略返回值)。

  1. public class Employee extends ActionSupport{
  2. @BeforeResult
  3. public void isValid() throws ValidationException {
  4. // validate model object, throw exception if failed
  5. }
  6. public String execute() {
  7. // perform action
  8. return SUCCESS;
  9. }
  10. }

ConversionErrorFieldValidator注释(验证注释)

此验证注释检查字段是否存在任何转换错误,并在存在时应用。

  1. public class Employee extends ActionSupport{
  2. @ConversionErrorFieldValidator(message = "Default message",
  3. key = "i18n.key", shortCircuit = true)
  4. public String getName() {
  5. return name;
  6. }
  7. }

DateRangeFieldValidator注释(验证注释)

此验证注释检查日期字段的值是否在指定范围内。

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

DoubleRangeFieldValidator注释(验证注释)

此验证注释检查具有指定范围内值的双字段。如果既不设置min也不设置max,则不会执行任何操作。

  1. public class Employee extends ActionSupport{
  2. @DoubleRangeFieldValidator(message = "Default message",
  3. key = "i18n.key", shortCircuit = true,
  4. minInclusive = "0.123", maxInclusive = "99.987")
  5. public String getIncome() {
  6. return income;
  7. }
  8. }

EmailValidator注释(验证注释)

如果该字段包含非空字符串,则该验证注释将检查该字段是否为有效的电子邮件地址。

  1. public class Employee extends ActionSupport{
  2. @EmailValidator(message = "Default message",
  3. key = "i18n.key", shortCircuit = true)
  4. public String getEmail() {
  5. return email;
  6. }
  7. }

ExpressionValidator注释(验证注释)

此非字段级验证器验证所提供的正则表达式。

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

IntRangeFieldValidator注释(验证注释)

此验证注释检查数字字段是否具有指定范围内的值。如果既不设置min也不设置max,则不会执行任何操作。

  1. public class Employee extends ActionSupport{
  2. @IntRangeFieldValidator(message = "Default message",
  3. key = "i18n.key", shortCircuit = true,
  4. min = "0", max = "42")
  5. public String getAge() {
  6. return age;
  7. }
  8. }

RegexFieldValidator注释(验证注释)

此注释使用正则表达式验证字符串字段。

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

RequiredFieldValidator注释(验证注释)

此验证注释检查字段是否为非空。注释必须在方法级别应用。

  1. public class Employee extends ActionSupport{
  2. @RequiredFieldValidator(message = "Default message",
  3. key = "i18n.key", shortCircuit = true)
  4. public String getAge() {
  5. return age;
  6. }
  7. }

RequiredStringValidator注释(验证注释)

此验证注释检查String字段不为空(即非null,长度大于0)。

  1. public class Employee extends ActionSupport{
  2. @RequiredStringValidator(message = "Default message",
  3. key = "i18n.key", shortCircuit = true, trim = true)
  4. public String getName() {
  5. return name;
  6. }
  7. }

StringLengthFieldValidator注释(验证注释)

此验证器检查字符串字段是否具有正确的长度,它假定该字段是一个字符串。如果既没有设置minLength也没有设置maxLength,则不会执行任何操作。

  1. public class Employee extends ActionSupport{
  2. @StringLengthFieldValidator(message = "Default message",
  3. key = "i18n.key", shortCircuit = true,
  4. trim = true, minLength = "5", maxLength = "12")
  5. public String getName() {
  6. return name;
  7. }
  8. }

UrlValidator注释(验证注释)

此验证器检查字段是否是有效的URL。

  1. public class Employee extends ActionSupport{
  2. @UrlValidator(message = "Default message",
  3. key = "i18n.key", shortCircuit = true)
  4. public String getURL() {
  5. return url;
  6. }
  7. }

Validations注释(验证注释)

如果要使用多个相同类型的注释,这些注释必须嵌套在@Validations()注释中。

  1. public class Employee extends ActionSupport{
  2. @Validations(
  3. requiredFields =
  4. {@RequiredFieldValidator(type = ValidatorType.SIMPLE,
  5. fieldName = "customfield",
  6. message = "You must enter a value for field.")},
  7. requiredStrings =
  8. {@RequiredStringValidator(type = ValidatorType.SIMPLE,
  9. fieldName = "stringisrequired",
  10. message = "You must enter a value for string.")}
  11. )
  12. public String getName() {
  13. return name;
  14. }
  15. }

CustomValidator注释(验证注释)

此注释可用于自定义验证器。使用ValidationParameter注释提供其他参数。

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

Conversion注释(类型转换注释)

这是类型级别的类型转换的标记注释。转换注释必须在类型级别应用。

  1. @Conversion()
  2. public class ConversionAction implements Action {
  3. }

CreateIfNull注释(类型转换注释)

此注释为类型转换设置CreateIfNull。CreateIfNull注释必须在字段或方法级别应用。

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

Element注释(类型转换注释)

此注释设置类型转换的元素。Element注释必须在字段或方法级别应用。

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

Key注释(类型转换注释)

此注释设置类型转换的key。Key注释必须在字段或方法级别应用。

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

KeyProperty注释(类型转换注释)

此注释设置类型转换的KeyProperty。KeyProperty注释必须在字段或方法级别应用。

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

TypeConversion注释(类型转换注释)

此注释用于类和应用程序范围的转换规则。TypeConversion注释可以在属性和方法级别应用。

  1. @TypeConversion(rule = ConversionRule.COLLECTION,
  2. converter = "java.util.String")
  3. public void setUsers( List users ) {
  4. this.users = users;
  5. }
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号