Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

jsf - Access session scoped bean from request scoped bean

I'm trying to use a pattern found on a IceFaces page.(I'm not using IceFaces, using PrimeFaces)

In this case I have two beans:

UserController and Usermodel

On my UserModel I have a instance of UserVO (created by another programmer). On My UserController I have this:

@ManagedBean
@RequestScoped
public class UserController implements Serializable
{

    private static final long serialVersionUID = 1L;
    private UserBO bo;
    private UserModel model;

    public UserController()
    {
        bo = new UserBO();
        model = new UserModel();
    }

    public void Login() throws IOException
    {
        model.setUserVo(bo.executeLogin(model.getUserVo()));
        ExternalContext externalContent = FacesContext.getCurrentInstance().getExternalContext();
        if (!model.getUserVo().isError())
        {
            model.setLoggedIn(true);
            externalContent.getSessionMap().put("userSession", model);
            externalContent.redirect(externalContent.getRequestContextPath() + "/views/request/search.html");
        } else
        {
            model.setLoggedIn(false);
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, model.getUserVo().getMessage(), model.getUserVo().getLogin());
            FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        }
    }

    public UserBO getBo()
    {
        return bo;
    }

    public void setBo(UserBO bo)
    {
        this.bo = bo;
    }

    public UserModel getModel()
    {
        return model;
    }

    public void setModel(UserModel model)
    {
        this.model = model;
    }
}

As you can see, I create a new instance of UserModel and set it with what was returned from the bo.executeLogin() and it is working, my object is returned.

To make sure the user is logged in, I have a property on my UserModel:

@ManagedBean
@SessionScoped
public class UserModel
{

    private UserVO userVo;
    private Boolean loggedIn = false;

    public UserModel()
    {
        userVo = new UserVO();
    }

    public UserVO getUserVo()
    {
        return userVo;
    }

    public void setUserVo(UserVO userVo)
    {
        this.userVo = userVo;
    }

    public Boolean getLoggedIn()
    {
        return loggedIn;
    }

    public void setLoggedIn(Boolean loggedIn)
    {
        this.loggedIn = loggedIn;
    }

I have a template.xhtml with:

<ui:fragment rendered="#{userModel.loggedIn}">
            <ui:include src="../includes/top.xhtml"/>
</ui:fragment>

And the thing is that it is not working, is not getting the loggedIn property value.

My guess is that accessing this way I'm kinda creating a new instance of UserModel, if so, it is a problem because my UserController is not session scoped, only the UserModel

EDIT

Instead of using this loggedIn property I know I can simply check if the UserModel userVo property is set but the problem is about the session scoped bean, I can't access it from UserController, where it is set because it isn't scoped session, and my template.xhtml will be used by every page.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(this was originally posted on https://stackoverflow.com/questions/10691324/working-with-3-java-beans-controller-backing-model, but the OP deleted the question and I didn't want to throw away the answer, I think a repost on this question is suitable as well)

You were probably focusing too much on this ICEfaces blog which contains mainly nonsense.

You should have a single JSF managed bean which acts as a controller, you already have it: UserController. You should have a simple entity bean which represents the model, you already have it: UserVO. You should have an enterprise bean which represents the service, you already have it: UserBO. Those last two doesn't need to be JSF managed beans.

Based on your question history you're using Glassfish, thus you can make use of JPA and EJB. So the model class should be a JPA @Entity and the service class should be a @Stateless EJB.

The use case of a "login user" is however special. You don't want to have the entity as a session scoped managed bean, or it would be implicitly created by JSF. You'd better put it straight in the session scope yourself, so that you can check on #{not empty user} if the user is logged in or not.

All with all it should look something like this:

@Entity
public class User {

    private String username;
    private String password;

    // ...
}
@Stateless
public class UserService {

    @PersistenceContext
    private EntityManager em;

    public User find(String username, String password) {
        return em.createQuery("SELECT u FROM User u WHERE username = :username AND password = MD5(:password)", User.class)
           .setParameter("username", username)
           .setParameter("password", password)
           .getSingleResult();
    }

}
@ManagedBean
@ViewScoped
public class UserController {

    private String username;
    private String password;

    @EJB
    private UserService service;

    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();

        try {
            User user = userService.find(username, password);
            context.getExternalContext().getSessionMap().put("user", user);
            return "/views/commons/home.html?faces-redirect=true";
        }
        catch (NoResultException) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unknown login, please try again", null));
            return null;
        }
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "/views/commons/login.html?faces-redirect=true";
    }

    // ...
}

with

<h:form>
    <h:inputText value="#{userController.username}" required="true" />
    <h:inputSecret value="#{userController.password}" required="true" />
    <h:commandButton value="login" action="#{userController.login}"/>
    <h:messages />
</h:form>

Alternatively, you can make the UserController a session scoped bean which holds the User entity, you'd only need to add an extra method to check if the user is logged in or not so that you can use it in EL by #{userController.loggedIn}.

@ManagedBean
@SessionScoped
public class UserController {

    private User user = new User();

    @EJB
    private UserService service;

    public String login() {
        FacesContext context = FacesContext.getCurrentInstance();

        try {
            user = userService.find(user.getUsername(), user.getPassword());
            return "/views/commons/home.html?faces-redirect=true";
        }
        catch (NoResultException) {
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unknown login, please try again", null));
            return null;
        }
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "/views/commons/login.html?faces-redirect=true";
    }

    public boolean isLoggedIn() {
        return user.getId() != null;
    }

    // ...
}

with

<h:form>
? ? <h:inputText value="#{userController.user.username}" required="true" />
? ? <h:inputSecret value="#{userController.user.password}" required="true" />
? ? <h:commandButton value="login" action="#{userController.login}"/>
? ? <h:messages />
</h:form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...