Bonjour,
Je souhaite mettre en place un simple formulaire qui affiche le détail d'une entreprise, puis permettre la modification des informations et enregistrer les modifs dans ma base.
Mon appli web a la config suivante: Java 1.6, Struts 2, Tiles 2, Hibernate, MySQL, JBoss.
J'ai donc une JSP avec les champs et les boutons modifier/enregistrer.
Deux actions: une pour afficher le formulaire, et une pour enregistrer les infos modifiées.
Le bouton modification passe les champs de la JSP à readonly=false en javascript.
J'utilise l'identifiant de l'entreprise comme paramètre pour afficher les détails.
Il est aussi affiché dans la JSP.
L'action showEnt m'affiche tout impec.
Le pb, c'est que qd je clique sur Enregistrer mon formulaire, tout les champs sont rapattriés dans l'action saveEnt sauf l'ID
Je vous donne mes bouts de code:
Entreprise_Fiche.jsp
Entreprise_showAction.java
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
52
53
54
55
56
57
58
59
60
61
62 <%-- Corps de la fiche entreprise --%> <tags:form action="saveEnt" theme="simple"> <div id="searchbar"> <table> <tr class="even"> <%-- Nom de l'entreprise --%> <td class="redtitle"><b><tags:text name="newent.fields.name"/>*</b></td> <td class="redbox"><b><tags:textfield id="box1" cssClass="textlarge" name="nom"/></b></td> <%-- Identifiant de l'entreprise --%> <td class="redtitle"><b><tags:text name="rechent.fields.id"/></b></td> <td class="redbox"><b><tags:property value="ident"/></b></td> <tags:hidden value="id" name="id"/> </tr> <tr class="odd"> <%-- Adresse de l'entreprise --%> <td class="title"><b><tags:text name="newent.fields.adr"/></b></td> <td class="box"><tags:textfield id="box2" cssClass="textlarge" name="adresse"/></td> <%-- Site web de l'entreprise --%> <td class="title"><b><tags:text name="newent.fields.web"/></b></td> <td class="box"><tags:textfield id="box3" cssClass="textlarge" name="web"/></td> </tr> <tr class="even"> <%-- Code postal de l'entreprise --%> <td class="title"><b><tags:text name="newent.fields.cp"/></b></td> <td class="box"><tags:textfield id="box4" cssClass="textlarge" name="cp"/></td> <%-- Département de travail dans Airbus de l'entreprise --%> <td class="title"><b><tags:text name="newent.fields.dpt"/></b></td> <td class="box"><tags:textfield id="box5" cssClass="textlarge" name="dpt"/></td> </tr> <tr class="odd"> <%-- Ville de l'entreprise --%> <td class="title"><b><tags:text name="newent.fields.city"/>*</b></td> <td class="box"><tags:textfield id="box6" cssClass="textlarge" name="ville"/></td> <%-- Type de facturation de l'entreprise --%> <td class="title"><b><tags:text name="newent.fields.autofact"/>*</b></td> <td class="box"><tags:select id="select1" label="Select Month" name="autoFact" list="#{'true':'Entreprise','false':'Organisme'}"/></td> </tr> <tr class="even"> <%-- Pays de l'entreprise --%> <td class="title"><b><tags:text name="newent.fields.country"/>*</b></td> <td class="box"><tags:textfield id="box7" cssClass="textlarge" name="pays"/></td> </tr> </table> <table> <tr class="odd"> <%-- Note sur le mode de facturation de l'entreprise --%> <td class="title"><b><tags:text name="newent.fields.notefact"/></b></td> <td width=85%><tags:textarea id="box8" cssClass="textarea" name="noteFact"/></td> </tr> <tr><td> </td></tr> </table> <table width="300px"> <%-- Option de la feuille --%> <tr class="even"> <td><input id="btnModif" type="button" class="modifyform" value="Modifier" onclick="javascript:undoReadOnly()"/> <tags:submit id="btnSave" cssClass="savedb" value="Enregistrer" /> <td><div id="error"><tags:actionerror/></div></td> </tr> </table> </div> </tags:form>
Entreprise_saveAction.java:
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
52
53
54 public class Entreprise_showAction extends ActionSupport { private String id=""; private String ident="" ; private String nom=""; private String web=""; private String adresse=""; private String cp=""; private String ville="Toulouse"; private String pays="France"; private String dpt=""; private boolean autoFact=true; private String noteFact=""; public String getId() { return id; } public void setId(String id) { this.id = id; } /* ...Tous les set et get... */ @Override public String execute() throws Exception { EntrepriseManager emRech = new EntrepriseManager() ; try { /* Effacement des messages d'erreurs précédent */ this.clearErrorsAndMessages(); /* Récupération de l'entreprise dans la base */ Entreprise ent = new Entreprise() ; ent = emRech.entrepriseById(this.getId()) ; this.setIdent(Integer.toString(ent.getId())); this.setAdresse(ent.getAdresse()); this.setAutoFact(ent.isAutoFact()); this.setCp(ent.getCodePostal()); this.setDpt(ent.getDpt()); this.setNom(ent.getNom()); this.setNoteFact(ent.getNoteFact()); this.setPays(ent.getPays()); this.setVille(ent.getVille()); this.setWeb(ent.getWeb()); return SUCCESS ; } catch (NumberFormatException nbrE) { addActionError("Erreur lors de l'affichage: l'identifiant doit etre renseigné."); return INPUT ; } catch (Exception e) { addActionError("Erreur lors de l'affichage: "+e.getMessage()); return INPUT ; } } }
struts.xml:
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 public class Entreprise_saveAction extends ActionSupport { private String ident=""; private String nom=""; private String web=""; private String adresse=""; private String cp=""; private String ville="Toulouse"; private String pays="France"; private String dpt=""; private boolean autoFact=true; private String noteFact=""; public String getIdent() { return ident; } public void setIdent(String ident) { this.ident = ident; } /* ... get et set ... */ @Override public String execute() throws Exception { try { this.clearErrorsAndMessages(); /* Instanciation de l'entreprise Manager */ EntrepriseManager emRech = new EntrepriseManager() ; /* Instanciation de la nouvelle entreprise */ Entreprise ent = new Entreprise(Integer.parseInt(this.ident), this.nom, this.adresse, this.cp, this.ville, this.pays, this.web, this.dpt, this.autoFact, this.noteFact) ; /* Enregistrement de l'entreprise dans la base */ emRech.updateEntreprise(ent); addActionError("Entreprise mise à jour avec succès."); return SUCCESS ; } catch (Exception e) { addActionError("Erreur lors de la mise à jour: "+e.getMessage()); return INPUT ; } } @Override public void validate() { if ( (getNom()==null || getNom().trim().equals(""))|| (getVille()==null || getVille().trim().equals(""))|| (getPays()==null || getPays().trim().equals(""))) { addActionError("Vous devez renseigner les champs obligatoires.") ; } } }
Pensez vous qu'il y ait une erreur dans mon code?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 <action name="saveEnt" class="com.formation.struts.actions.Entreprise_saveAction"> <result name="success" type="redirect-action"> <param name="actionName">showEnt</param> <param name="id">${id}</param> </result> <result name="input" type="tiles">site.contact.rechEntreprise</result> </action>
Ou bien un defaut d'analyse plutot, au niveau du fonctionnement de struts2.
J'ai un peu du mal au niveau des paramètres de l'action dans struts.xml.
Où struts va chercher ${id} ? Cela devrait correspondre à un paramètre dans mon action Entreprise_saveAction? ou dans la jsp?
Je suis un peu perdue dans tous ces framework etc^^
Merci d'avance si vous avez des idées.
Partager