Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF ViewParam from CommandButton

Tags:

jsf

primefaces

I'm using JSF and Primefaces and my question is:

I have a page (page1.jsf) that recive a view param with a list of strings (space delimited):

<f:metadata>
 <f:viewParam name="list" value="#{bean1.list}" converter="listConverter"/>
</f:metadata>

The listConverter convert the string into a list of individual words. If I access the page through url (eg: page1.jsf?list=word1 word2 word3") everything works just fine!

But now I'm trying to use another page (page2.jsf) to create that list of terms. I'm using a Primeface DataTable, following this example: http://www.primefaces.org/showcase/ui/datatableRowSelectionRadioCheckbox.jsf

I want tomake possible to the user to select multiple rows (checkbox Primeface example) and then press a button that will redirect to page1.jsf and also passes the list of selected items as parameter (eg. using the Primeface showcase example, pass a list of the selected car models).

I'm trying to do this:

<p:commandButton action="page1?faces-redirect=true&amp;includeViewParams=true" >
 <f:attribute name="list" value="#{bean2.convertSelectedItemsToString()}" />
</p:commandButton>

or this:

<p:commandButton action="page1?faces-redirect=true&amp;includeViewParams=true" >
 <f:param name="list" value="#{bean2.convertSelectedItemsToString()}" />
</p:commandButton>

where bean2 has a selectedItems[] with the objects selected.

Needless to say... It's not working.

Any help? Thanks in advance.

like image 339
userk Avatar asked Sep 08 '12 18:09

userk


1 Answers

A command button does not use an attribute or parameter to construct the URL to which redirection happens.

For this you normally use the h:commandLink or optionally the h:outputLink, but you need to be aware that these will render the parameters as they were at the time the page was requested. If the user makes changes to the selection after that, those won't be reflected in the link.

An alternative that would give you the latest selection, is using an action method and construct the redirect URL there.

E.g.

<p:commandButton action="#{bean2.navigateWithSelection}" />

And then in your bean:

public String navigateWithSelection() {
    return "page1?faces-redirect=true&" + convertSelectedItemsToString();
}

As per the comments, if bean2 is a generic bean that has no knowledge about the page, you can pass in the target page as a parameter:

<p:commandButton action="#{bean2.navigateWithSelection('page1')}" />

And then in your bean again:

public String navigateWithSelection(String target) {
    return target + "?faces-redirect=true&" + convertSelectedItemsToString();
}
like image 81
Arjan Tijms Avatar answered Oct 20 '22 15:10

Arjan Tijms