7

I have seen this question ask around a lots, however, none was properly answered so I decided to ask again. So if I have this: if I am in A.xhtml and I

<ui:include src="B.xhtml">
    <ui:param name="formId" value="awesome Id"/>
</ui:include>

so in B.xhtml, I can do this

<h:outputText value="#{formId}"/>

when I run A.xhtml, I would see awesome Id get printed on the screen. However how do I access the value of formId in the backing bean. I look inside FacesContext.getCurrentInstance().getAttributes() and FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() and I just cannot seems to locate it. To go a bit further, so I try:

Inside B.xhtml, I now have

<h:inputHidden id="hiddenFormId" value="#{formId}"/>
<h:outputText value="#{formId}"/>

the idea is that I can access the value of formId in the RequestParameterMap under key hiddenFormId. But now if I have:

<h:form id="myForm">
        <ui:include src="B.xhtml">
            <ui:param name="formId" value="awesome Id"/>
        </ui:include>
        <a4j:commandButton render="myForm" value="My Button"/>
</h:form>

then I would get this erro if I look inside the POST request (when inside chrome or ff debug mode)

<partial-response><error><error-name>class javax.faces.component.UpdateModelException</error-name><error-message><![CDATA[/B.xhtml @9,61 value="${formId}": /index.xhtml @27,61 value="awesome Id": Illegal Syntax for Set Operation]]></error-message></error></partial-response>

so How to access ui:param value in the managed bean?

Thang Pham
  • 35,825
  • 73
  • 192
  • 279
  • 1
    Is [this question](http://stackoverflow.com/questions/5394304/passing-values-with-uiparam-and-access-them-in-backing-bean) something similar to yours? – ElderMael Sep 21 '12 at 16:11
  • @mael: I am trying his code now, but there are things I dont get. if you understand his/her codes can you help me bit more please? is `hiddenValue` the id of the outputLabel, and what is `UiTreeWalker`? – Thang Pham Sep 21 '12 at 16:25

1 Answers1

12

Where the <ui:param> is under the covers stored is actually implementation dependent. In Mojarra it's stored as an attribute of the FaceletContext and thus available in your backing bean as follows:

FaceletContext faceletContext = (FaceletContext) FacesContext.getCurrentInstance().getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
String formId = (String) faceletContext.getAttribute("formId");

Whether the value would be available is however subject to timing. If your backing code is running while executing the rendering of the include, then it'll be available, else it'll be null.

I recall that MyFaces does it a bit differently, but I don't recall the details anymore and I don't have its source at hand right now.

As to your <h:inputHidden> attempt, the <h:inputHidden> isn't well suited for the sole purpose of passing view-definied hidden parameters along with the form submit. Just use plain HTML instead.

<input type="hidden" name="hiddenFormId" value="#{formId}" />

It'll be available as a request parameter with exactly this name.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • 1
    Thank you BalusC. There is a small problem I forget to mention, that I need the value of formId in my validation phase. So when I access that value from FaceletContext, it is null. Previous if I use h:inputHidden, this information appear in my RequestParameterMap so I can get the value correct, but I switch to I cannot see this value in my RequestParameterMap inside my validation phase anymore. Any idea why, BalusC? – Thang Pham Sep 21 '12 at 17:26
  • Did you get the hidden input value by the exact name `"hiddenFormId"`? Note that there's no form ID prefix in the name. – BalusC Sep 21 '12 at 17:33
  • oh I am sorry, it works, it is `name="hiddenFormId"` but I put `id="hiddenFormId"`. Thank you very much. – Thang Pham Sep 21 '12 at 17:35