I think what he's saying is that if you have a request-scoped bean
that needs to reference a session scoped bean. You can have JSF
automatically set the request-scoped bean's property upon creation
with the managed-property facility.
<managed-bean>
<managed-bean-name>mySessionBean</managed-bean-name>
<managed-bean-class>org.heath.MySessionBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>myRequestBean</managed-bean-name>
<managed-bean-class>org.heath.MyRequestBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>mySessionBean</property-name>
<property-class>org.heath.MySessionBean</property-class>
<value>#{mySessionBean}</value>
</managed-property>
</managed-bean>
Essentially, what this does is create a 'MyRequestBean' instance for
each request and call MyRequestBean.setMySessionBean(mySessionBean);
This allows you to be guaranteed to have access to the session bean
through your request beans.
I'm not using it myself, but its pretty slick.
On Wed, 02 Feb 2005 23:03:59 -0600, Aaron Bartell
<[EMAIL PROTECTED]> wrote:
> Thanks for your critique Kalle.
>
>
> >If you don't want to use container-managed authentication &
> authorization, I'd implement a filter to do that. So the app wouldn't
> need to do any extra checks for basic authorization anymore.
>
> I actually implement isLoggedIn in a PhaseListener which then gets
> called every request for PhaseId.RESTORE_VIEW. Is this the type of
> filter you are talking about?
>
>
> >If you need to access some other bean in a bean, why not make the
> other bean a managed property of the bean and let the framework handle
> setting it for you?
>
> Help me understand what you are saying. Do you mean every bean would
> have a getter/setter for any other managed-bean it needs access to? So
> if bean1 needed access to beans b1, b2, b3, b4,b5,b6 and b7 I would have
> to create property accessor methods for each of those?
>
> >And isLoggedIn() sounds like it should semantically be an operation of
> User object.
>
> I have mixed feelings on that, because User is my Model object and there
> will be many instances where a property called loggedIn just wont make
> sense, but it is a perfect fit for a class called LoginController. See
> my point? I didn't post the User object, but it is basically hibernate
> generated with some ease-of-use accessor methods built in.
>
> Once again thanks for your critique,
> Aaron Bartell
>
>
> Korhonen, Kalle wrote:
>
> >No offense, but looks overly complex to me... If you need to access some
> >other bean in a bean, why not make the other bean a managed property of
> >the bean and let the framework handle setting it for you? Most of our
> >request scoped beans have User bean (session scoped) as their managed
> >property. And isLoggedIn() sounds like it should semantically be an
> >operation of User object. If you don't want to use container-managed
> >authentication & authorization, I'd implement a filter to do that. So
> >the app wouldn't need to do any extra checks for basic authorization
> >anymore.
> >
> >Kalle
> >
> >
> >
> >>-----Original Message-----
> >>From: Aaron Bartell [mailto:[EMAIL PROTECTED]
> >>Sent: Wednesday, February 02, 2005 7:29 PM
> >>To: MyFaces Discussion
> >>Subject: Critique implementation of managed bean access
> >>
> >>Hey all you great minds! I have found a comfortable way to
> >>access other managed-bean's in a session and I would like to
> >>know if it is a good implementation. Here is a
> >>LoginController class that act's as both a POJO and also
> >>allows you to retrieve the same named session object from the
> >>faces configuration using static methods. Before using this
> >>approach I was up in arms on where to put methods like
> >>LoginController.loggedIn(). I am sure others have done
> >>something similar to this and I am not the first, but I would
> >>still like input on whether it is good or bad.
> >>
> >>Thoughts?
> >>Aaron Bartell
> >>
> >>
> >>-----faces-config.xml entry-----
> >> <managed-bean>
> >> <managed-bean-name>LoginCtl</managed-bean-name>
> >>
> >><managed-bean-class>com.mowyourlawn.controller.LoginController
> >></managed-bean-class>
> >> <managed-bean-scope>session</managed-bean-scope>
> >> </managed-bean>
> >>
> >>
> >>-----Usage Examples-----
> >>
> >>userid = LoginController.user().getUid(); ...
> >>LoginController.loggedIn(Boolean.FALSE);
> >>...
> >>if(!LoginController.loggedIn().booleanValue()){
> >> // go to login.jsp
> >>}
> >>
> >>
> >>-----LoginController Class-----
> >>
> >>package com.mowyourlawn.controller;
> >>
> >>import javax.faces.application.FacesMessage;
> >>import javax.faces.context.FacesContext;
> >>
> >>import com.mowyourlawn.dao.User;
> >>import com.mowyourlawn.util.Const;
> >>
> >>public class LoginController {
> >>
> >> private User user = new User();
> >> Boolean loggedIn = new Boolean(false);
> >>
> >> public LoginController() {
> >> }
> >>
> >> public static User user() {
> >> FacesContext fc = FacesContext.getCurrentInstance();
> >> return (User)
> >>fc.getApplication().createValueBinding("#{LoginCtl.user}").get
> >>Value(fc);
> >> }
> >>
> >> public static Boolean loggedIn() {
> >> FacesContext fc = FacesContext.getCurrentInstance();
> >> return (Boolean)
> >>fc.getApplication().createValueBinding("#{LoginCtl.loggedIn}")
> >>.getValue(fc);
> >> }
> >>
> >> public static void loggedIn(Boolean x) {
> >> FacesContext fc = FacesContext.getCurrentInstance();
> >>
> >>fc.getApplication().createValueBinding("#{LoginCtl.loggedIn}")
> >>.setValue(fc,
> >>x);
> >> return;
> >> }
> >>
> >> public String validLogin() {
> >> if (LoginController.user().validLogin()) {
> >> LoginController.loggedIn(Boolean.TRUE);
> >> return "success";
> >> }
> >> LoginController.loggedIn(Boolean.FALSE);
> >> FacesContext fc = FacesContext.getCurrentInstance();
> >> FacesMessage facesMessage = new FacesMessage("You
> >>have entered an invalid user name and/or password.");
> >> fc.addMessage("loginform", facesMessage);
> >> return "failure";
> >> }
> >>
> >> public String logout() {
> >> LoginController.loggedIn(Boolean.FALSE);
> >> FacesContext fc = FacesContext.getCurrentInstance();
> >> FacesMessage facesMessage = new FacesMessage("You
> >>have been logged out of the system.");
> >> fc.addMessage("loginForm", facesMessage);
> >> return "logout";
> >> }
> >>
> >> public boolean getAdminAccess() {
> >> if (LoginController.user().getAccesslevel().intValue() ==
> >>Const.AL_ADMIN.intValue()
> >> ||
> >>LoginController.user().getAccesslevel().intValue() ==
> >>Const.AL_OPERATOR.intValue()) return true;
> >> return false;
> >> }
> >>
> >> public User getUser() {
> >> return user;
> >> }
> >>
> >> public void setUser(User usr) {
> >> this.user = usr;
> >> }
> >>
> >> public Boolean getLoggedIn() {
> >> return loggedIn;
> >> }
> >>
> >> public void setLoggedIn(Boolean loggedIn) {
> >> this.loggedIn = loggedIn;
> >> }
> >>}
> >>
> >>
> >>
> >>
> >
> >
> >
>
--
-Heath Borders-Wing
[EMAIL PROTECTED]