tags:

views:

26

answers:

1

Writing a simple JSF application I've some across the following Problem: My entities.controller.EntityNameManager class contains a method getEntityNameSelectList() which I can use to populate a ComboBox with. This works and shows all Entities, since the Method to retrieve the Entities does not have a where clause. This Method was automatically created.

Now I want to have a second similar Method, that filters the options based on a variable in the sessionscope. To do this I copied the original Method, renamed it to getEntityNameSelectListByUser(User theUser) and changed the Method that queries the database to one that does indeed filter by UserId.

However, when trying to load the page in the browser, I get an error stating that the controller class does not have a "EntityNameSelectListByUser" property. I assume that since my new method expects a parameter it can't be found. Is there a way I can make it aware of the Parameter or the Sessionscope userid?

A: 

Support for parameters in EL is slated for the next maintenance release of JSR 245 (announcement here; implementation here).


Assuming you don't want to wait for JEE6, you have several ways to overcome this limitation. These approached are defined in terms of POJO managed beans, so adapt them to your EJBs as appropriate.

1.

Do the session lookup and function call in a backing bean:

  public String getFoo() {
    FacesContext context = FacesContext
        .getCurrentInstance();
    ExternalContext ext = context.getExternalContext();
    String bar = (String) ext.getSessionMap().get("bar");
    return getFoo(bar);
  }

Example binding:

#{paramBean.foo}

2.

Use an EL function (defined in a TLD, mapped to a public static method):

  public static String getFoo(ParamBean bean, String bar) {
    return bean.getFoo(bar);
  }

Example binding:

#{baz:getFoo(paramBean, bar)}

3.

Subvert the Map class to call the function (a bit of a hack and limited to one parameter):

  public Map<String, String> getFooMap() {
    return new HashMap<String, String>() {
      @Override
      public String get(Object key) {
        return getFoo((String) key);
      }
    };
  }

Example binding:

#{paramBean.fooMap[bar]}
McDowell