owszem nie mam, była tylko w projekcie "wzorcowym" (który dostaliśmy na ćwiczeniach)
EDIT: Problem rozwiązany. Powodem był sam Eclipse(cache). Niestety pojawił się kolejny - nie działa mi logowanie tzn cokolwiek wpiszę program wyłapuje jako null.
package com.jsfcourse.security;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
//import java.util.Date;
import com.jsf.dao.GraczDAO;
import com.jsf.entities.Gracz;
@ManagedBean
public class LoginBB {
private static final String PAGE_MAIN = "index.jsf";
private static final String PAGE_LOGIN = "gracz.jsf?faces-redirect=true";
private static final String PAGE_STAY_AT_THE_SAME = null;
@EJB
GraczDAO graczDAO;
private String nazwa;
private String haslo;
public String getNazwa() {
return nazwa;
}
public void setNazwa(String nazwa) {
this.nazwa = nazwa;
}
public String getHaslo() {
return haslo;
}
public void setHaslo(String haslo) {
this.haslo = haslo;
}
public boolean validateData() {
boolean result;
FacesContext ctx = FacesContext.getCurrentInstance();
// check if not empty
if (nazwa == null || nazwa.length() == 0) {
ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Musisz podać nazwę gracza", "null"));
}
if (haslo == null || haslo.length() == 0) {
ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Musisz podać hasło ", "null"));
}
if (ctx.getMessageList().isEmpty()) {
result = true;
} else {
result = false;
}
return result;
}
public String doLogin() {
FacesContext ctx = FacesContext.getCurrentInstance();
Gracz gracz = null;
// 1. check parameters and stay if errors
if (!validateData()) {
return PAGE_STAY_AT_THE_SAME;
}
// 2. verify login and password - get User from "database"
gracz = getUserFromDatabase(nazwa, haslo);
// 3. if bad login or password - stay with error info
if (gracz == null) {
ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Niepoprawny login lub hasło", null));
return PAGE_STAY_AT_THE_SAME;
}
// 4. if login ok - save User object in session
HttpSession session = (HttpSession) ctx.getExternalContext()
.getSession(true);
session.setAttribute("Gracz", gracz);
// and enter the system
return PAGE_LOGIN;
}
public Gracz getGracz() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(true);
return (Gracz) session.getAttribute("Gracz");
}
public String doLogout(){
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(true);
//Invalidate session
// - all objects within session will be destroyed
// - new session will be created (with new ID)
session.invalidate();
return PAGE_MAIN;
}
// simulate finding user in database
private Gracz getUserFromDatabase(String nazwa, String haslo) {
Gracz g = null;
if (nazwa.equals("nazwa") && haslo.equals("haslo")) {
g = new Gracz(nazwa, haslo);
g.setNazwa("nazwa");
// g.setOstatniWynik("#{gracz.ostatniWynik}");
// assumed system roles
/* g.getRoles().add("role1");
g.getRoles().add("role2");
g.getRoles().add("admin");*/
}
return g;
}
}