How to pass a query string parameter

jsf-2

We want to pass a variable from a backing bean in request scope of one page as query string parameter to other backing bean in view scope of the next page.

I tried to use @ManagedParam, but this signature is not found.

Is there any way to do this?

Best Answer

You probably meant to use @ManagedProperty. This isn't useable on a view scoped bean to set a request parameter, because the view scope is of a broader scope than the request scope.

The canonical JSF2 way of passing request parameters and invoking actions on them would be something like the following:

view.xhtml view:

<h:link value="Edit" outcome="edit">
    <f:param name="id" value="#{item.id}" />
</h:link>

edit.xhtml view:

<f:metadata>
    <f:viewParam name="id" value="#{edit.id}" />
    <!-- You would normally also convert/validate it here. -->
    <f:event type="preRenderView" listener="#{edit.init}" />
</f:metadata>

Edit backing bean:

@ManagedBean
@ViewScoped
public class Edit {

    private Long id;

    public void init() {
        // This method will be invoked after the view parameter is set.
    }

    // ...
}

See also:

Related Topic