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
|
public class ModificationAction extends DispatchAction{
/**
* afficher la page de creation/modification de contact selon la valeur de idContactAff
*/
public ActionForward afficher(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
AfficherContactForm acf = (AfficherContactForm)form;
ContactForm cf = new ContactForm ();
// tester la valeur de idContactAff pour differencier la creation et la modification de contact
if (acf.getIdContactAff () != 0){// afficher le formulaire de modification de contact
Contact contact = ContactDAO.loadSingleContact (acf.getIdContactAff ());
cf.loadWithContact (contact);
request.setAttribute ("contactForm", cf);
} else {// afficher le formulaire de creation de contact
// alimentation du bean depuis la bdd
List<Category> allCategories = CategoryDAO.loadAllCategories();
request.getSession().setAttribute("categories", allCategories);
//remplir les attributs actif et npai de contactForm
ContactForm cfn = new ContactForm();
cfn.setActif ("actif");
cfn.setNpai ("npai");
request.setAttribute ("contactForm", cfn);
}
return mapping.findForward ("creation");
}
/**
* Valider la creation/modification de contact selon la valeur de idContact
*/
public ActionForward valider(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
ContactForm cf = (ContactForm)form;
int idContact = 0;
//récupérer le contact a créer depuis le formulaire
Contact contact = cf.getContact ();
if(cf.getIdContact () == 0){// il s'agit d'une creation de contact
ContactDAO.updateContact (contact);
//recuperer l'idContact de contact inséré dans la bdd
idContact = ContactDAO.loadLastIndexInserted ();
}else{// il s'agit d'une modification de contact
contact.setId (cf.getIdContact ());
ContactDAO.modifyContact (contact);
idContact = cf.getIdContact ();
}
//associer l'idContact au formulaire d'affichage de contact
AfficherContactForm acf = new AfficherContactForm();
acf.setIdContactAff (idContact);
request.setAttribute ("afficherContactForm", acf);
return mapping.findForward ("affichage");
}
public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
return afficher(mapping, form, request, response);
}
} |
Partager