Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF commandButton URL parameters

Tags:

jsf

I would like to make a button which navigates to a different URL and pass some request parameters through in the URL. The outputLink works but I would like a button, the commandButton looks good but I can pass parameters.

Is there a solution?

like image 896
DD. Avatar asked Jun 09 '10 09:06

DD.


2 Answers

The h:commandButton doesn't fire a GET request, but a POST request, so you can't use it. If you're already on JSF 2.0 and the target page is in the same context, then you can use the h:button for this:

<h:button value="press here" outcome="otherViewId">
    <f:param name="param1" value="value1" />
    <f:param name="param2" value="value2" />
</h:button>

(no h:form is required here as in h:outputLink). This will create a button which goes to otherViewId.jsf?param1=value1&param2=value2.

But if you're not on JSF 2.0 yet, then your best is just to grab CSS to style the link like a button.

<h:outputLink styleClass="button">

with something like

a.button {
    display: inline-block;
    background: lightgray;
    border: 2px outset lightgray;
    cursor: default;
}
a.button:active {
    border-style: inset;
}
like image 176
BalusC Avatar answered Oct 30 '22 07:10

BalusC


With the button you associate an action, which is a method in the backing bean You can set params in the backing bean and read them when you press the button, from the method linked to action. The action method should return a String, which will be read by the Navigation Handler to check if it has to move to a new page, according to the configuration in the faces-config.xml.

<h:form>
    <h:commandButton value="Press here" action="#{myBean.action}">
        <f:setPropertyActionListener target="#{myBean.propertyName1}" value="propertyValue1" />
        <f:setPropertyActionListener target="#{myBean.propertyName2}" value="propertyValue2" />
    </h:commandButton>
</h:form>

Backing bean:

package mypackage;


public class MyBean {

    // Init --------------------------------------------------------------------------------------

    private String propertyName1;
    private String propertyName2;

    // Actions -----------------------------------------------------------------------------------

    public void action() {
        System.out.println("propertyName1: " + propertyName1);
        System.out.println("propertyName2: " + propertyName2);
    }

    // Setters -----------------------------------------------------------------------------------

    public void setPropertyName1(String propertyName1) {
        this.propertyName1 = propertyName1;
    }

    public void setPropertyName2(String propertyName2) {
        this.propertyName2 = propertyName2;
    }

}

This example is taken from here (BalusC blog, probably he will come and tell you to check that link but I'm faster! :P)

Of course to achive this the bean has to be set as session scoped. If you want it to be request scoped you can follow the steps here

like image 42
pakore Avatar answered Oct 30 '22 07:10

pakore