如何在托管bean中访问ui:param值 [英] How to access ui:param value in the managed bean

查看:130
本文介绍了如何在托管bean中访问ui:param值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

所以在B.xhtml中,我可以做到

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

当我运行A.xhtml时,我会看到awesome Id被打印在屏幕上.但是,如何在后备bean中访问formId的值.我在FacesContext.getCurrentInstance().getAttributes()FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()内部查看,但似乎无法找到它.要走得更远,所以我尝试:

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:

B.xhtml内部,我现在拥有

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

这个想法是我可以在键hiddenFormId下的RequestParameterMap中访问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>

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

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>

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

推荐答案

<ui:param>所存储的内容实际上取决于实现.在Mojarra中,它存储为 ,因此可以在您的后备bean中使用,如下所示:

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");

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

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.

我记得MyFaces所做的工作有所不同,但是我不记得详细信息了,我现在没有手头资料.

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.

关于您的<h:inputHidden>尝试,<h:inputHidden>并不完全适合将视图定义的隐藏参数与表单提交一起传递.只需使用纯HTML即可.

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.

这篇关于如何在托管bean中访问ui:param值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆