How to access ui: param value in managed bean

I saw that this question asked a lot, but no one received the proper answer, so I decided to ask again. Therefore, if I have this: if I'm 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 will see awesome Id to print on the screen. However, how do I access the formId value in the bean base. I look inside FacesContext.getCurrentInstance().getAttributes() and FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() , and I just can't find it. To go a little further, I try:

Inside B.xhtml now have

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

The idea is that I can access the formId value in RequestParameterMap under the 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 into 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 do I access the ui: param value in a managed bean?

+4
source share
1 answer

Where <ui:param> is under the protective covers actually depends on the implementation. In Mojarra, it is stored as a FaceletContext attribute and is thus available in your bean support as follows:

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

Whether a value is available, however, depends on time. If your executable code is running while the include rendering is running, then it will be available, otherwise it will be null .

I remember that MyFaces does it a little differently, but I don’t remember the details anymore and I don’t have its source now.

As for your attempt to <h:inputHidden> , then <h:inputHidden> not suitable for the sole purpose of passing certain hidden parameters along with the submit form. Just use plain HTML.

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

It will be available as a query parameter with that name.

+9
source

Source: https://habr.com/ru/post/1435531/


All Articles