Bonjour,
Je cherche à insérer un enregistrement dans une base de données (c'est le but premier).
Ce que j'ai :
1. Sur une des pages j'ai ce lien :
<html:link href="AjoutClient.do">Ajouter un Client</html:link>
2. Dans mon fichier struts-config.html j'ai :
1 2 3 4 5 6 7 8 9 10 11 12 13
|
<form-bean name="ajoutClientForm" type="gescom_pkg.AjoutClientForm" />
<action
path="/AjoutClient"
type="gescom_pkg.AjoutClientAction"
name="ajoutClientForm"
input="/pages/AjoutClient.jsp"
validate="true">
<forward name="success" path="/pages/AjoutClient.jsp" redirect="false"/>
<forward name="failure" path="/pages/AjoutClient.jsp" />
</action> |
3. Dans mon fichier AjoutClientForm.java j'ai :
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
| package gescom_pkg;
import org.apache.struts.action.*;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
public class AjoutClientForm extends ActionForm
{
static final long serialVersionUID=1;
private String intitule;
private String desc_fonc;
private String commentaires;
private String axe_dev;
private String nom_liste;
private ArrayList<String> activite;
public String getNom_liste()
{return nom_liste; }
public void setNom_liste(String nom_liste)
{this.nom_liste = nom_liste; }
public String getIntitule()
{return intitule; }
public void setIntitule(String intitule)
{this.intitule = intitule; }
public String getDesc_fonc()
{return desc_fonc; }
public void setDesc_fonc(String desc_fonc)
{this.desc_fonc = desc_fonc; }
public String getCommentaires()
{return commentaires; }
public void setCommentaires(String commentaires)
{this.commentaires = commentaires; }
public String getAxe_dev()
{return axe_dev; }
public void setAxe_dev(String axe_dev)
{this.axe_dev = axe_dev; }
public ArrayList<String> getActivite()
{return activite; }
public void setActivite(ArrayList<String> activite)
{this.activite = activite; }
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors errors = new ActionErrors();
return errors;
}
} |
4.Fichier AjoutClientAction.java
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
| package gescom_pkg;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.ArrayList;
public class AjoutClientAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest req, HttpServletResponse res)
throws Exception
{
AjoutClientForm ajoutClientForm = (AjoutClientForm) form;
ActionErrors errors = new ActionErrors();
ActionError newError = null;
ArrayList<String> liste = new ArrayList<String>();
try
{
// Ajout à la base de données
ConnexionBDD connec=new ConnexionBDD();
connec.DBConnexion();
String s="SELECT * from activite;";
ResultSet rs=connec.getResultat(s);
while(rs.next())
{
liste.add(rs.getString("ACT_LIB"));
}
liste.add("Autre ...");
ajoutClientForm.setAxe_dev("");
ajoutClientForm.setIntitule("");
ajoutClientForm.setCommentaires("");
ajoutClientForm.setDesc_fonc("");
ajoutClientForm.setActivite(liste);
//System.out.println(ajoutClientForm.getActivite());
ajoutClientForm.setNom_liste("liste_activite");
connec.BDDeconnexion();
return mapping.findForward("success");
}
catch (Exception e)
{
System.out.println(e);
newError = new ActionError("errors.db.not.found");
errors.add(ActionErrors.GLOBAL_ERROR, newError);
saveErrors(req, errors);
return mapping.findForward("failure");
}
}
} |
5. Fichier AjoutClient.jsp :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <html:form action="/AjoutClient">
<font color="red"><html:errors /></font>
<table width=100% border=0>
<tr height=20><td width=50%>Intitulé : </td><td><html:text property="intitule" /></td></tr>
<tr height=20><td>Descriptif fonctionnel : </td><td><html:textarea property="desc_fonc"></html:textarea></td></tr>
<tr height=20><td>Commentaires : </td><td><input type=textarea property="commentaires"></textarea></td></tr>
<tr height=20><td>Axes de développements : </td><td><input type=textarea property="ave_dev"></textarea></td></tr>
<tr height=20><td>Activité : </td><td>
<html:select property="nom_liste">
<html:options property="activite"/>
</html:select>
</td></tr>
<tr height=20><td><html:submit value="VALIDER"/></html:form>
<td>..... |
Première question :
Jusque là, pas de problème. Lorsque je clique sur le lien ajouter un client, je suis redirigé vers la page ajoutClient.jsp avec le select du formulaire rempli avec les valeurs de la base de données. Je cherche ensuite à ajouter ce que je vais saisir dans le formulaire dans la base de données mais je n'y arrive pas.
J'ai essayé en changeant l'action du formulaire en mettant une autre action du style ValiderAjoutClient (avec la classe Form et Action derrière qui récupère les valeurs pour les ajouter à la base) mais ça me mets comme erreur que activite a une valeur nulle.
Deuxième question :
J'aimerais savoir si il y a la possibilité de définir les propriétés value d'un select. Je m'explique, lorsque mon Action rempli la liste des activités, pour chaque <option> le champs value est égal à la chaîne affichée. J'aimerai savoir si je peux, au moment de la définition de la liste, affecter une autre valeur à ce champ value ?
merci d'avance
Shiv@
Partager