package org.brain.bean;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpSession;
import org.brain.dbo.DBOperate;
import org.brain.pojos.User;
@ManagedBean(name="authenticationBean")
@SessionScoped
public class authenticationBean {
private String username;
private String password;
private String usercareer;
private Date userregtime;
private String message="";
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setUsercareer(String usercareer) {
this.usercareer = usercareer;
}
public String getUsercareer() {
return usercareer;
}
public void setUserregtime(Date userregtime) {
this.userregtime = userregtime;
}
public Date getUserregtime() {
return userregtime;
}
public void validateInput(FacesContext context,
UIComponent component, Object value){
String text = value.toString();
if(text.equals("")){
throw new ValidatorException(new FacesMessage("内容不能为空!"));
}
}
public String login(){
List list = DBOperate.search("User where username='"+username+"' and password='"+password+"'");
if(!list.isEmpty()){
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext extContext = facesContext.getExternalContext();
extContext.getSession(true);
User user = (User) list.get(0);
extContext.getSessionMap().put("user",user);
return "welcome?faces-redirect=true";
}
else{
message="用户名或密码错误!";
return "login";
}
}
public String logout(){
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) facesContext
.getExternalContext().getSession(false);
session.invalidate();
return "login?faces-redirect=true";
}
public void reset(){
this.username = "";
this.password = "";
}
}