Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
SpringControllers_it




JSPWiki v2.2.33

[RSS]


Hide Menu

ValidationAndListSpring_it


Difference between version 2 and version 1:

At line 5 changed 2 lines.
!!About this Tutorial
This tutorial will show you how to add Validation logic (client and server-side) to the Person object using Commons Validator. We'll also create a list screen using the [Display Tag Library|http://displaytag.sf.net] to display all the people in the database.
!!Info su questo Tutorial
Questo tutorial mostra come aggiungere la logica di validazione (sia client che server-side) all'oggetto Person usando Commons Validator. Verrà creata anche una schermata di elenco usando la [Display Tag Library|http://displaytag.sf.net] per mostrare tutte le persone nel database.
At line 8 changed 1 line.
;:%%(color: blue)''I will tell you how I do stuff in the __Real World__ in text like this.''%%
;:%%(color: blue)''Ti dirò come faccio le cose nel __Mondo Reale__ in un testo come questo.''%%
At line 10 changed 9 lines.
!Table of Contents
* [1] Add XDoclet Validator tags to Person.java
* [2] View JSP with validation added and test
* [3] Add ''testGetPeople'' methods to DAO and Manager Tests
* [4] Add ''getPeople'' methods to PersonDao and Manager
* [5] Create PersonControllerTest
* [6] Create PersonController
* [7] Create personList.jsp and Canoo test
* [8] Add link to menu
!Indice
* [1] Aggiungi i tag XDoclet per il Validator a Person.java
* [2] Vedi JSP con la validazione aggiunta e verifica
* [3] Aggiungi i metodi ''testGetPeople'' ai test di DAO e Manager
* [4] Aggiungi i metodi ''getPeople'' a PersonDao e Manager
* [5] Crea il PersonControllerTest
* [6] Crea il PersonController
* [7] Crea personList.jsp ed il test Canoo
* [8] Aggiungi un collegamento al menu
At line 20 changed 2 lines.
!!Add XDoclet Validator to Person.java [#1]
To use Commons Validator with Spring MVC, normally you have to write a validation.xml file by hand. However, thanks to XDoclet, it's much easier - you just need to add a couple of ''@spring.validator'' tags to our POJO (Person.java). Let's open it up (src/dao/**/dao/Person.java) and modify your setFirstName and setLastName methods to resemble the following:
!!Aggiungi i tag XDoclet per il Validator a Person.java [#1]
Per usare Commons Validator con Spring MVC normalmente devi scrivere un file validation.xml a mano. Tuttavia, grazie a XDoclet, è nolto più semplice - devi solo aggiungere un paio di tag ''@spring.validator'' al tuo POJO (Person.java). Apriamo il file (src/dao/**/dao/Person.java) e modifichiamo i metodi setFirstName e setLastName come segue:
At line 40 changed 1 line.
I should mention that you can also add a ''msgkey'' attribute to this tag to override the default message key for this error.
Va inoltre menzionato che puoi anche aggiungere un attributo ''msgkey'' a questo tag per effettuare l'override della chiave messaggio predefinita per questo errore.
At line 47 changed 1 line.
The default key for type="required" is already ''errors.required'', so I usually leave it to the default. This key is defined in web/WEB-INF/classes/ApplicationResources_*.properties.
La chiave predefinita per type="required" è già ''errors.required'', per cui io di solito la lascio al default. Questa chiave è definita in web/WEB-INF/classes/ApplicationResources_*.properties.
At line 49 changed 1 line.
Now if you save Person.java and run __ant clean webdoclet__, a validation.xml file will be generated in build/appfuse/WEB-INF/. It's contents should have now have an entry for "person".
Ora se registri Person.java ed esegui __ant clean webdoclet__, verrà generato un file validation.xml in build/appfuse/WEB-INF/. Il contenuto dovrebbe avere un elemento per "person".
At line 67 changed 1 line.
To enable validation in personForm.jsp, you'll need to make sure your personForm.jsp has the following at the bottom:
Per attivare la validazione in personForm.jsp, devi assicurarti che personForm.jsp contenga alla fine quanto segue:
At line 74 changed 1 line.
You'd also need to uncomment the "validator" property we commented out earlier. Make sure the "personFormController" <bean> in web/WEB-INF/action-servlet.xml has the following XML:
Devi anche decommentare la property "validator" che abbiamo commentato prima. Controlla che il <bean> "personFormController" in web/WEB-INF/action-servlet.xml abbia il seguente XML:
At line 88 changed 1 line.
!!View JSP with validation added and test [#2]
!!Vedi JSP con la validazione aggiunta e verifica [#2]
At line 90 changed 1 line.
Now that we have Validation configured for this form, let's make sure it works. Run __ant db-load deploy__, start Tomcat and go to [http://localhost:8080/appfuse/editPerson.html?id=1]. One thing you might notice is that the "First Name" and "Last Name" labels now have asterisks next to them - indicating required fields. This is achieved with a LabelTag that looks up the validation rules from Commons Validator.
Ora che abbiamo la validazione configurata su questo form, assicuriamoci che funzioni. Esegui __ant db-load deploy__, avvia Tomcat e vai su [http://localhost:8080/appfuse/editPerson.html?id=1]. Un'altra cosa che potresti notare è che le label "First Name" e "Last Name" ora hanno degli asterischi vicino - che indicano i campi come obbligatori. Ciò è ottenuto tramite un LabelTag che legge le regole di validazione da Commons Validator.
At line 92 changed 1 line.
If you erase the values in the firstName and lastName fields and click the save button, you should get the following JavaScript alert.
Se cancelli i valori nei campi firstName e lastName e fai clic sul bottone registra, dovresti avere il seguente JavaScript.
At line 152 changed 1 line.
!!Add getPeople() method to DAO and Manager [#4]
!!Aggiungi il metodo getPeople() a DAO e Manager [#4]
At line 207 changed 1 line.
!!Create PersonControllerTest [#5]
!!Crea il PersonControllerTest [#5]
At line 253 changed 2 lines.
!!Create PersonController [#6]
Create src/web/**/action/PersonController.java. It should implement {{org.springframework.web.servlet.mvc.Controller}} and read as follows:
!!Crea il PersonController [#6]
Crea il file src/web/**/action/PersonController.java. Dovrebbe implementare {{org.springframework.web.servlet.mvc.Controller}} e leggersi come segue:
At line 295 changed 1 line.
Now add a definition to web/WEB-INF/action-servlet.xml for this Controller.
Ora aggiungi una definizione a web/WEB-INF/action-servlet.xml per questo Controller.
At line 304 changed 1 line.
Now if you run __ant test-web -Dtestcase=PersonController__, the test should pass. Next, create a URL mapping for this controller. To do this, search for the "urlMapping" bean in web/WEB-INF/action-servlet.xml and add the following line to the "mappings" property:
Ora se esegui __ant test-web -Dtestcase=PersonController__, il test dovrebbe passare. Successivamente, crea il mapping su URL per questo controller. Per fare ciò, cerca il bean "urlMapping" in web/WEB-INF/action-servlet.xml ed aggiungi la riga seguente alla proprietà "mappings":
At line 311 changed 2 lines.
!!Create personList.jsp and Canoo test [#7]
Open the personList.jsp file in the ''web/pages'' folder of your project. One thing you'll probably want to change is the plural form of the items you're listing. The generated name in this example is "persons" and it should probably be people. At or near line 31, you should have the following line:
!!Crea personList.jsp ed il test Canoo [#7]
Apri il file personList.jsp nella cartella ''web/pages'' del tuo progetto. Una cosa che vorrai probabilmente modificare è la forma plurale degli elementi che stai elencando. Il nome generati in questo esempio è "persons" è probabilmente dovrebbe essere people. At or near line 31, you should have the following line:

Back to ValidationAndListSpring_it, or to the Page History.