1

I have a Java Bean called appProps defined as an ApplicationScope that is a Hashmap and of the type <string, object>. I can access it via SSJS using the format

var appDesc:String = appProps["some application name"].getAppDesc();

and this returns the application description which is stored in one of the fields in the Hashmap Object.

Now I need to call the same process in another JAVA Class.

The definition in the faces-config is:

<managed-bean>
    <managed-bean-name>appProps</managed-bean-name>
    <managed-bean-class>ca.wfsystems.core.ApplicationMap</managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>
  </managed-bean>
Bill F
  • 2,057
  • 3
  • 18
  • 39
  • This might help: [Get JSF managed bean by name in any Servlet related class][1] [1]: http://stackoverflow.com/questions/2633112/get-jsf-managed-bean-by-name-in-any-servlet-related-class – Floresj4 Sep 02 '15 at 21:23

3 Answers3

1

The best Java equivalent of the implicit lookup that SSJS and EL do for appProps there is:

ApplicationMap appProps = (ApplicationMap)ExtLibUtil.resolveVariable(FacesContext.getCurrentInstance(), "appProps")
Jesse Gallagher
  • 4,461
  • 13
  • 11
  • Because Jesse's too modest, he was the one who contributed the simplified method to ExtLib 14, which will allow you to avoid having to pass the FacesContext instance. – Paul Stephen Withers Sep 02 '15 at 21:31
  • With a bit of search and figuring out which libraries to import this code works great. Thanks – Bill F Sep 02 '15 at 22:34
  • Along those lines, you can generally hover your cursor over the class name or press ctrl-space at the end of the class name to have Designer try to find it for you. – Jesse Gallagher Sep 03 '15 at 00:27
  • Could you run into a scenario where the `resolveVariable` call throws an error because the bean hasn't been instantiated yet? I recall that I ran into that once (and solved it by instantiating and storing it myself). – Mark Leusink Sep 03 '15 at 13:55
  • resolveVariable should instantiate it if need be: it runs through the same routine as named access in SSJS. If you try to get the variable right from the scoped map, that's when it would be trouble. – Jesse Gallagher Sep 04 '15 at 00:24
0

There are two easy solutions:

  1. ExtLibUtil.resolveVariable() allows you to access it by the variable name declared in faces-config, so appProps. ExtLib 14 allows you to just pass the name.

  2. Add a static get() method in the bean that uses ExtLibUtil.resolveVariable(). You can then call ApplicationMap.get()

Paul Stephen Withers
  • 15,699
  • 1
  • 15
  • 33
0

Another (smart) way to access a java bean from a different java class

This is your bean class:

package ca.wfsystems.core;

import javax.faces.context.FacesContext;

public class ApplicationMap {

    // Constants

    private static final String BEAN_NAME = "appProps"; //$NON-NLS-1$

    // Operations

    public static ApplicationMap getCurrentInstance() {
        // This is a neat way to get a handle on the instance of this bean in the scope from other Java code...
        FacesContext context = FacesContext.getCurrentInstance(); 
        return (ApplicationMap) context.getApplication().getVariableResolver().resolveVariable(context, BEAN_NAME);
    }

}

And this one is a sample class using your bean class:

package ca.wfsystems.core;

public class ApplicationMapClient {

    // Operations

    public void doSomeThing() {

        ApplicationMap appMap = ApplicationMap.getCurrentInstance();
        // Your code goes here....

    }

}

For further information take a look at the blog entry a lesson on scoped managed beans in xpages from John Dalsgaard.

Georg Kastenhofer
  • 1,387
  • 12
  • 32