I am trying in my application to get the id of the current user when he create a document to register that document with the id of the user. But when using <f:viewParam> the user id in the doument bean is allways null.

Here is my code :

         <h:form id="form">
            <br></br>

            <div class="centre">
                <p:graphicImage url="images/usericon.png" id="usericon"
                    alt="utilisateur IsetN" style="width:100px;height:100px"></p:graphicImage>
            </div>
            <br></br>

            <div class="centre">
                <p:inputText placeholder="E-mail" required="true" id="email"
                    value="#{userBean.email}" requiredMessage="E-mail obligatoire">
                     <f:validateRegex pattern="[\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]" />
                    </p:inputText>
                    <p:message for="email"></p:message>
            </div>
            <br></br>
            <div class="centre">
                <p:password id="motdepasse" value="#{userBean.password}" required="true"
                    placeholder="Mot de passe" requiredMessage="Mot de passe Obligatoire">

                    </p:password>
                    <p:message for="motdepasse"></p:message>
            </div>
            <br></br>
            <div class="centre">
                <h:commandButton id="login" value="Connexion" action="#{userBean.login}" update="form" >
                <f:param name="IdUser" value="#{userBean.cin}"  >
                            </f:param> 
                </h:commandButton>
                <p:message for="login"></p:message>
            </div>
            <br></br>
            <div class="centre">
                <a href="AideAdmin.xhtml">Compte n'est pas accessible ?</a><br></br>
            </div>
            <div class="centre">
                <a href="authentification.xhtml">Connexion premier fois ?</a><br></br>
            </div>

        </h:form>

and this is the destination page

                     !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
       <f:view contentType="text/html">
<h:head>


    <link rel="stylesheet" type="text/css" href="css/styles.css" />
    <title>Profil: Portail IsetN</title>
    <link rel="stylesheet" href="css/style.css" media="screen" />
</h:head>
<f:metadata>
        <f:viewParam name="IdUser" value="#{userBean.cin}"
            converter="javax.faces.Long" />
        <f:viewParam name="IdUser" value="#{documentBean.cin}"
            converter="javax.faces.Long" />
    </f:metadata>
<h:body>

and this is the managedBeans code

                 @ManagedBean(name="userBean")
           @SessionScoped
          public class UtilisateurBean implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Utilisateur newUtilisateur = new Utilisateur();
private List<SelectItem> etudsItem;
private DataModel utilisateurs;
private Utilisateur Utilisateur=new Utilisateur();
private Utilisateur editUtilisateur;
private Long cin;
private String nom;
private String prenom;
private String email;
private String password;

private DaoUtilisateur eDao = new DaoUtilisateur();

public String login(){
    try{
Utilisateur= eDao.findUtilisateurByEmail(email, password)   ;

if (Utilisateur==null){
    FacesMessage msg = new FacesMessage("Mot de passe ou email non valide");
    FacesContext.getCurrentInstance().addMessage(null, msg);
        return "loginfailed";
    }

else{
    cin=Utilisateur.getCin();
    System.out.println("cin connecté "+cin);
    return"loginUtilisateur";

}
    }
         catch(Exception ex){ return"loginfailed";} 
}

and this is the document Bean code

                 @ManagedBean(name="documentBean")
          @SessionScoped
          public class DocumentBean implements java.io.Serializable{

private static final long serialVersionUID = 1L;
private List<SelectItem> docItems;
private DataModel documents;
private List<Document> tps;
private List<Document> tds;
private List<Document> annonces;
private Document newDocument= new Document();
private Document editDocument;
private Long cin;
        public String create(){
    newDocument.setDateajout(new Date());
    System.out.println("your cin annonce  "+cin);
    dDao.ajouter(newDocument,cin);
    newDocument= new Document();
    documents.setWrappedData(dDao.selectAll());
    return null;
}

I want to know waht i am missing i updated mojarra, changed the namespaces, changed the beans scopes but still it's not working.

有帮助吗?

解决方案

I suppose, this is your supposed workflow:

  1. user enters E-Mail/Password into input-fields, clicks the button
  2. server retrievals the correct user and fills the user.cin field
  3. server forwards user to the destination page

under this asumptions you don't have a technical but a logic problem:

  1. you load the login-page and write the (in this moment undefined) user-cin as http request parameter
  2. you do the login and send the http request parameter IdUser (which is null, see 1.) parallel to the login action
  3. via <f:viewParam /> you take the null-parameter and inject it into the documentBean.

you'll have to choose a different solution, I guess. Hope it helps...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top