FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext el = facesContext.getELContext();
Application app = facesContext.getApplication();
ExpressionFactory ef = app.getExpressionFactory();
ValueExpression ve = ef.createValueExpression(el, expression, type);
ve.setValue(el, value);
where expression is the EL expression in
string format and type is the Class, value is any Object passing the IS-A
test.
For your example, before invoking the application phase and translating the logical outcome of the next page, you can have something like this.
public String nextPage() {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext el = facesContext.getELContext();
Application app = facesContext.getApplication();
ExpressionFactory ef = app.getExpressionFactory();
ValueExpression ve = ef.createValueExpression(el, "#{beanb}", MyBeanb.class);
MyBeanb b = new MyBeanb();
// alter the state of b here
ve.setValue(el, b);
return "nextPage"
}
Whenever JSF encounters an EL, it first looks in the scope defined in the faces-config.xml. If it does not find any instance of the managed bean mapped to the expression "#{beanb}", it invokes the constructor and set it in the scope. That is what we get when our direct to a page and it does not encounter any instance. What we are doing here is creating the instance ourselves and setting it in the context. Of course, the same thing is achieved if we used this line of code -
MyBeanb b = new MyBeanb();
// alter the state of b here
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext ec = facesContext.getExternalContext();
ec.getRequestMap().put("beanb", b);
The second approach considers that scope of the managed bean. Using the first approach, the ValueExpression is automatically mapped to the defined scope in the faces-config.xml
I hope this helps.
Regards.
[ August 06, 2008: Message edited by: Jerwin Louise Uy ]