JSF 2:当需要下拉列表且默认条目为null时,无法在下拉列表元素中选择默认条目 [英] JSF 2: Cannot choose default entry in dropdown element when dropdown is required and default entry is null

查看:69
本文介绍了JSF 2:当需要下拉列表且默认条目为null时,无法在下拉列表元素中选择默认条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSF 2代码:

I have the following JSF 2 code:

    <p:selectOneMenu id="dropdown" value="#{data.selection}" required="true" converter="selectOneMenuConverter">
            <f:selectItem itemLabel="Select one..." itemValue="" noSelectionOption="true" />
            <f:selectItems value="#{data.entries}" var="entry" itemLabel="#{entry.name}" itemValue="#{entry}" />
            <p:ajax update="display" event="change" />
    </p:selectOneMenu>

    <h:panelGroup id="display">
            <h:outputText value="#{data.selection}" />
    </h:panelGroup>

当我从下拉列表中选择一个值时,一切都会按预期进行. 当用户通过选择选择一个"来取消选择"条目时,JSF抱怨说这是不可能的,因为需要选择菜单.

Everything works as expected when I choose a value from the dropdown. When the user "deselects" an entry by choosing "Select One", JSF complains that this is not possible because the selectonemenu is required.

问题出在那儿,p:ajax进行了部分提交以触发验证. Instant = true也不起作用,因为万一立即发生在输入字段上(如selectonemenu),将执行验证. 仅当用户按下页面底部的继续"按钮(代码中未显示)时,验证才会发生

The problem comes from there that the p:ajax makes a partial submit that triggers validation. Immediate=true does also not work because in case the immediate happens on an input field (like selectonemenu is) a validation is performed. The validation shall only happen when the user presses the "go on" button on the bottom of the page (not shown in code)

另外,给定的转换器将字符串转换为对象,并且对于默认值,它返回null(这也是不选择"域中的期望值).

Further the given converter converts the Strings to Objects and for the default value it returns null (that's also the expected value within the domain for "no selection").

所以我的问题是我必须做什么才能完成我的案子. 对于我来说,这是一个标准案例,我无法想象对此没有解决方案.

So my question is what I must do to fulfill my case. For my this is a standard case and I cannot imagine that there is no solution for this.

有什么想法吗?

最诚挚的问候, 弗洛里安

Best regards, Florian

推荐答案

仅当用户按下页面底部的继续"按钮(代码中未显示)时,验证才会发生

然后只需告诉下拉列表的required属性即可执行此操作,而不是对true进行硬编码.

Then just tell the dropdown's required attribute to do exactly that instead of hardcoding a true.

<h:form id="form">
    <p:selectOneMenu ... required="#{not empty param['form:go']}">
        ...
    </p:selectOneMenu>

    ...

    <p:commandButton id="go" ... />
</h:form>

#{not empty param['form:go']}仅在通过提交按钮实际提交表单时才评估true,在特定示例中,该提交按钮的客户端ID为form:go.如果您也不喜欢对客户端ID进行硬编码,请按以下方式引用它:

The #{not empty param['form:go']} will only evaluate true when the form submit has actually been taken place by the submit button which has the client ID form:go in the particular example. If you don't like hardcoding client IDs either, then reference it as follows:

<h:form>
    <p:selectOneMenu ... required="#{not empty param[go.clientId]}">
        ...
    </p:selectOneMenu>

    ...

    <p:commandButton binding="#{go}" ... />
</h:form>

这篇关于JSF 2:当需要下拉列表且默认条目为null时,无法在下拉列表元素中选择默认条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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