I’m trying to add JSF <h:commandButtons> dynamically to my webpage, and so far I have them displaying, but I cannot set the action with parameters, like I could in a static page:
action="#{bean.function(parameter)}". (this is of course using EL-2.2)
Looking around I find that I have to create a MethodExpression, but this is obscure to me and I haven’t been able to find much information on this. If someone could shine a light through the fog and explain how this can be done, it would be greatly appreciated.
EDIT: so now I have this
public void displayNode( String childName ){
//lots of messy code instantiating JSF components
if( activeEmployee.getParent() != null ){
HtmlCommandButton parent = new HtmlCommandButton();
HtmlOutputText parentLabel = new HtmlOutputText();
parentLabel.setId("label" + count++); //I really hate having to use count
parentLabel.setValue( "Parent: " );
parent.setId("Parent" + count++);
String parentName = activeEmployee.getParent().getName();
parent.setValue( parentName );
MethodExpression expression = createMethodExpression("#{tree.displayNode('" + parentName + "')}",
null, String.class);
parent.setActionExpression( expression );
newDiv.getChildren().add( parentLabel );
newDiv.getChildren().add( parent );
}
Use
ExpressionFactory#createMethodExpression().Here’s a convenience method:
The following action method examples:
can then be created as follows:
Note that the EL expression is exactly the same as you would use in the normal view file.
Update here’s an SSCCE which works fine for me with Mojarra 2.1.12 on Tomcat 7.0.27.
Unrelated to the concrete problem, all of above is a poor practice. See also How does the 'binding' attribute work in JSF? When and how should it be used?