📄 login.java
字号:
package com.jsfcompref.trainer.backing;
import com.jsfcompref.trainer.UserBean;
import com.jsfcompref.trainer.UserRegistry;
import com.jsfcompref.trainer.util.UserUtil;
import com.jsfcompref.trainer.util.JSFUtil;
import java.io.IOException;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Backing bean for login.jsp
*/
public class Login {
private HtmlInputText userid;
private HtmlInputSecret password;
private final static String AUTH_USER = "Authorized_User";
public Login() {
}
public String login() {
//Grab UserRegistry Managed Bean
UserRegistry userRegCopy =
(UserRegistry)JSFUtil.getManagedObject("UserRegistry");
// Call findByUserCredentials method
// and retrieve currentUser which matches credentials
UserBean currentUser =
userRegCopy.findUserByCredentials(userid.getValue().toString(), password.getValue().toString());
if (currentUser == null) {
// login failed
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage("Invalid Login!"));
return "failure";
} else {
// login success
UserBean managedUserBean =
(UserBean)JSFUtil.getManagedObject("UserBean");
UserUtil.copyUserProperties(currentUser, managedUserBean);
managedUserBean.setIsLoggedIn(true);
// Place authorized user on session to disable security filter
JSFUtil.storeOnSession(FacesContext.getCurrentInstance(), AUTH_USER, "Authorized_User");
return "success";
}
}
public String logout() throws IOException{
ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
HttpSession session = (HttpSession)ectx.getSession(false);
session.invalidate();
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
app.getNavigationHandler().handleNavigation(ctx, "/welcome.jsp", "welcome");
// To avoid using the navigation handler you could also use...
//response.sendRedirect("../index.jsp");
return null;
}
public void setUserid(HtmlInputText userid) {
this.userid = userid;
}
public HtmlInputText getUserid() {
return userid;
}
public void setPassword(HtmlInputSecret password) {
this.password = password;
}
public HtmlInputSecret getPassword() {
return password;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -