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
- Public class RegistAction extends ActionSupport
- {
- Private String name;
- Private String pass;
- Private int age;
- Private Date birth;
- // Setter and getter methods of the name attribute
- // Use Annotation to specify the required and regular expression verification rules
- @ RequiredStringValidator (key = "name. requried"
- , Message = "")
- @ RegexFieldValidator (expression = "\ w {4, 25 }"
- , Key = "name. regex", message = "")
- Public void setName (String name)
- {
- This. name = name;
- }
- Public String getName ()
- {
- Return this. name;
- }
- // The setter and getter methods of the pass attribute
- @ RequiredStringValidator (key = "pass. requried"
- , Message = "")
- @ RegexFieldValidator (expression = "\ w {4, 25 }"
- , Key = "pass. regex", message = "")
- Public void setPass (String pass)
- {
- This. pass = pass;
- }
- Public String getPass ()
- {
- Return this. pass;
- }
- // The setter and getter methods of the age attribute
- @ IntRangeFieldValidator (message = ""
- , Key = "age. range", min = "1"
- , Max = "150 ")
- Public void setAge (int age)
- {
- This. age = age;
- }
- Public int getAge ()
- {
- Return this. age;
- }
- // Setter and getter methods of the birth attribute
- // Use Annotation to specify a date range validation rule
- @ DateRangeFieldValidator (message = ""
- , Key = "birth. range", min = "1900/01/01"
- , Max = "2050/01/21 ")
- Public void setBirth (Date birth)
- {
- This. birth = birth;
- }
- Public Date getBirth ()
- {
- Return this. birth;
- }
- }
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.