I'm trying to build a login page by using hibernate,jsf,mysql and netbeans. I created my index page which includes forms and informations. Created a Login JSF Managed bean page and set the neccessary things like try..catch etc. Here is my Login.java
package common;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
@ManagedBean(name = "login_bean")
@SessionScoped
@Entity
public class Login implements Serializable {
@Id
private int sl_no;
private String user_name;
private String password;
public String getUser_name() {
return user_name;
}
public String getPassword() {
return password;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public void setPassword(String password) {
this.password = password;
}
public boolean checkuser(){
try {
System.out.println("user name" +user_name);
System.out.println("password" +password);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query= session.createQuery("from Login where user_name=:user_name and password=:password");
query.setString("user_name", user_name);
query.setString("password", password);
List list= query.list();
System.out.println("list size" +list.size());
if(list.size()==1){
return true;
}else{
return false;
}
} catch (Exception e) {
System.out.println(e);
}
return false;
}
public Login() {
}
}
Also connected my database with netbeans so users can be taken from my database. It works correct. I want to add a feature which gives an error mesage when id/pw is wrong. I want to do it in my faces-config.xml because i am navigating the logins from this page. Tried to do it but couldn't fit any code to my xml page. There were some examples and similar problems but they all wrote in php or java. Here is my faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<navigation-rule>
<from-view-id>index.xhtml</from-view-id>
<navigation-case>
<from-action>#{login_bean.checkuser()}</from-action>
<from-outcome>true</from-outcome>
<to-view-id>loginSucc.xhtml</to-view-id>
<redirect></redirect>
</navigation-case>
<navigation-case>
<from-action>#{login_bean.checkuser()}</from-action>
<from-outcome>false</from-outcome>
<to-view-id>index.xhtml</to-view-id>
<redirect></redirect>
</navigation-case>
</navigation-rule>
</faces-config>
After redirect, i want to display an message just like "fail" and keep index.xhtml working on. I am open to any suggestions. My xml knowledge is not enough for this and could not find any example on xml.