0

login authentication

asked 2013-06-12 11:54:04 +0800

santoshkarna gravatar image santoshkarna
13 2

updated 2013-06-12 14:12:45 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

I am creating simple login form using ZK & Hibernate.I want user can login only if username and password exists in the database.I am trying to use Hibernate criteria api.I stuck at writing logic for login method. please help me?

My LoginController.java is as:

package com.petlist.santosh.controller;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Button;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.petlist.santosh.dao.User;
import com.petlist.santosh.util.HibernateUtil;

public class LoginController extends SelectorComposer<Window> {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Wire
    Textbox userName;   
    @Wire
    Textbox passWord;   
    @Wire
    Button loginButton ;

    @Override
       public void doAfterCompose(Window comp) throws Exception{
         super.doAfterCompose(comp);
    }




      @Listen("onClick= #loginButton")
      public void login(){

        Session session =   HibernateUtil.getSessionFactory().openSession();        
        Transaction tx  =   session.beginTransaction();
                //this is from login page       
        String uname=userName.getValue();
        String pword=passWord.getValue();

        Criteria criteria= session.createCriteria(User.class)
                 .add(Restrictions.eq("username", uname))
                .add(Restrictions.eq("password",pword));
        //stucked here
        if(user != null){
            Executions.getCurrent().sendRedirect("/index.zul");
            Messagebox.show("successful");      
        }
        else{
            Messagebox.show("please sign up new user");
        }


             tx.commit();
          session.close();  
      }




}
delete flag offensive retag edit

Comments

The solution provided by cor3000 worked for me.Thank you very much Mr cor3000. I used List<User> users = (List<User>)criteria.list(); User user = users.size() == 1 ? users.get(0) : null;

santoshkarna ( 2013-06-13 11:51:04 +0800 )edit

5 Answers

Sort by ยป oldest newest most voted
0

answered 2013-06-13 04:03:00 +0800

cor3000 gravatar image cor3000
4406 2 7
ZK Team

I hope I am not missing the obvious here. provided your data tables and entities exist you'll have to send a query to the DB to retrieve the result. (hibernate docu)

User user = (User)criteria.uniqueResult();

or

List<User> users = (List<User>)criteria.list();
User user = users.size() == 1 ? users.get(0) : null;
link publish delete flag offensive edit
0

answered 2013-06-12 13:22:38 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2615 3 8
http://emrpms.blogspot.in...

See here

See here

link publish delete flag offensive edit
0

answered 2013-06-12 22:57:36 +0800

dalvarez gravatar image dalvarez
1

1234567890

link publish delete flag offensive edit
0

answered 2015-12-09 09:46:27 +0800

SachinChavan gravatar image SachinChavan
1

after that you write following two lines of code, your program worked.

criteria.setProjection(Projections.rowCount()); int c= (Integer) criteria.uniqueResult(); if(c!=0) { //valid user }else { //invalid user }

link publish delete flag offensive edit
0

answered 2015-11-25 08:19:24 +0800

smileinheavenn gravatar image smileinheavenn
1

i thing u use MVVM.it's done

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
3 followers

RSS

Stats

Asked: 2013-06-12 11:54:04 +0800

Seen: 262 times

Last updated: Dec 09 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More