Bonjour,
Je bute sur un malheureux problème d'affichage d'erreurs. Un simple formulaire, index.jsp demande un nom et un âge :
Le Bean du formulaire, FormulaireBean.java est le suivant :
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
26
27
28
29
30 <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <jsp:useBean id="clock" class="java.util.Date"/> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="fr"> <head> <title>Projet en Struts</title> <meta http-equiv="Content-type" content="text/html;charset=iso-8859-1" /> </head> <body> <h1>Bonjour!</h1> Date et heure du jour : <%=clock.toString()%> <hr> <html:form action="/accueil"> <table width="45%" border="0"> <tr> <td>nom:</td> <td><html:text property="nom" /></td> </tr> <tr> <td>age:</td> <td><html:text property="age" /></td> </tr> <tr> <td colspan="2" align="center"> <html:submit /></td> </tr> </table> </html:form> </body> </html>
Enfin, le struts-config :
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 public class FormulaireBean extends ActionForm { private static final long serialVersionUID = 1L; private String nom; private int age; /* setters et getters */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (getNom() == null || getNom().equals("")) { errors.add("nom", new ActionMessage("erreur.nom.required")); } if (getAge() <= 0) { errors.add("age", new ActionMessage("erreur.age.required")); } return errors; } }
Bien évidemment, le fichier "MessageResources.properties" se trouve dans /WEB-INF/resources :
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 <struts-config> <form-beans> <form-bean name="formBean" type="beans.FormulaireBean" /> </form-beans> <action-mappings> <action path="/accueil" name="formBean" input="/jsp/index.jsp" validate="false" type="src.Accueil"> <forward name="echec" path="/jsp/echec.jsp" /> <forward name="succes" path="/jsp/succes.jsp" ></forward> </action> </action-mappings> <message-resources parameter="resources.MessageResources"></message-resources> </struts-config>
Maintenant, je n'arrive pas à comprendre pourquoi mes messages d'erreur n'apparaissent pas dans ma page echec.jsp (quand je laisse le champ "nom" vide et/ou un âge négatif) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 erreurs.nom.required=Le nom n'est pas précisé erreur.age.required=L'â n'est pas précisé erreurs.formulaire.vide=Le formulaire est vide !
Je suis certain que cela doit être tout bête
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 <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <jsp:useBean id="clock" class="java.util.Date"/> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="fr"> <head> <title>Projet en Struts : échec !</title> <meta http-equiv="Content-type" content="text/html;charset=iso-8859-1" /> <style type="text/css"> h1 {color : red} </style> </head> <body> <h1>Échec !</h1> <u>Raisons :</u> <hr/> <html:errors /> <hr/> Date et heure du jour : <%=clock.toString()%> </body> </html>, mais si quelqu'un pouvait m'aider...
Merci par avance !![]()
Partager