Requirement – how to set value of attribute value in binding…
Solutions – you can use this expression –
JsfUtils.setExpressionValue("#{ManagedBean.empName}", "Vinay");
You have to add this method in JsfUtils class.
Note to call the getters and setters via your EL expression, you don’t include the get/set prefix.
public static void setExpressionValue(String expression, Object newValue) { FacesContext facesContext = getFacesContext(); Application app = facesContext.getApplication(); ExpressionFactory elFactory = app.getExpressionFactory(); ELContext elContext = facesContext.getELContext(); ValueExpression valueExp = elFactory.createValueExpression(elContext, expression, Object.class); //Check that the input newValue can be cast to the property type //expected by the managed bean. //If the managed Bean expects a primitive we rely on Auto-Unboxing //I could do a more comprehensive check and conversion from the object //to the equivilent primitive but life is too short Class bindClass = valueExp.getType(elContext); if (bindClass.isPrimitive() || bindClass.isInstance(newValue)) { valueExp.setValue(elContext, newValue); } }
Happy coding in Techartifact with Vinay…