jsf-2managed-bean

Insert query string parameter into property of managed bean


I wish to pre-populate a jsf form based on the parameters in a query string. How do I go about this?


Solution

  • Use the f:viewParam utility. Having this url:

    /myForm.xhtml?nameField=Anthony
    

    You can set the view parameter before the form itself gets rendered. Using this code in your form view:

    <f:metadata>
        <f:viewParam name="nameField"
            value="#{formBean.name}" />
    </f:metadata>
    <h:form>
        <h:inputText value="#{formBean.name}" />
        <h:commandButton value="Send name" action="#{formBean.sendName}" />
    </h:form>
    

    The value you have specified will be set as default for the input.

    See also: