Thursday, August 2, 2012

JSF : Get a bean from Session, Request

To retrieve the session scoped bean or a request scope bean in JSF we can use  session map or request map respectively.


<faces-config>
    <managed-bean>
        <managed-bean-name>testSessionObject</managed-bean-name>
        <managed-bean-class>test.TestSessionObject</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
     </managed-bean>
    <managed-bean>
        <managed-bean-name>testRequestbject</managed-bean-name>
        <managed-bean-class>test.TestRequestbject</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
     </managed-bean>
<faces-config>

Java Code :

FacesContext context = FacesContext.getCurrentInstance(); 

// Get a Session scoped bean
Map sessionMap = context.getExternalContext().getSessionMap();

TestSessionObject  testSessionObject  = (TestSessionObject  ) map.get("testSessionObject");

// Get a Request scoped bean
Map requestMap = context.getExternalContext().getRequestMap();
TestRequestObject  testRequestObject  = (TestRequestObject  ) map.get("testRequestObject  ");


No comments:

Post a Comment