After coding many backing/managed beans it became clear to me that there were a few things that I seem to be doing over and over, these were:- Getting values from the binding layer
- Setting values in the binding layer
- Executing operation bindings
- Using a combination of the above in a backing bean method
The logical thing to do was be to but my code for doing this into a class and extend this class for all of my managed beans. The class is called JSFBean and uses the binding "#{bindings}" to access the binding container. The three methods in the class (so far) are: execute, getValue and setValue.
The bean which I have called JSFBean includes these three methods and some basic error handling. When creating a backing bean simply add "extends JSFBean" to the class definition.
JSFBean.java:package com.delexian.ui.backing;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.binding.OperationBinding;
public class JSFBean {
public JSFBean() {
}
public DCBindingContainer getBindings() {
FacesContext fc = FacesContext.getCurrentInstance();
ValueBinding vb = fc.getApplication().createValueBinding("#{bindings}");
DCBindingContainer dc = (DCBindingContainer) vb.getValue(fc);
return dc;
}
public boolean execute(String operation){
DCBindingContainer bindings = getBindings();
OperationBinding operationBinding = bindings.getOperationBinding(operation);
if (operationBinding == null){
FacesContext fc = FacesContext.getCurrentInstance();
fc.addMessage("Invalid Operation", new FacesMessage(operation + " is not a valid operation for this page"));
return true;
}
operationBinding.execute();
return operationBinding.getErrors().isEmpty();
}
public Object getValue(String el){
FacesContext fc = FacesContext.getCurrentInstance();
ValueBinding expr = fc.getApplication().createValueBinding(el);
return expr.getValue(fc);
}
public void setValue(String el, Object value){
FacesContext fc = FacesContext.getCurrentInstance();
ValueBinding expr = fc.getApplication().createValueBinding(el);
expr.setValue(fc, value);
}
}
An example usage in a backing bean Employees.java, extends JSFBean:package com.delexian.ui.backing;
public class Employees extends JSFBean {
public Employees() {
}
public String calculate_action() {
execute("CalculateCommission");
execute("Commit");
return null;
}
}