can JSF Still data user login?
Hello all i have one problem between 2 class of JSF Manage bean. i have 2 class JSF Manage bean one class use for login, one use for user add/edit/delete user table and edit his information when his login.
my login class
import com.DAO.UserBean;
import com.entity.IUser;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
*
* @author MYE
*/
@ManagedBean
@SessionScoped
public class LoginController implements Serializable{
@EJB
private UserBean userBean;
private IUser user;
private boolean admin ;
private boolean mod ;
/** Creates a new instance of LoginController */
public LoginController() {
user = new IUser();
}
//getter / setter
public boolean isMod() {
return mod;
}
public void setMod(boolean mod) {
this.mod = mod;
}
public IUser getUser() {
return user;
}
public void setUser(IUser user) {
this.user = user;
}
public boolean isAdmin() {
return admin ;
}
public void setAdmin(boolean admin) {
this.admin = admin;
}
public String cplogin() {
if (userBean.userLogin(user.getUsername(), user.getPassword()) != null) {
if(user.getUsername() != null || user.getPassword() != null){
user = userBean.userLogin(user.getUsername(), user.getPassword());
if(user.getGroups().getAdmin() != null){
setAdmin(user.getGroups().getAdmin());
}
if(user.getGroups().getMods() != null){
setMod(user.getGroups().getMods());
}
if(isAdmin() == true || isMod() == true){
return "home";
}else{
return "login";
}
}else {
return "login";
}
} else {
return "login";
}
}
public String logout() {
user = null;
return "login";
}
public boolean isLoggedIn() {
return user != null;
}
}
and my user bean
import com.DAO.UserBean;
import com.entity.Groups;
import com.entity.IUser;
import com.entity.UserGender;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
/**
*
* @author Kency
*/
@ManagedBean
@ViewScoped
public class UserController implements Serializable{
@EJB
private UserBean userBean;
private IUser user;
private LoginController loginsController;
private Groups groups;
private List<SelectItem> enumGender = new ArrayList<SelectItem>();
private UserGender userGender;
public List<SelectItem> getEnumGender() {
return enumGender;
}
public void setEnumGender(List<SelectItem> enumGender) {
this.enumGender = enumGender;
}
public UserGender getUserGender() {
return userGender;
}
public void setUserGender(UserGender userGender) {
this.userGender = userGender;
}
/** Creates a new instance of UserController */
public UserController() {
user = new IUser();
loginsController = new LoginController();
System.out.println(loginsController.getUser());
for(UserGender iGender : UserGender.values()){
enumGender.add(new SelectItem(iGender, iGender.getLabel()));
}
}
@PostConstruct
public Groups construcGroup(){
return groups = new Groups(1, "Memmber");
}
public IUser getUser() {
return user;
}
public void setUser(IUser user) {
this.user = user;
}
public List<IUser> getUserList(){
return userBean.retrieveAllUser();
}
public void updateUser(){
user = userBean.updateUser(user);
}
public void deleteUser (IUser user){
userBean.deleteUser(user);
}
public void showUserdetails(IUser user){
this.user = user;
}
public IUser getUserDetails(){
return user;
}
public String addUser(){
if(loginsController.isAdmin() == false){
user.setGroups(construcGroup());
if(userBean.checkExistName(user.getUsername()) != null && userBean.checkExistEmail(user.getEmail()) != null
|| userBean.checkExistName(user.getUsername()) != null || userBean.checkExistEmail(user.getEmail()) != null){
checkUserEmail("username or email has exist");
}else{
user = userBean.addUser(user);
addMessage("add success");
return "home";
}
}else {
if(userBean.checkExistName(user.getUsername()) != null && userBean.checkExistEmail(user.getEmail()) != null
|| userBean.checkExistName(user.getUsername()) != null || userBean.checkExistEmail(user.getEmail()) != null){
checkUserEmail("username or email has exist");
}else {
user = userBean.addUser(user);
addMessage("add success");
return "ok";
}
}
return "String";
}
public javax.faces.model.SelectItem[] getGroupList()
{
开发者_如何学编程 SelectItem[] options = null;
List<Groups> groups = userBean.retrieveAllGroups();
if (groups != null && groups.size() > 0)
{
int i = 0;
options = new SelectItem[groups.size()];
for (Groups iGroups : groups)
{
options[i++] = new SelectItem(iGroups,iGroups.getGroupname());
}
}
return options;
}
public void checkUserEmail(String msg) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,msg,msg));
}
public void addMessage(String msg) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(msg));
}
}
my question is when i use method isAdmin from loginController for userController to check if he is admin he can choose user groups, if he isn't admin he add default groups is member. but isAdmin from loginController return false why? why it not still data when user login?
You need to inject the bean as a managed property.
@ManagedProperty(value="#{loginController}")
private LoginController loginController;
And get rid of the new LoginController()
line in the constructor. It would construct a completely separate instance of the bean, not reuse the already-created one in the session scope.
精彩评论