Accessing value of a session bean on a JSF page

Discussion in 'Java' started by laredotornado, Jul 9, 2008.

  1. Hi,

    I was hoping someone could help me optimize some code on my JSF page,
    since I figure that's the point of JSF. Here's what I haev

    SSOSessionBean ssoSessionBean = null;
    ssoSessionBean = (SSOSessionBean)
    session.getAttribute(IMConstants.SSOSession);
    String userName = ssoSessionBean.getUserName();
    out.println(userName);

    Is is possible to condense this down to a single line of code? What
    changes, if any, would I have to make to my faces-config.xml file?

    Thanks, - Dave
     
    laredotornado, Jul 9, 2008
    #1
    1. Advertising

  2. laredotornado

    Guest

    laredotornado wrote:
    > I was hoping someone could help me optimize some code on my JSF page,
    > since I figure that's the point of JSF.  Here's what I haev
    >
    >         SSOSessionBean ssoSessionBean = null;
    >         ssoSessionBean = (SSOSessionBean)
    > session.getAttribute(IMConstants.SSOSession);


    How come you initialize then assign, when you could initialize in a
    single step?

    Is 'SSOSessionBean' an EJB?

    >         String userName = ssoSessionBean.getUserName();
    >         out.println(userName);


    JSF is not used inside Java classes, but can invoke methods from such
    classes. The JSF components are embedded in XHTML or JSP pages,
    typically.

    > Is is possible to condense this down to a single line of code?  What
    > changes, if any, would I have to make to my faces-config.xml file?


    What is the magic of having it in a single line of code?

    In any event, you're better off putting display logic in a JSP than in
    a "regular" servlet.

    This will not condense the logic into a single line. In fact, it will
    add lines and split the answer into two artifacts - the JSP that
    displays 'userName' and the business logic that uses the
    'SSOSessionBean' to obtain the value. It will be a better solution
    than what you show us here.

    --
    Lew
     
    , Jul 9, 2008
    #2
    1. Advertising

  3. On Jul 9, 3:02 pm, wrote:
    > laredotornadowrote:
    > > I was hoping someone could help me optimize some code on my JSF page,
    > > since I figure that's the point of JSF.  Here's what I haev

    >
    > >         SSOSessionBean ssoSessionBean = null;
    > >         ssoSessionBean = (SSOSessionBean)
    > > session.getAttribute(IMConstants.SSOSession);

    >
    > How come you initialize then assign, when you could initialize in a
    > single step?
    >
    > Is 'SSOSessionBean' an EJB?
    >
    > >         String userName = ssoSessionBean.getUserName();
    > >         out.println(userName);

    >
    > JSF is not used inside Java classes, but can invoke methods from such
    > classes.  The JSF components are embedded in XHTML or JSP pages,
    > typically.
    >
    > > Is is possible to condense this down to a single line of code?  What
    > > changes, if any, would I have to make to my faces-config.xml file?

    >
    > What is the magic of having it in a single line of code?
    >
    > In any event, you're better off putting display logic in a JSP than in
    > a "regular" servlet.
    >
    > This will not condense the logic into a single line.  In fact, it will
    > add lines and split the answer into two artifacts - the JSP that
    > displays 'userName' and the business logic that uses the
    > 'SSOSessionBean' to obtain the value.  It will be a better solution
    > than what you show us here.
    >
    > --
    > Lew


    The code I had above was in a JSP but I was wondering I could replace
    it with JSF tags. That is fine is this is not in a single line, but
    how would you code it in JSF?

    Thanks, - Dave
     
    laredotornado, Jul 10, 2008
    #3
  4. laredotornado

    Guest

    laredotornado wrote:
    > The code I had above was in a JSP but I was wondering I could replace
    > it with JSF tags.  That is fine is this is not in a single line, but
    > how would you code it in JSF?


    Approximately like this:

    <html> ... <body> ...

    <h:eek:utputText id="userName" value="#{backer.userName}" />
    ...

    where 'backer' is an instance of a JavaBean that encapsulates the
    'SSOSe4ssionBean' call.

    BTW, in your code snippet you showed
    > SSOSessionBean ssoSessionBean = (SSOSessionBean)
    > session.getAttribute(IMConstants.SSOSession);


    What is 'IMConstants.SSOSession'? If it's a field, it's name violates
    the coding conventions, as it looks like a nested class name.

    --
    Lew
     
    , Jul 10, 2008
    #4
  5. On Jul 10, 11:40 am, wrote:
    > laredotornadowrote:
    > > The code I had above was in a JSP but I was wondering I could replace
    > > it with JSF tags.  That is fine is this is not in a single line, but
    > > how would you code it in JSF?

    >
    > Approximately like this:
    >
    > <html> ... <body> ...
    >
    >  <h:eek:utputText id="userName" value="#{backer.userName}" />
    > ...
    >
    > where 'backer' is an instance of a JavaBean that encapsulates the
    > 'SSOSe4ssionBean' call.
    >
    > BTW, in your code snippet you showed
    >
    > > SSOSessionBean ssoSessionBean = (SSOSessionBean)
    > >                session.getAttribute(IMConstants.SSOSession);

    >
    > What is 'IMConstants.SSOSession'?  If it's a field, it's name violates
    > the coding conventions, as it looks like a nested class name.
    >
    > --
    > Lew


    How would I construct the managed-bean construct such that it is
    understood that the session attribute's name is "USER_CREDENTIAL"?

    Thanks for your replies, - Dave
     
    laredotornado, Jul 10, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Thielen
    Replies:
    2
    Views:
    834
    Marek Lange
    Sep 12, 2003
  2. X3
    Replies:
    0
    Views:
    432
  3. BlackIce
    Replies:
    1
    Views:
    17,223
  4. Replies:
    0
    Views:
    2,585
  5. Johnson Scaria John
    Replies:
    0
    Views:
    1,152
    Johnson Scaria John
    Jun 30, 2008
Loading...

Share This Page