Passing parameter to JSF action

The <f:param> sets a HTTP request parameter, not an action method parameter. To get it, you would need to use <f:viewParam> or @ManagedProperty. In this particular case, the latter is more suitable. You only have to replace CDI annotations by JSF annotations in order to get @ManagedProperty to work:

@ManagedBean(name="bean")
@RequestScoped
public class ActionParam {

    @ManagedProperty("#{param.param}")
    private Integer param;

    public String submit() {
        System.out.println("Submit using value " + param);
        return null;
    }

}

When you’re targeting a Servlet 3.0 container (Tomcat 7, Glassfish 3, JBoss AS 6, etc) with a web.xml whose <web-app> root declaration definies Servlet 3.0, then you should be able to just pass the parameter straight into the action method by EL as that’s supported by EL 2.2 (which is part of Servlet 3.0):

<h:commandButton id="actionButton" value="Submit"
    action="#{bean.submit(123)}">
</h:commandButton>

with

public String submit(Integer param) {
    System.out.println("Submit using value " + param);
    return null;
}

If you target an old Servlet 2.5 container, then you should still be able to do this using JBoss EL. See also this answer for installation and configuration details: Invoke direct methods or methods with arguments / variables / parameters in EL

Leave a Comment