0

I have a JSF project, and I am trying to do a login page, in my project I have a managed bean that has a validate method for the username and password, and I have a bean class with setters and getters which has the user info that get filled for a database eg.(username, password, isActive, Full name), my question is how can I call the user info in JSF el expression in my xhtml pages if they are not in the managed bean?

Here is my java bean:

@Table(name="students_info")
public class User {
    @Column(name="std_record_id")
    private int id;

    @Column(name="std_id")
    private String userId;

    @Column(name="first_name")
    private String firstName;

    @Column(name="web_password")
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

And here is my MBLogin:

    @ManagedBean
    @SessionScoped
    public class MBLogin {

        User user = new User();
        LoginDAO loginDao = new LoginDAO();

        public String validteStudent() {
            boolean valid = loginDao.validateStudent(user.getUserId(), user.getUserId());
            if (valid) {
                HttpSession session = SessionUtils.getSession();
                session.setAttribute("username", user);
                return "student";
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                        "Incorrect Username and Passowrd", "Please enter correct username and Password"));
                return "login";
            }
        }
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Khaled
  • 55
  • 1
  • 9

1 Answers1

1

Add a getter for the user:

@ManagedBean
    @SessionScoped
    public class MBLogin {

        User user = new User();
        LoginDAO loginDao = new LoginDAO();

        public String validteStudent() {
            boolean valid = loginDao.validateStudent(user.getUserId(), user.getUserId());
            if (valid) {
                HttpSession session = SessionUtils.getSession();
                session.setAttribute("username", user);
                return "student";
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                        "Incorrect Username and Passowrd", "Please enter correct username and Password"));
                return "login";
            }
        }
        public User getUser() {
            return user;
        }
    }

Then in your xhtml you can call it like this:
#{user.id}, #{user.firstName}

Luud van Keulen
  • 1,204
  • 12
  • 38
  • Thanks sir that worked fine :D – Khaled Feb 27 '17 at 11:17
  • that worked for my getters but now my setters are not working is there any way to fix them? – Khaled Feb 27 '17 at 11:35
  • Just add a setter to the managedbean. – Luud van Keulen Feb 27 '17 at 12:01
  • Thanks sir the only thing left now is #{user.firstName} is not priniting any thing and I am sure that it has a value as Its printing out using System.out.println(user.getFirstName()); in my dao class can you suggest why is that happening I would really appreciate it. – Khaled Feb 27 '17 at 12:26