Mateusz Dymczyk Mateusz Dymczyk - 1 year ago 118
Java Question

Passing a parameter with h:commandButton

I have a a4j:commandButton which is supposed to redirect me to an appropriate "Edit" page based on an Id, which I wanted to pass as a parameter, something like this:

<h:commandButton action="/details.jsf?faces-redirect=true" value="details">
<f:attribute name="id" value="#{}" />

The problem is, it doesn't work. I also tried replacing f:attribute with "f:param name="id" value="#{}" ", but it also failed. The only thing I got to work is an outputLink:

<h:outputLink value="/details.jsf">
<f:param name="id" value="#{}" />

But I'm not really happy with a link, so is there a way to make the commandButton work?

Oh and I also have a bean which is supposed to get that "id" after the redirect:

public void init(){
id= resolve("id");

Answer Source

Have a look at this article about communication in JSF, by BalusC

f:param only works with h:commandLink and h:outputLink.

You can use an input hidden:

    <h:commandButton action="/details.jsf?faces-redirect=true" value="details"/>
    <input type="hidden" name="id" value="#{}" />

And then in your faces-config, I guess is request scoped. If you use the annotations of JSF2, just translate this to the proper annotations.


You need obviously to have getters and setters for that field in the backing bean.

or try to "paint" the link as a button through CSS.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download