Bonjour,
j'ai un petit soucis avec la déclaration de formulaires dynamiques sous STRUTS.
Je suis entrain de réaliser le tutoriel de serge tahe sur le sujet.
Mon probleme est que lorsque je veux declarer une classe qui herite de DynaActionForm et que je mets le validate à true, j'ai des messages d'erreurs.
Message d'erreur :
L'erreur se produit quand je valide le formulaire.java.lang.NullPointerException
org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:783)
org.apache.struts.action.RequestProcessor.processActionForm(RequestProcessor.java:364)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:253)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
Mon struts config :
Ma 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 <struts-config> <form-beans> <form-bean name="frmPersonne" type="com.bourakba.struts.strutspersonne.PersonneDynaForm"> <form-property name="nom" type="java.lang.String" initial=""/> <form-property name="age" type="java.lang.String" initial=""/> </form-bean> </form-beans> <action-mappings> <action path="/main" name="frmPersonne" scope="session" validate="true" input="/erreurs.do" parameter="/vues/main.html" type="org.apache.struts.action.DynaActionForm" > <!-- type="org.apache.struts.actions.ForwardAction" "com.bourakba.struts.strutspersonne.PersonneDynaForm" "org.apache.struts.action.DynaActionForm" --> <forward name="reponse" path="/reponse.do"> </forward> </action> <action path="/erreurs" parameter="/vues/erreurs.personne.jsp" type="org.apache.struts.actions.ForwardAction"> </action> <action path="/reponse" parameter="/vues/reponse.personne.jsp" type="org.apache.struts.actions.ForwardAction"> </action> <action path="/formulaire" parameter="/vues/formulaire.personne.jsp" type="org.apache.struts.actions.ForwardAction"> </action> </action-mappings> <message-resources parameter="ressources.personneressources" /> </struts-config>
Mon Bean :
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 <html:form action="/main.do" name="frmPersonne" type="com.bourakba.struts.strutspersonne.PersonneDynaForm"> <table> <tr><td>Nom</td><td> <html:text property="nom" size="20" value="" /> </td></tr> <tr><td>Age</td><td><html:text property="age" size="3" value ="" /> </td></tr> </table> <table> <tr> <td><html:submit value="Envoyer"/></td> <td><html:reset value="Reinitaliser"/></td> <td><html:button property="btnEffacer" value="Effacer" onclick="effacer()"/></td> </tr> </table> <hr></hr> <p><html:link page="/formulaire.do" >Retour au formulaire</html:link></p> <p><html:link page="" >Retour à l'accueil </html:link></p> </html:form> </body> </html>
mon action :
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
31
32
33
34
35
36
37 public class PersonneDynaForm extends DynaActionForm { /* (non-Javadoc) * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest) */ @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // gestion des erreurs ActionErrors erreurs = new ActionErrors(); String nom = (String) this.get("nom"); String age = (String) this.get("age"); // le nom doit être non vide if (nom == null || nom.trim().equals("")) { erreurs.add("nomvide", new ActionError("personne.formulaire.nom.vide")); // l'âge doit être un entier positif } if (age == null || age.trim().equals("")) { erreurs.add("agevide", new ActionError("personne.formulaire.age.vide")); } else { // l'âge doit être un entier positif if (!age.matches("^\\s*\\d+\\s*$")) { erreurs.add("ageincorrect", new ActionError("personne.formulaire.age.incorrect", age)); // on rend la liste des erreurs } } //if // on rend la liste d'erreurs return erreurs; } }
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 public class FormulaireAction extends Action { /* (non-Javadoc) * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // on a un formulaire valide, sinon on ne serait pas arrivé là PersonneDynaForm formulaire=(PersonneDynaForm)form; request.setAttribute("nom", formulaire.get("nom")); request.setAttribute("age", formulaire.get("age")); return mapping.findForward("reponse"); } }
Bonne Soirée.
Partager