自定义验证器如何知道单击了哪个commandButton [英] How can a custom-validator know which commandButton was clicked

查看:145
本文介绍了自定义验证器如何知道单击了哪个commandButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单有几个提交"按钮, 而某些字段的验证取决于所按下的字段. 如何在我的自定义验证器中找到答案?

my form has several "submit" buttons, and the validation of some of the fields depends on which was pressed. How can I find that out in my custom validator?

推荐答案

按钮的客户端ID也作为<input type="submit">name生成.按下的<input type="submit">name=value也作为请求参数发送.因此,您只需在请求参数映射中进行检查即可.

The button's client ID get also generated as name of the <input type="submit">. The name=value of the pressed <input type="submit"> get also sent as request parameters. So you could just check for that in the request parameter map.

例如

<h:form id="formId">
    ...
    <h:commandButton id="button1" ... />
    <h:commandButton id="button2" ... />
</h:form>

,在validate()实现中具有以下内容:

with the following in validate() implementation:

Map<String, String> params = context.getExternalContext().getRequestParameterMap();

if (params.containsKey("formId:button1")) {
    // Button 1 is pressed.
}
else if (params.containsKey("formId:button2")) {
    // Button 2 is pressed.
}

这篇关于自定义验证器如何知道单击了哪个commandButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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