0

我有以下内容h:commandButton

<h:commandButton action="#{myBean.myAction}" value="Send">
    <f:param name="myFlag" value="true" />
</h:commandButton>

我想获得对ValidatormyFlag内部的访问权限,该Validator附加到另一个带有.f:validator

不幸的是,当我想通过FacesContext检索参数时,我只会null返回。

是否仅在调用所有验证器后才发送参数?

4

1 回答 1

2

仅自JSF 2.0 起才支持<f:param>内部<h:commandButton>,但您使用的是 JSF 1.2。然后<f:param>仅支持<h:commandLink>.

<h:commandLink action="#{myBean.myAction}" value="Send">
    <f:param name="myFlag" value="true" />
</h:commandLink>

还有其他选择,例如<h:inputHidden>or <f:setPropertyActionListener>or <f:attribute>or 在这种情况下可能只是普通<input type="hidden">的(隐藏的输入组件和动作侦听器只会在更新模型值期间设置它们的值,这比验证阶段晚;属性标签最好设置在调用验证器的组件)。由于功能要求不明确,因此无法提出最佳替代方案。


根据评论进行更新,显然您只需要知道是否按下了特定按钮;在这种情况下,只需给它和父表单一个固定的 ID

<h:form id="form">
    <h:commandButton id="send" ...>

这样,它将有一个固定的请求参数名称,form:send您可以在验证器中检查它:

if (externalContext.getRequestParameterMap().containsKey("form:send")) {
    // Send button is pressed.
}

顺便说一句,您还可以在required验证器中进行如下检查:

<h:inputText ... required="#{not empty param['form:send']}" />

也可以看看:

于 2011-11-22T12:36:33.347 回答