org.picocontainer.web.jsf
Class PicoVariableResolver

java.lang.Object
  extended by javax.faces.el.VariableResolver
      extended by org.picocontainer.web.jsf.PicoVariableResolver

public class PicoVariableResolver
extends javax.faces.el.VariableResolver

This is a variable resolver implementation for Java ServerFaces.

Installation

Add install this variable resolver by adding setting the application's variable resolver to org.picocontainer.web.jsf.NanoWarDelegatingVariableResolver. An example follows:


   <faces-config>
      <application>
          <strong>
          <variable-resolver>
              org.picocontainer.web.jsf.NanoWarDelegatingVariableResolver
          </variable-resolver>
          </strong>
      </application>
      ....
   </faces-config>
 

Usage

Part 1 - Write your Constructor Dependency Injection (CDI) - based backing bean:

Even though you are writing a backing bean, you can utilize PicoContainers CDI capabilities to the fullest. Example:

    //Imports and variables...
    
    public ListCheeseController(<strong>CheeseService service</strong>) {
       this.service = service;       
    }
    
    //The rest of the class.
 

Part 2 - Set up your NanoWAR services.

(This assumes you have installed NanoWAR properly. Please see the NanoWAR documentation for specific instructions)

You need to name your services with the name you will be giving your Backing Bean. Example:

    pico = builder.container(parent: parent) {
        if(assemblyScope instanceof javax.servlet.ServletContext) {
          // Application Services would go here.
        } else if (assemblyScope instanceof javax.servlet.ServletRequest) {
            <strong>addComponent(key: 'cheeseBean', class: 'org.picocontainer.web.samples.jsf.ListCheeseController')</strong>
        }
    }
 

Part 3 - Set up your managed beans for JSF

Set the managed bean names in your faces-config to equal the names given to the backing beans in the nanowar composition script. Example:

    <managed-bean>
        <description>CDI Injected Bean</description>
        <strong><managed-bean-name>cheeseBean</managed-bean-name></strong>
        <managed-bean-class>
            org.picocontainer.web.samples.jsf.CheeseController
        </managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
 

Notice how the same names were used in the faces-config as in the nanowar configuration. When the JSF page asks for the bean named 'addCheeseBean', the Nano variable resolver will take that name and check nanocontainer for an object of that instance. If it finds one, it will send it back to the page.

Note:

This class currently has only been tested using MyFaces. There are reports that this technique doesn't work on all reference implementation versions. We welcome success or failure feedback!

Author:
Michael Rimov

Nested Class Summary
static class PicoVariableResolver.ServletFilter
           
 
Constructor Summary
PicoVariableResolver(javax.faces.el.VariableResolver decorated)
          Decorated Variable resolver.
 
Method Summary
protected  javax.faces.el.VariableResolver getNested()
          Retrieve the delegated value.
protected  org.picocontainer.PicoContainer getPicoContainer(javax.faces.context.FacesContext facesContext)
          Tries to locate the nanocontainer first at request level, and then if it doesn't find it there.
 Object resolveVariable(javax.faces.context.FacesContext facesContext, String name)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PicoVariableResolver

public PicoVariableResolver(javax.faces.el.VariableResolver decorated)
Decorated Variable resolver.

Parameters:
decorated -
Method Detail

getNested

protected javax.faces.el.VariableResolver getNested()
Retrieve the delegated value.

Returns:
the wrapped variable resolver.

resolveVariable

public Object resolveVariable(javax.faces.context.FacesContext facesContext,
                              String name)

Specified by:
resolveVariable in class javax.faces.el.VariableResolver
Parameters:
facesContext -
name -
Returns:
the resulting object, either resolved through NanoWAR, or passed onto the delegate resolver.
Throws:
javax.faces.el.EvaluationException
See Also:
VariableResolver.resolveVariable(javax.faces.context.FacesContext, java.lang.String)

getPicoContainer

protected org.picocontainer.PicoContainer getPicoContainer(javax.faces.context.FacesContext facesContext)
Tries to locate the nanocontainer first at request level, and then if it doesn't find it there. (Filter might not be installed), it tries Application level. If that fails it throws an exception since you wouldn't expect the NanoWarDelegatingVariableResolver

Parameters:
facesContext -
Returns:
NanoContainer instance.
Throws:
javax.faces.el.EvaluationException - if it cannot find a NanoWAR instance.


Copyright © 2008. All Rights Reserved.