J'ai intégré Validator dans mon application, et voici la configuration :
1- struts-config :
2- validation.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in> <!-- Properties...--> <message-resources parameter="resources/application"/> <!-- Formulaires...--> <form-beans> <form-bean name="formContact" type="src.main.formulaires.ContactForm"/> </form-beans>
3- ContactForm
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 <formset> <!-- Validation pour le formulaire ContactForm --> <form name="formContact"> <field property="nom" depends="required"> <arg key="formContact.nom"/> </field> </form> </formset>
4- et la JSP : contacts.jsp :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class ContactForm extends ValidatorForm{ /** Serial Version UID */ private static final long serialVersionUID = 1L; /** Champ du formulaire : nom */ private String nom; // Constructeur par défaut de ContactForm()... public ContactForm(){} // getter et setter du name public String getNom() { return nom; } public void setNom(String nom) { this.nom = nom; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // ici on récupère tous les messages d'erreurs gérés par le Validator ActionErrors errors = super.validate(mapping, request); if (nom == null || nom.length()==0){ errors.add("error.nom.empty",new ActionMessage ("error.nom")); } return errors; } }
5- Properties : application.properties
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <td colspan="2"> <html:text property="nom" styleClass="champ" value=""></html:text> <FONT face="Arial, Helvetica, sans-serif" size=2 color=red> <html:errors property="error.nom.empty"/> </FONT> </td>
Ce que je veux, c'est afficher un message d'erreur en rouge à coté de mon champ, j'ai pris simple pour commencer, mais je suis perdu je n'arrive pas à comprendre pourquoi ça ne marche pas...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 formContact.nom=Nom errors.nom=<font color="blue">Le champ [{0}] doit être renseigné.</font> errors.required= <font color="blue">Le champ [{0}] doit être renseigné.</font>
Est ce que j'ai oublié qque chose ??
Merci d'avance de votre aide...
Partager