繁体   English   中英

将setter方法中组件的ID传递给ManagedBean

[英]Pass id of component in setter method to ManagedBean

好吧,我的ManagedBean中有一个通用的setter方法。 此通用方法应将具有组件ID的属性的值添加到“地图”中,请参见下面的XHTML页面中的组件:

<h:outputLabel value="Data Fechamento: " />
                    <p:calendar pattern="dd/MM/yyyy" required="true"
                        requiredMessage="A Data de Fechamento é obrigatória"
                        value="#{relatorioMB.param}">
                    </p:calendar>

在我的ManagedBean中,看到设置方法“ setParam()”:

public void setParam(Object value){
    String compId = "dataFechamento"; //here should be a generic form to get component id
    AppContext.getInstance().addAttribute(compId, value);
    }

XHTML页面中的所有组件都将调用“ setParam()”,并将新属性添加到“ AppContext”类。 我该怎么做 ?

编辑1

我尝试在setter方法中使用getCurrentComponent,但它返回的是“ panelGrid”(父级)而不是Calendar。

UIComponent ui = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());

您可以创建一个处理程序bean,该处理程序将采用所有参数并将它们放入数组中。 在那里,您可以使用循环为数组中的每个元素调用setParam()

例如:

在页面上,如果您需要来自inputText的信息:

<h:inputText id="id" value="#{Handler.id}"></h:inputText>

这会将处理程序上的值设置为可以使用的字段。

然后,当您有一个按钮时,请从处理程序中调用一个操作,例如:

<h:commandButton action="#{Handler.doAction}" />

然后,就像从处理程序中调用方法一样简单!

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM