如果两个下拉列表都设置为必需,如何根据另一个下拉列表设置值

发布于 2024-09-03 21:42:23 字数 1951 浏览 0 评论 0原文

谁能告诉我如何根据另一个 自动设置 (或任何其他组件)的值(如果存在空元素)表单上的“必需”设置为“true”?如果设置 则不会更改任何内容,因为未设置更改的值。但是如果没有 immediate="true" 我总是收到这样的消息:这个或那个元素不能为空。这是不起作用的代码示例。

<h:outputLabel value="* #{msg.someField}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:selectOneMenu id="someSelect"
            value="#{MyBean.someObj.someId}"
            required="true" label="#{msg.someField}"
            >
        <a4j:support event="onchange" reRender="anotherSelect" limitToList="true" immediate="true"/>
        <f:selectItem itemValue=""/>
        <f:selectItems value="#{MyBean.someList}"/>
    </h:selectOneMenu>
    <rich:message for="someSelect" styleClass="redOne"/>
</h:panelGrid>

<h:outputLabel value="* #{msg.anotherField}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:selectOneMenu id="anotherSelect"
            value="#{MyBean.someObj.anotherId}"
            required="true" label="#{msg.anotherField}"
            >
        <f:selectItem itemValue=""/>
        <f:selectItems value="#{MyBean.anotherList}"/>
    </h:selectOneMenu>
    <rich:message for="anotherSelect" styleClass="redOne"/>
</h:panelGrid>

<h:outputLabel value="* #{msg.name}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:inputText id="myName" value="#{MyBean.someObj.myName}" 
            required="true" label="#{msg.name}"/>
    <rich:message for="myName" styleClass="redOne"/>
</h:panelGrid>

因此,在这里(我重复一遍),如果我尝试更改“someSelect”,那么“anotherSelect”应该更新其值,但事实并非如此,因为当它尝试获取“someSelect”的值时,它会变为空(如果 immediate 设置为 true)或表单验证在空元素上失败。如何跳过验证但从“someSelect”获取更改后的值?

Can anyone tell me how to automatically set <h:selectOneMenu> (or any other component) with values depending on another <h:selectOneMenu> if there empty elements with 'required' set to 'true' on the form? If to set <a4j:support event="onchange" reRender="anotherElement" immediate="true" /> then nothing is changed because changed value isn't set. But without immediate="true" I always have message that this or that element cannot be empty. Here's code example that doesn't work.

<h:outputLabel value="* #{msg.someField}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:selectOneMenu id="someSelect"
            value="#{MyBean.someObj.someId}"
            required="true" label="#{msg.someField}"
            >
        <a4j:support event="onchange" reRender="anotherSelect" limitToList="true" immediate="true"/>
        <f:selectItem itemValue=""/>
        <f:selectItems value="#{MyBean.someList}"/>
    </h:selectOneMenu>
    <rich:message for="someSelect" styleClass="redOne"/>
</h:panelGrid>

<h:outputLabel value="* #{msg.anotherField}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:selectOneMenu id="anotherSelect"
            value="#{MyBean.someObj.anotherId}"
            required="true" label="#{msg.anotherField}"
            >
        <f:selectItem itemValue=""/>
        <f:selectItems value="#{MyBean.anotherList}"/>
    </h:selectOneMenu>
    <rich:message for="anotherSelect" styleClass="redOne"/>
</h:panelGrid>

<h:outputLabel value="* #{msg.name}: "/>
<h:panelGrid cellpadding="0" cellspacing="0">
    <h:inputText id="myName" value="#{MyBean.someObj.myName}" 
            required="true" label="#{msg.name}"/>
    <rich:message for="myName" styleClass="redOne"/>
</h:panelGrid>

So, here (I repeat), if I try to change 'someSelect' then 'anotherSelect' should update its values but it doesn't because either when it tries to get value of 'someSelect' it gets null (if immediate set to true) or form validation fails on empty elements. How can I skip validation but get this changed value from 'someSelect'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倚栏听风 2024-09-10 21:42:24

您是否尝试过将 ajaxSingle="true" 添加到 someSelect a4j:support 元素?删除 immediate="true"

Have you tried adding ajaxSingle="true" to the someSelect a4j:support element? Remove the immediate="true"

纸短情长 2024-09-10 21:42:24

以下解决方案适用于 JSF 2.0

<h:outputLabel
    value="* Country: "/>
<h:selectOneMenu
    id="someSelect"
    value="#{testController.countryName}"
    required="true">
    <f:selectItem
        itemLabel="Select Country"
        itemValue=""/>
    <f:selectItems
        value="#{testController.countryNamesSelectItems}"/>
    <!-- This will only update "someSelect" and repaint "anotherSelect" -->
    <f:ajax
        execute="@this"
        render="anotherSelect"/>
</h:selectOneMenu>

<h:outputLabel
    value="* State: "/>
<h:selectOneMenu
    id="anotherSelect"
    value="#{testController.stateName}"
    required="true">
    <f:selectItem
        itemLabel="Select State"
        itemValue=""/>
    <f:selectItems
        value="#{testController.stateNamesSelectItems}"/>
</h:selectOneMenu>

f:ajax 调用将在“someSelect”发生更改时向服务器提交 ajax 请求。仅“someSelect”组件的模型更新将会发生(因为execute="@this")。在渲染响应阶段,仅“anotherSelect”将被重新渲染(从而使用更新的国家/地区名称调用 testController.stateNamesSelectItems)。

所有这些的结果是,所选国家/地区的状态将在国家/地区发生更改时更新......以 ajax 方式。

希望这有帮助。

The following solution works in JSF 2.0

<h:outputLabel
    value="* Country: "/>
<h:selectOneMenu
    id="someSelect"
    value="#{testController.countryName}"
    required="true">
    <f:selectItem
        itemLabel="Select Country"
        itemValue=""/>
    <f:selectItems
        value="#{testController.countryNamesSelectItems}"/>
    <!-- This will only update "someSelect" and repaint "anotherSelect" -->
    <f:ajax
        execute="@this"
        render="anotherSelect"/>
</h:selectOneMenu>

<h:outputLabel
    value="* State: "/>
<h:selectOneMenu
    id="anotherSelect"
    value="#{testController.stateName}"
    required="true">
    <f:selectItem
        itemLabel="Select State"
        itemValue=""/>
    <f:selectItems
        value="#{testController.stateNamesSelectItems}"/>
</h:selectOneMenu>

The f:ajax call will submit an ajax request to the server onchange of "someSelect". The model update for only the "someSelect" component will happen (because of execute="@this"). In the render response phase, only "anotherSelect" will be re-rendered (thus invoking testController.stateNamesSelectItems with the updated country name).

As a result of all this, the states of the selected country will get updated as an when country is changed...the ajax way.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文