如何在托管bean中访问ui:param值

Tha*_*ham 6 jsf jsf-2 uiinclude

我已经看到这个问题在很多地方问了,但是没有一个得到适当的回答所以我决定再问一遍.所以,如果我有这个:如果我在A.xhtml和我

<ui:include src="B.xhtml">
    <ui:param name="formId" value="awesome Id"/>
</ui:include>
Run Code Online (Sandbox Code Playgroud)

所以B.xhtml,我可以这样做

<h:outputText value="#{formId}"/>
Run Code Online (Sandbox Code Playgroud)

当我跑步时A.xhtml,我会看到awesome Id在屏幕上打印出来.但是,如何访问formId辅助bean中的值.我看里面FacesContext.getCurrentInstance().getAttributes(),FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()我似乎无法找到它.为了更进一步,我尝试:

在里面B.xhtml,我现在有

<h:inputHidden id="hiddenFormId" value="#{formId}"/>
<h:outputText value="#{formId}"/>
Run Code Online (Sandbox Code Playgroud)

这个想法是,我可以访问的值formIdRequestParameterMap下键hiddenFormId.但是现在如果我有:

<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>
Run Code Online (Sandbox Code Playgroud)

如果我查看POST请求(在chrome或ff调试模式下),我会得到这个错误

<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>

那么如何在托管bean中访问ui:param值?

Bal*_*usC 12

<ui:param>存储的封面实际上取决于实现.在Mojarra中,它作为属性存储FaceletContext,因此可以在您的支持bean中使用,如下所示:

FaceletContext faceletContext = (FaceletContext) FacesContext.getCurrentInstance().getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
String formId = (String) faceletContext.getAttribute("formId");
Run Code Online (Sandbox Code Playgroud)

然而,价值是否可用取决于时间.如果您的后备代码在执行包含的呈现时正在运行,那么它将可用,否则它将是null.

我记得MyFaces的做法有点不同,但我不再记得这些细节,而且我现在还没有掌握它的来源.

至于你的<h:inputHidden>尝试,<h:inputHidden>它不太适合传递视图定义的隐藏参数以及表单提交.只需使用纯HTML即可.

<input type="hidden" name="hiddenFormId" value="#{formId}" />
Run Code Online (Sandbox Code Playgroud)

它将作为具有此名称的请求参数提供.