【问题标题】:How to access ui:param value in the managed bean如何访问托管 bean 中的 ui:param 值
【发布时间】:2012-09-21 15:48:50
【问题描述】:

我看到这个问题问了很多,但是没有一个得到正确的回答,所以我决定再问一次。所以如果我有这个:如果我在A.xhtml 并且我

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

所以在B.xhtml,我可以做到这一点

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

当我运行A.xhtml 时,我会看到awesome Id 打印在屏幕上。但是,我如何访问支持 bean 中的 formId 的值。我查看FacesContext.getCurrentInstance().getAttributes()FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() 内部,但我似乎无法找到它。为了更进一步,所以我尝试:

B.xhtml里面,我现在有了

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

我的想法是我可以在键 hiddenFormId 下的 RequestParameterMap 中访问 formId 的值。但现在如果我有:

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

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

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

so 如何访问托管 bean 中的 ui:param 值?

【问题讨论】:

  • this question 是否与您的相似?
  • @mael:我现在正在尝试他的代码,但有些东西我没有得到。如果你理解他/她的代码,你能帮我多一点吗? hiddenValue是outputLabel的id,UiTreeWalker是什么?

标签: jsf jsf-2 uiinclude


【解决方案1】:

&lt;ui:param&gt; 的隐藏位置实际上取决于实现。在 Mojarra 中,它被存储为 FaceletContext 的一个属性,因此可以在您的支持 bean 中使用,如下所示:

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

但该值是否可用取决于时间。如果您的支持代码在执行包含的呈现时正在运行,那么它将可用,否则它将是 null

我记得 MyFaces 的做法有点不同,但我不记得细节了,我现在也没有它的来源。

至于您的&lt;h:inputHidden&gt; 尝试,&lt;h:inputHidden&gt; 不太适合将视图定义的隐藏参数与表单提交一起传递的唯一目的。只需使用纯 HTML。

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

它将作为具有该名称的请求参数提供。

【讨论】:

  • 谢谢 BalusC。有个小问题我忘了提,我在验证阶段需要 formId 的值。因此,当我从 FaceletContext 访问该值时,它为空。以前,如果我使用 h:inputHidden,此信息会出现在我的 RequestParameterMap 中,因此我可以获得正确的值,但我切换到 我无法在验证阶段的 RequestParameterMap 中看到该值。知道为什么吗,BalusC?
  • 您是否通过确切的名称"hiddenFormId" 获得了隐藏的输入值?请注意,名称中没有表单 ID 前缀。
  • 哦,对不起,它有效,它是name="hiddenFormId",但我输入了id="hiddenFormId"。非常感谢。
猜你喜欢
  • 2013-02-16
  • 1970-01-01
  • 2012-03-24
  • 2012-09-03
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
相关资源
最近更新 更多