2

I am having below command button in generalPage.xhtml which having GeneralPageMB backing bean.

    <p:commandButton value="#{facility.facilityName}" ajax="false"
                        action="facilityHome.xhtml?faces-redirect=true&amp;includeViewParams=true">
                        <f:param name="fid" value="#{facility.id}" />
    </p:commandButton>

And I have second page facilityHome.xhtml which having the following code:

     <div style="width: 400px; height: 250px;" class="loginContainerBg">
                <h:form id="f" prependId="false">
                    <f:metadata>
                        <f:viewParam name="fid" value="#{facilityHomeMB.facilityId}"
                            converter="com.ot.common.utility.FacilityIdConverter" />
                    </f:metadata>
     </div>

facilityHome.xhtml having backing bean as FacilityHomeMB. So I am trying to get view Parameter and set to facilityId property in FacilityHomeMB bean. My problem is facilityId not getting set. What is wrong with the code ?. Any help will be appreciated.

I am using tomcat 7, Primefaces 4.0, Mojarra 2.1.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
  • Is that even legal JSF? I always thought `` can only be attached to a page, as described in the [vld](http://docs.oracle.com/javaee/7/javaserverfaces/2.2/vdldocs/facelets/f/metadata.html). Try putting that tag at the top of your view. – mabi Feb 04 '14 at 13:12
  • yes mabi you are right I placed wrongly as in vld it should be child of so I moved tag in but it is not woring.. – Shekhar Khairnar Feb 04 '14 at 13:24
  • How did you check the id is not getting set? Can you post the important parts from `facilityHomeMB` and your converter? – mabi Feb 04 '14 at 14:00

1 Answers1

1

The <p:commandButton> is the wrong tool for the job. It's intented to perform a POST request whereby the data of the enclosing form is being submitted. It's not intented for page-to-page navigation. Your concrete problem is caused because you're explicitly sending a redirect afterwards, causing the initial request with the parameter to be lost.

Better just use <p:button>. This way you also don't need a whole <h:form> anymore and you also don't need to mess with faces-redirect, etc.

<p:button value="#{facility.facilityName}" outcome="facilityHome">
    <f:param name="fid" value="#{facility.id}" />
</p:button>

That's all.

In the target page just keep the <f:metadata>, but it shouldn't be nested in a form or so. See also When using <ui:composition> templating, where should I declare the <f:metadata>? Further, you should absolutely also not use prependId="false". This is not related to the current problem, but it will make a complete mess of the JSF ajax works (can't find components to render, etc).

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you very much BaluchC you are genius. Your p:button suggestion work perfect. And I will remember not to use prependId="false". thanks again. – Shekhar Khairnar Feb 05 '14 at 08:30
  • You're welcome. JSF is easier to understand if you're familiar with HTTP and HTML basics. JSF is basically "just" a HTTP controller and HTML generator. – BalusC Feb 05 '14 at 08:32