Annotation-based input verification

Source: Internet
Author: User

This Annotation-based input validation is also part of the "Zero Configuration" feature of Struts 2. It allows Annotation to define the rules that each field should meet. opensymphony. xwork2.validator. the Annotation package provides Annotation related to a large number of validators. These Annotation correspond to the previously described validators. You can refer to the API documentation on your own.

In order to specify the verification rule through Annotation in the Action class, you can configure it as follows:

Use the Annotation validators to modify the setter methods corresponding to each attribute in the Action.

Next, we modify the I18NValidate application, and delete the validation rule file under the WEB-INF \ src \ lee path of the application, modify the RegistAction under the path. java file, specifying the rules that each attribute should meet by commenting. The modified Action Code is as follows.

Program list: codes \ 04 \ 4.2 \ annotation \ WEB-INF \ src \ org \ crazyit \ app \ action \ RegistAction. java

 

 
 
  1. Public class RegistAction extends ActionSupport
  2. {
  3. Private String name;
  4. Private String pass;
  5. Private int age;
  6. Private Date birth;
  7. // Setter and getter methods of the name attribute
  8. // Use Annotation to specify the required and regular expression verification rules
  9. @ RequiredStringValidator (key = "name. requried"
  10. , Message = "")
  11. @ RegexFieldValidator (expression = "\ w {4, 25 }"
  12. , Key = "name. regex", message = "")
  13. Public void setName (String name)
  14. {
  15. This. name = name;
  16. }
  17. Public String getName ()
  18. {
  19. Return this. name;
  20. }
  21. // The setter and getter methods of the pass attribute
  22. @ RequiredStringValidator (key = "pass. requried"
  23. , Message = "")
  24. @ RegexFieldValidator (expression = "\ w {4, 25 }"
  25. , Key = "pass. regex", message = "")
  26. Public void setPass (String pass)
  27. {
  28. This. pass = pass;
  29. }
  30. Public String getPass ()
  31. {
  32. Return this. pass;
  33. }
  34. // The setter and getter methods of the age attribute
  35. @ IntRangeFieldValidator (message = ""
  36. , Key = "age. range", min = "1"
  37. , Max = "150 ")
  38. Public void setAge (int age)
  39. {
  40. This. age = age;
  41. }
  42. Public int getAge ()
  43. {
  44. Return this. age;
  45. }
  46. // Setter and getter methods of the birth attribute
  47. // Use Annotation to specify a date range validation rule
  48. @ DateRangeFieldValidator (message = ""
  49. , Key = "birth. range", min = "1900/01/01"
  50. , Max = "2050/01/21 ")
  51. Public void setBirth (Date birth)
  52. {
  53. This. birth = birth;
  54. }
  55. Public Date getBirth ()
  56. {
  57. Return this. birth;
  58. }
  59. }

The bold text code of the above Action uses the Annotation validators to modify the setter methods of each attribute, so that Struts 2 will know what rules should be met by each attribute. By using Annotation in Action to specify the validation rules that each field should meet, you can avoid writing XML validation rule files.

The use of Annotation to replace XML configuration files is a trend after Annotation is added in JDK 1.5. Using this method, you do not need to write XML files, which can simplify application development, however, the side effect is that all content is written into Java code, which makes it difficult for later maintenance.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.