2
votes

With requestscoped beans managed by JSF (@Managedbean) you can get the value of a request parameter with @ManagedProperty("#{param.id}").

If the request is a GET request, you can get the value of a request parameter with f:viewParam.

Is there a special mechanism to get the value of a request parameter when the bean is CDI managed (@Named) and the request is a POST request? I only know the Java code

FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
1

1 Answers

1
votes

The <f:viewParam> works as good in POST requests.

You should only keep in mind that this runs during apply request values phase only, not during render response phase. So, if you're actually navigating on an action method and expecting the parameter to be set as view parameter of the target page, then you're wrong. It will only be set on the view where the POST request is actually submitting to.

As an evidence that the <f:viewParam> works on POST as well, here's a test snippet:

<f:metadata>
    <f:viewParam name="foo" />
</f:metadata>
<h:form>
    <h:commandButton value="submit">
        <f:param name="foo" value="bar" />
    </h:commandButton>
</h:form>
<p>foo: #{foo}</p>

Pressing the submit button will show up foo: bar.

You need to solve your concrete functional requirement in a different manner. As you didn't tell anything about the concrete functional requirement in your question, it's not possible to give an elaborate answer on that. Here are at least some hints:

  1. @Inject target bean and set it as property during action method.
  2. Use <f:setPropertyActionListener>.
  3. Use flash scope.
  4. Send a redirect with parameter in query string.