如果两个下拉列表都设置为必需,如何根据另一个下拉列表设置值
谁能告诉我如何根据另一个
自动设置
(或任何其他组件)的值(如果存在空元素)表单上的“必需”设置为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将
ajaxSingle="true"
添加到 someSelect a4j:support 元素?删除immediate="true"
Have you tried adding
ajaxSingle="true"
to the someSelect a4j:support element? Remove theimmediate="true"
以下解决方案适用于 JSF 2.0
f:ajax 调用将在“someSelect”发生更改时向服务器提交 ajax 请求。仅“someSelect”组件的模型更新将会发生(因为execute="@this")。在渲染响应阶段,仅“anotherSelect”将被重新渲染(从而使用更新的国家/地区名称调用 testController.stateNamesSelectItems)。
所有这些的结果是,所选国家/地区的状态将在国家/地区发生更改时更新......以 ajax 方式。
希望这有帮助。
The following solution works in JSF 2.0
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.