You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Elam Daly <el...@gmail.com> on 2006/01/10 01:11:28 UTC

Populating Component Tree Values from Servlet

Hi all,

I got the code below from the JSF spec, and it works fine except for the
fact that I don't know how to put values recieved from the query string of a
servlet into my Managed beans so that they are displayed when the first page
initially comes up.  In fact, I can't get a childCount from the UIViewRoot
component till after the call to lifecycle.render(), which calls the render
response phase, which is the last phase in the cycle.

Any help is much appreciated.

-Elam


---------------------

public class RedirectServlet extends HttpServlet {

       protected void doGet( HttpServletRequest request, HttpServletResponse
response) {

LifecycleFactory lifecycleFactory = (LifecycleFactory)
FactoryFinder.getFactory(
                       FactoryFinder.LIFECYCLE_FACTORY);
           Lifecycle lifecycle = lifecycleFactory.getLifecycle(
                   LifecycleFactory.DEFAULT_LIFECYCLE);

           FacesContextFactory facesContextFactory =
               (FacesContextFactory) FactoryFinder.getFactory(
                       FactoryFinder.FACES_CONTEXT_FACTORY);
           FacesContext facesCtx = facesContextFactory.getFacesContext(
getServletContext(),
                   request, response, lifecycle);

                      // Create new view

                      // 2.4.2.1
           Application app = facesCtx.getApplication();
           ViewHandler viewHdlr = app.getViewHandler();
           UIViewRoot viewRoot = viewHdlr.createView( facesCtx,
requestedView);
                      // 2.4.2.2: we accept the default renderkit
                      // 2.4.2.3: we'll let Faces handle the
                      //   component tree

                      // 2.4.2.4: store view into context
           facesCtx.setViewRoot( viewRoot);
           lifecycle.render( facesCtx);

Re: Populating Component Tree Values from Servlet

Posted by Craig McClanahan <cr...@apache.org>.
On 1/9/06, Simon Kitching <sk...@apache.org> wrote:
>
> [snip]
> However your query params are just keys in the request scope, so they
> can be accessed using EL expressions like #{someQueryParamName} from the
> JSP pages.


Almost ... but not quite.  You'll have much better luck with an expression
like:

    #{param.someQueryParamName}

There are a series of predefined "variable" names that let you access
interesting things via EL expressions.  In addition to "param" (which gets
the first, or only, value for the specified parameter name), you might take
a look at (all of these return a Map):
* cookie -- The HTTP cookies that came in on this request
* header -- The first, or only, HTTP header for the specified key
* initParam -- The context initialization parameters for this application

The full list is outlined in Section 5.3.1.2 of the JSF Specification, and
is a (slight) superset of the implicit names known to JSTL and JSP
expressions.

Craig

Re: Populating Component Tree Values from Servlet

Posted by Simon Kitching <sk...@apache.org>.
On Mon, 2006-01-09 at 19:11 -0500, Elam Daly wrote:
> Hi all,
> 
> I got the code below from the JSF spec, and it works fine except for
> the fact that I don't know how to put values recieved from the query
> string of a servlet into my Managed beans so that they are displayed
> when the first page initially comes up.  In fact, I can't get a
> childCount from the UIViewRoot component till after the call to
> lifecycle.render(), which calls the render response phase, which is
> the last phase in the cycle.
> 
> Any help is much appreciated.

When you're using JSP as the view technology, then the first time the
page is rendered, the components are created as rendering is done. So
obviously before lifecycle.render they won't exist. There's just no way
to explicitly "push" the values into the components before rendering.

However your query params are just keys in the request scope, so they
can be accessed using EL expressions like #{someQueryParamName} from the
JSP pages.

You should also be able to "inject" values into any request-scope
managed beans, as shown below. Note that I haven't got a web.xml example
handy, so the xml element names are only very rough :-)

  <managed-bean>
    <bean-name>myBean</beanName>
    <bean-class>example.MyBean</bean-class> 
    <scope>request</scope>
    <managed-properties>
      <managed-property>
        <name>somePropertyName</name>
        <value>#{someQueryParamName}</value>
      </managed-property>
   ....

When the jsp page refers to #{myBean}, the bean will be created "on
demand", and initialised as defined in the managed bean mapping. The
expression #{someQueryParamName} is evaluated by looking in
request/session/app scope - where it will find the query param value.

Note that the bean *must* be request-scope however, as it's forbidden to
initialise a session or app scoped bean with a request-scoped value (for
obvious reasons).


Or your backing bean can just call: 
    FacesContext.getCurrentInstance().
      getExternalContext().getRequestMap().
        get(someQueryParamName);

Regards,

Simon