【问题标题】:How to pass second value in JSF validator?如何在 JSF 验证器中传递第二个值?
【发布时间】:2013-02-04 20:52:33
【问题描述】:

我有一个简单的 JSF 输入表单进入编辑页面和检查重复值的验证器:

<td>Name</td>
<td>
    <h:outputText value="#{ud.name}"
                  rendered="#{not DCProfileTabGeneralController.editable}" />
    <h:inputText id="dcname" value="#{ud.name}" rendered="#{DCProfileTabGeneralController.editable}"
                 validator="#{ValidatorDatacenterController.validateDatacenterName}" autocomplete="off">
        <f:ajax event="blur" render="dcnamevalidator" />
    </h:inputText>
    <h:message id="dcnamevalidator" for="dcname" />
</td>

public void validateDatacenterName(FacesContext context, UIComponent component, Object value){
....
}

我很感兴趣是否有任何可能的方式来发送第二个值,该值将用于验证过程?

【问题讨论】:

  • 从您的实际代码示例来看,似乎没有理由发送另一个值。最好解释一下您的功能要求以获得更好的指导或您向验证器发送第二个参数的动机。

标签: jsf jsf-2


【解决方案1】:

&lt;f:attribute&gt; 浮现在脑海。向 inputtext 添加一个并在验证器中检索它。

<h:inputText id="dcname".....
<f:attribute name="param" value="myparam" /> 
</h:inputText>

还有:

String param = (String) component.getAttributes().get("param"); 

可以从EL获取值。祝你好运

【讨论】:

    【解决方案2】:

    您可以添加一个 postValidate 事件来验证多个字段,例如

    <f:event listener="#{bean.validationMethod}" type="postValidate" />
    

    这应该在模型更新之前触发,您可以使用

    获取不同组件的新值
    FacesContext fc = FacesContext.getCurrentInstance(); 
    UIComponent components = event.getComponent();
    UIInput param1 = (UIInput) components.findComponent("param1");
    UIInput param2 = (UIInput) components.findComponent("param2");
    

    如果验证失败,调用 FacesContext renderResponse 方法跳过模型更新。

    【讨论】:

    • 这个我用过一些。我喜欢使用 f:ajax ,所以我不需要找到组件和那些东西。这个 ofc 需要比 requestscoped 更长的范围
    猜你喜欢
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 2011-04-28
    相关资源
    最近更新 更多