Passing a parameter with h:commandButton

25,965

Solution 1

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:form>
    <h:commandButton action="/details.jsf?faces-redirect=true" value="details"/>
    <input type="hidden" name="id" value="#{bean.id}" />
</h:form>

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.

<managed-bean>
    <managed-bean-name>bean</managed-bean-name>
    <managed-bean-class>mypackage.Bean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>id</property-name>
        <value>#{param.id}</value>
    </managed-property>
</managed-bean>

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.

Solution 2

Unless my JSF is extremely rusty, the action attribute on a command button or command link is used to specify the outcome string defined in your faces-config-nav file, or it should point to a method on the bean which will return an outcome (or redirect/whatever).

In your case, if you want to redirect to another page... you should define that in your config file, as a navigation link (with redirect if necessary). Then in your action button you should have something like

<h:commandButton action="showDetails" value="details">

...

<navigation-case>
        <from-outcome>showDetails</from-outcome>
        <to-view-id>/details.jsf?faces-redirect=true</to-view-id>
</navigation-case>

As an aside, the <f:atribute> tag will work, but it will only set the attribute onto the component. So if you got a hold of the command button in your bean, you could be able to get the attribute value by name. To pass a request param, use the hidden field technique like pakore mentioned

Share:
25,965
Mateusz Dymczyk
Author by

Mateusz Dymczyk

Currently hacking @ www.H2O.ai I'm predominantly a JVM guy (Java and Scala) although I do use Python here and there. I try to focus on the server side, distributed computing, machine learning and all that fun stuff. I'm currently based in Tokyo, Japan so if you ever decide to come over and want to chat about tech or Japan don't be shy and just drop me a message.

Updated on September 15, 2020

Comments

  • Mateusz Dymczyk
    Mateusz Dymczyk over 3 years

    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="#{bean.id}" />
    </h:commandButton>
    

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

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

    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:

    @PostConstruct
    public void init(){
        id= resolve("id");
    }