Search code examples
javadatabasejsf-2attributesmanaged-bean

setting attribute value to String in managed Bean JSF 2.2


I have an attribute called chemin in my managed bean, which I want to set It's value to a String ( the attribute value should be stored in database, and not coming from a form), but I don't know how to do that. the attribute chemin is inside a method that upload a file and store it's relatives info in data base such as date, document title, description, and path to the uploaded file ( the file is stored in a directory). this is the code doing that : managed bean :

public String upload() throws IOException{
    file.write(getFilename(file));
    docDAO.createDoc(idDocument, titreDocument, descriptionDocument, sousCategorie, Categorie, chemin, dateMise, lien);
    idDocument=null;
    titreDocument="";
    descriptionDocument="";
    lien="";
    sousCategorie="";
    dateMise="";
    Categorie="";
    chemin="C:\\data\\" +getFilename(file);
}

DAO class :

public void createDoc(Integer idDocument, String titreDocument, 
            String descriptionDocument, String sousCategorie
            , String Categorie, String chemin, String dateMise, String lien) {

        em.getTransaction().begin();
        Document f =new Document();
        f.setIdDocument(idDocument);
        f.setDateMise(dateMise);
        f.setDescriptionDocument(descriptionDocument);
        f.setLien(lien);
        f.setChemin(chemin);
        f.setTitreDocument(titreDocument);
        f.setCategorie(Categorie);
        f.setSousCategorie(sousCategorie);  
        em.persist(f);
        em.getTransaction().commit();

    }

the xhtml page :

<h:form>
        <h:outputText  value="chemin"></h:outputText>
        <h:inputHidden value="#{docBean.chemin}"></h:inputHidden><br/> 
        <!--  <c:set value="mobil" target="#{docBean}" property="chemin" /> -->
        <h:outputText  value="Date de la mise "></h:outputText>
        <h:inputText value="#{docBean.dateMise}"></h:inputText><br/>
        <h:outputText  value="Lien"></h:outputText>
        <h:inputText value="#{docBean.lien}"></h:inputText><br/>
        <h:outputText  value="Catégorie"></h:outputText>
        <h:inputText value="#{docBean.categorie}"></h:inputText><br/>
        <h:outputText  value="sous catégorie"> </h:outputText>
        <h:inputText value="#{docBean.sousCategorie}"></h:inputText><br/>
        <h:outputText> télécharger le document</h:outputText>
        <h:inputFile value="#{docBean.file}"></h:inputFile><br/>
        <h:commandButton value="valider" action="#{docBean.upload}"/>
    </h:form>

any idea could help.


Solution

  • First , you have to remove <h:inputHidden value="#{docBean.chemin}"></h:inputHidden> from your xhtml page, and in your upload method give to chemin the desired value before calling docDAO.createDoc() so that you can save it in your database.