Bonjour,

Je suis entrain d'apprendre JSF et primefaces en créant une petite application. L'application doit géré les catégories en faisant l'enregistrement, modification et suppression.

Mais je constate après avoir cliquer sur le bouton enregistrer, l'information est insérée dans la base des données mais les champs ne réinitialise pas dans le formulaire, les anciennes données restent toujours visible.

Voici les codes source:

1. Formulaire

Code XML : 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
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
		xmlns:h="http://java.sun.com/jsf/html"
		xmlns:f="http://java.sun.com/jsf/core"
		xmlns:p="http://primefaces.org/ui"
		template="/resources/templates/generalTemplate.xhtml">
    <h:head>
        <title>TODO supply a title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
         <!-- Bootstrap Core CSS -->
        <link href="/photo/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
 
    </h:head>
    <h:body>
        <div class="panel panel-primary">
            <div class="panel-heading">					
                NOUVELLE CATEGORIE					
            </div>
            <div class="panel-body">
               <h:form> 
                     <!--affichage message d'erreur-->
                     <p:growl id="msg" showDetail="false"/>
                    <div class="form-group">
                        <h:outputLabel  value="Codecat " for ="codecat" class="control-label"/>
                        <p:inputText value="#{categorieBean.codecat}" id="codecat" class="form-control"/>
 
                    </div>
                    <div class="form-group">
                        <h:outputLabel value="Désignation" for="designcat" class="control-label"/>
                        <p:inputText value="#{categorieBean.designcat}" id="designcat" class="form-control"/>
                    </div>
                     <p:commandButton value="valider" action="#{categorieBean.enregistrer}" update="msg" />
 
               </h:form>
            </div><!--fin div panel body-->
        </div> <!--fin div panel panel primary-->   
 
    </h:body>
</html>


2. ManagedBean

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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation;
 
import entites.Categorie;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import services.CategorieImpService;
import services.ICategorieService;
 
/**
 *
 * @author ERIWANG
 */
@ManagedBean(name="categorieBean")
@RequestScoped
public class CategorieBean {
    private String codecat;
    private String designcat;
 
    //délcaration des objets du type liste des categories
    private List<Categorie>categorieList;
    //liaison couche service et presentation
    private ICategorieService services=new CategorieImpService();
 
    public CategorieBean() {
    }
 
    @PostConstruct
    public void initBean(){
        System.out.println("PostConstruct");
        //chargement de la liste
        categorieList=services.listCategorie();
 
    }
 
 
    public String getCodecat() {
        return codecat;
    }
 
    public void setCodecat(String codecat) {
        this.codecat = codecat;
    }
 
    public String getDesigncat() {
        return designcat;
    }
 
    public void setDesigncat(String designcat) {
        this.designcat = designcat;
    }
 
    public List<Categorie> getCategorieList() {
        return categorieList;
    }
 
    public void setCategorieList(List<Categorie> categorieList) {
        this.categorieList = categorieList;
    }
 
    public ICategorieService getServices() {
        return services;
    }
 
    public void setServices(ICategorieService services) {
        this.services = services;
    }
 
    public String enregistrer() {
       System.out.println("nous sommes dans enregistrer");
       System.out.println("code cat:"+codecat+" - design:"+designcat);
        if(codecat.equals("") || designcat.equals("")){
 
            FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage("Aucun champ vide n'est autorisé!"));
            RequestContext.getCurrentInstance().update("administration:msg");
 
        }else{
 
            //creation un objet categorie
            Categorie ca=new Categorie();
            ca.setCodecat(codecat);
            ca.setDesigncat(designcat);
 
            String reussie=services.creerCategorie(ca);
 
            System.out.println("reponse:"+reussie);
            if(reussie.equalsIgnoreCase("reussie")){
                //message de confirmation
 
                FacesContext.getCurrentInstance().addMessage(null,
                     new FacesMessage("Opération réussie avec succès."));
            }
 
            if(reussie.equalsIgnoreCase("doublons")){
                FacesContext.getCurrentInstance().addMessage(null,
                     new FacesMessage("Duplication des données")); 
            }           
            codecat="";
            designcat=""; 
        }         
 
       return null;
    }
 
 
    public String index(){
       return "index?faces-redirect=true";
    }
 
 
 
}

Nom : categorie_.png
Affichages : 318
Taille : 21,8 Ko


Svp pouvez vous m'aider pour qu'après enregistrement les champs du formulaire soit vide.