Add a checksum menu
This chapter will show you how to add check logic for PersonForm, so that firstname and lastname becomes a required field on the page, and add a screen list to display the records in the database.
This part of the content depends on Part III: Create an action and JSP
About this chapter
This chapter will show you how to use Struts's Validator to add check logic (clients and server-side) for your PersonForm object. We will also use Display Tag Library
Create a screen list to display all people in the database.
content
· [1] Add XDoclet Validator Tag for Person.java
· [2] Check and test the JSP to add a check
· [3] Add TestgetPeople Method for DAO and Manager Test
· [4] Add a getPeople () method for DAO and Manager
· [5] Add TestSearch () method for Action Test
· [6] Add a search method for Action
· [7] Create PersonList.JSP and Canoo Test
· [8] Add a link to the menu (Link)
Add xdoclet validator tag for Person.java
Under normal circumstances, if you want to use Struts Validator, you must write the value.xml file with your hand. If you don't use AppFuse, you must also configure Validator Plugin and Error Key in the ApplicationResources_en.properties file. If you want more information, see Validation Made Easy Tutorial
.
We want to thank xdoclets, which makes all this is very simple - you only need to add a few @ Struts.validator tags for the Person class. Open it (SRC / DAO / ** / Model / Person.java), then modify the javadoc's Javadoc's Javadoc to enable them to include @ struts.validator type = "required" tab.
/ ** * @ struts.validator type = "required" * @ hibernate.property column = "first_name" length = "50" * / public String getFirstName () {return this.firstName;} / ** * @ struts.validator TYPE = "Required" * @ hibernate.property column = "Last_name" length = "50" * / public string getlastname () {return this.lastname;}
You can also add a MSGKey property (Attribute) that is used in this tag, which is used to overwrite the key of the wrong default message.
@ Struts.validator type = "required" msgkey = "error.required"
The default key of Type = "Required" is already error.Required, so I often keep it as the default. This key is defined in web / web-inf / classes / applicationResources _ *. Property. You may notice that although the xdoclet's documentation should put these labels in the setter section, we put them in the getter section, because the template file (Metadata / Template / Struts_Form.xDT) places these labels in the generated PersonForm. Java's setter section.
Now if you save Person.java, then run the Ant Clean WebDoclet, you will generate a validation.xml file in the directory build / your project name / web -in. It should have an entry used by "PersonForm":