问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/12533865/how-to-access-uiparam-value-in-the-managed-bean