conflit Validator + DispatchAction
bonjour,
j'ai un formulaire basé sur une classe action héritée de 'DispatchAction'. Lorsque j'appuie sur le bouton valider du formulaire, il affiche les erreurs s'il y en a (ex: champs requis), jusque là tout va bien.. Puis lorsque qu'il n'y a plus d'erreurs a afficher, le controlleur ne fait pas passer a une autre page. Il ne se passe rien, même pas d'erreur.
Si je ne valide pas les erreurs du formulaire (validate a false dans struts config), le passage d'une page a l'autre se fait bien, toujours en utilisant la classe qui hérite de 'DispatchAction'.
Donc le validator semble me bloquer le passage dans ma classe d'action.
un peu de code:
Le fichier de config:
Code:
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
| <?xml version="1.0" encoding="UTF-8"?>
<struts-config>
<form-beans>
<form-bean name="animalForm" type="myStruts.form.AnimalForm" />
</form-beans>
<action-mappings>
<action
type="myStruts.action.AnimalAction"
input="/pages/animal.jsp"
name="animalForm"
attribute="animal"
path="/GestionAnimal"
parameter="nomMethod"
validate="true" >
<forward name="proprietaire" path="/pages/proprietaire.jsp" />
<forward name="adresse" path="/pages/adresse.jsp" />
<forward name="affiche" path="/pages/affiche.jsp" />
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="MessageResources"/>
<message-resources key="mesMessages" parameter="Struts.resources.messages"/>
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
<set-property property="moduleAware" value="true"/>
</plug-in>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config> |
L'action form:
Code:
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| package myStruts.form;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
import myStruts.bean.Adresse;
import myStruts.bean.Identite;
public class AnimalForm extends ValidatorForm {
private String nom;
private String dateNaiss;
private GregorianCalendar dateTmp;
private String age = "0";
private String type;
private Date date;
private Identite identite = new Identite();
public Identite getIdentite() {
return identite;
}
public void setIdentite(Identite identite) {
this.identite = identite;
}
/* public void reset(ActionMapping mapping, HttpServletRequest request) {
//if (page == 0) {
identite = new Identite();
identite.setNom("");
identite.setPrenom("");
identite.setEmail("");
identite.setAdresse(new Adresse());
//}
} */
public String getDateNaiss() {
return dateNaiss;
}
public void setDateNaiss(String dateNaiss) {
int anInt;
int moisInt;
int jourInt;
String[] mesDates = dateNaiss.split("/");
this.dateNaiss = dateNaiss;
try {
if (mesDates.length > 2) {
anInt = Integer.parseInt(mesDates[2]);
moisInt = Integer.parseInt(mesDates[1]);
jourInt = Integer.parseInt(mesDates[0]);
dateTmp = new GregorianCalendar(anInt, moisInt, jourInt);
}
} catch (Exception e){
this.dateNaiss = "" ;
}
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAge() {
date = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(date);
int annee = cal.get(Calendar.YEAR) - dateTmp.get(GregorianCalendar.YEAR);
age = String.valueOf(annee);
return age;
}
} |
la classe d'action:
Code:
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 52 53 54 55 56 57 58 59 60
| /*******************************************************************************
* Copyright (c) 2005 Eteration A.S. and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Eteration A.S. - initial API and implementation
******************************************************************************/
package myStruts.action;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class AnimalAction extends DispatchAction {
public ActionForward pageAnimal(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
System.out.println("on passe dans la classe action");
String target = "proprietaire";
return mapping.findForward(target);
}
public ActionForward pageProprietaire(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String target = "adresse";
return mapping.findForward(target);
}
public ActionForward pageAdresse(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String target = "affiche";
return mapping.findForward(target);
}
} |
et le formulaire:
Code:
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="http://struts.apache.org/tags-nested" prefix="nested" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:html>
<head>
<title>Informations relatives à l animal</title>
</head>
<body>
<html:form action="/GestionAnimal.do?nomMethod=pageAnimal">
<bean:message key="animal.nom" bundle="mesMessages" />
<html:text property="nom"/>
<html:errors property="nom" header="" footer="" prefix="" suffix="" /><br>
<bean:message key="animal.dateNaiss" bundle="mesMessages" />
<html:text property="dateNaiss" />
<html:errors property="dateNaiss" header="" footer="" prefix="" suffix="" /><br>
<bean:message key="animal.type" bundle="mesMessages" />
<html:text property="type" />
<html:errors property="type" header="" footer="" prefix="" suffix="" /><br>
<html:submit value="Valider" />
</html:form>
</body>
</html:html> |
Si quelqu'un connait ce probleme ou voit des erreurs dans le code, je vous remercie de me le signaler.