0

i m working on a login jsf and hibernate application , i have a problem, whan i click on login button nothing happens

<h:head></h:head>
<body>
 <h:form>
<h:outputText value="#{accountController.message}" escape="false">     </h:outputText>
  </h:form>
   <h:panelGrid columns ="2" cellpadding="2" cellpacing="2">

       <h:outputText value="Username"></h:outputText>
  <  h:inputText value="#{utilisateursController.utilisateurs.username}">   </h:inputText>

  <h:outputText value="Password"></h:outputText>
  <h:inputSecret value="#{utilisateursController.utilisateurs.password}"></h:inputSecret>

 <h:commandButton value = "login" action="#{utilisateursController.login()}" ></h:commandButton>

UtilisateursController.java

package controller;

import javax.faces.bean.*;
import entities.*;
import model.UtilisateursModel;

  import java.util.*;

 @ManagedBean(name="UtilisateursController")
 @SessionScoped
   public class UtilisateursController {

    private UtilisateursModel utilisateursModel = new UtilisateursModel();
    private Utilisateurs utilisateurs = new Utilisateurs();
   private String message = "";



     public String getMessage() {
    return message; 
 }
    public void setMessage(String message) {
    this.message = message;
  }
     public Utilisateurs getUtilisateurs() {
    return utilisateurs;
  }
         public void setUtilisateurs(Utilisateurs utilisateurs) {
      this.utilisateurs = utilisateurs; 
   }

      public String login(){
       if(utilisateursModel.login(this.utilisateurs.getUsername(),
            this.utilisateurs.getPassword())!=null){

         return "welcome";
        } else {
       this.message = "Utilisateur invalid";
        this.utilisateurs = new Utilisateurs();
       return "login"; 
         }
          } 
           }

UtilisateursModel.java

    package model;

      import org.hibernate.Query;
   import org.hibernate.resource.transaction.spi.TransactionStatus;

     import entities.*; 

   public class UtilisateursModel extends AbstractModel<Utilisateurs>{

     public UtilisateursModel(){
         super(Utilisateurs.class);
       }


         public Utilisateurs login(String username, String password){
           try{

         if (!(sessionFactory.getCurrentSession().getTransaction().getStatus() != TransactionStatus.ACTIVE) )
      sessionFactory.getCurrentSession().getTransaction().begin();


            Query query = sessionFactory
                .getCurrentSession()
                .createQuery(
           "from utilisateurs where username=:username and      password=:password");
            query.setString("username", username);  
            query.setString("password", password);  
           return (Utilisateurs)query.uniqueResult();
          } catch(Exception e){
            return null;
        }

      } 
     }

thank you for your help , i really don t see anything wrong on the code

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
usra
  • 3
  • 4
  • put your commandButton inside `` .action will fire when button inside form tag. – Subodh Joshi Apr 04 '16 at 05:12
  • Welcome at Stack Overflow! Before posting a future question about JSF please make sure you have read http://stackoverflow.com/tags/jsf/info. The current question in its current form contains a lot of irrelevant code not contributing to the real problem. Try working on that. – BalusC Apr 04 '16 at 07:24

2 Answers2

-1

You cannot execute an action when the command button is outside a form. Change your code to the following:

<h:form>
<h:outputText value="#{accountController.message}" escape="false">       </h:outputText>
<h:panelGrid columns ="2" cellpadding="2" cellpacing="2">
   <h:outputText value="Username"></h:outputText>
   <h:inputText value="#{utilisateursController.utilisateurs.username}"/>
   <h:outputText value="Password"/>
   <h:inputSecret value="#{utilisateursController.utilisateurs.password}"/>
   <h:commandButton value = "login" action="#{utilisateursController.login()}" />
</h:panelGrid>
</h:form>

Hope this works out for you!

ArnoldKem
  • 100
  • 2
-1

You need to change some code as per following

  1. You need to put all element inside form body like all inputs and submit button
  2. You need to change your manage bean name utilisateursController instead of UtilisateursController

I means

@ManagedBean(name="utilisateursController") instead of @ManagedBean(name="UtilisateursController")

Hope you will fix your problem!

  • thank you , i fixed the login button problem , but when i execute even when i put the correct login and password ,i have always "Account's Invalid" message !! – usra Apr 04 '16 at 09:08
  • please debug your code and check with managebean login method you got right user name and password and add **trim()** with username and password while you check with database. – Ravi Kavaiya Apr 04 '16 at 09:15
  • can you explain more, the connection with the database is ok , the login code too – usra Apr 04 '16 at 12:16
  • i wrote the code again and cheked it again and again , the same problem – usra Apr 04 '16 at 19:53