JSF 2 In and Out Injection with @ManagedProperty

Usually during login proses, we validate the user credential, fetch the user details from database and store it in a session for the future use. Normally HttpSession is used to achieve this goal. In Seam we do this injection by using @In and @Out annotation, in JSF 2, we use @ManagedProperty.

Consider we have a login bean with request scope and a user session bean with session scope. So it would be like this :
1.user session bean
@ManagedBean(name="userSession")
@SessionScoped
public class UserSession {

   private String username;
   //getter and setter for username
}

2.login bean
@ManagedBean(name="loginActionBean")
@RequestScoped
public class loginActionBean { 

   @ManagedProperty(value = "#{userSession}")
   private UserSession userSession ;
   //getter and setter for userSession

   private String username;
   //getter and setter for username

   public String authenticate() {
      userSession.setUsername(this.username);
      return "nextpage.jsf";
   }
}

then we can get the user session value in other managed beans or our view(xhtml) by calling something like userSession.getUsername or #{userSession.username} . Again, @ManagedProperty(value = "#{userSession}") with its getter and setter must be set in the managed bean where we want to use the session.

Alternatively, we can also use FacesContext to set and get the session.

8 comments:

  1. Showing the following exception:

    javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:619)

    ReplyDelete
  2. Hi Soumitra,

    This should work on a default JSF2 setup with the Tomcat servlet runner. I'm not sure in your case. If you can post a more specific details, I might be able to help you.
    -Cheers-

    ReplyDelete
  3. hi, i'm getting userSession variable null in loginActionBean when i'm going to use the set method at the authenticate, do you know why? i'm using default jsf2 setup with glassfish 3. thanx in advance.

    ReplyDelete
  4. ok I resolved that problem is when I use the @ManagedProperty of the userSession in other bean I got his properties null, let's say I'm going to insert a row in a Person table, I have the PersonBean in a RequestSession and the
    @ManagedProperty(value = "#{userSession}")
    private UserSession userSession;

    In userSession instead of a String value I have Person value, Person being an Entity Class, so basically when I use the userSession.getPerson() it comes null.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. to use this
    you should be sure that "javax.faces.STATE_SAVING_METHOD" in webconfig.xml must be "server".
    otherwise you will get "nullpointerexception".

    ReplyDelete