marți, 30 iunie 2015

[OmniFaces utilities 2.1] Programmatically EL-resolve the given property on the given base object and return the resolved value


[OmniFaces utilities] The resolveExpressionGet() method programmatically EL-resolve the given property on the given base object and return the resolved value.

Method:
See also: Faces#getContext()
Usage:

Let's suppose that we have the following simple CDI bean:

@Named
@SessionScoped
public class CommandBean {

 private String command;

 public String getCommand() {
  return command;
 }

 public void setCommand(String command) {
  this.command = command;
 }
}

Now, the Faces#resolveExpressionGet() is capable to programmatically EL-resolve the command property on the CommandBean base object and return the resolved value. In a real scenario, you will be in a JSF artifact (e.g. in a bean, custom component, etc), and you will need to inspect the property of an existing CommandBean object (instance). But, we can also perform a quick test via Beans#getInstance() utility method, which returns the CDI managed bean instance (actual) of the given bean class and creates one if one doesn't exist. Since the above bean is a CDI bean, we can do this:

// returns:
   - null, if the CommandBean object (instance) was created by the Beans#getInstance(), since one doesn't exist
   - if the CommandBean object (instance) exist, it will return the current value of the command property
CommandBean commandBean = Beans.getInstance(CommandBean.class);
String resolved = Faces.resolveExpressionGet(commandBean, "command");

You can resolve a top-level variable also, by passing null as base:

Object resolved = Faces.resolveExpressionGet(null, "top-level variable");

Niciun comentariu:

Trimiteți un comentariu