Jsf 2 使用ajax更新托管bean,忽略所需的
每次更改Jsf 2 使用ajax更新托管bean,忽略所需的,jsf-2,primefaces,Jsf 2,Primefaces,每次更改selectOneMenu的值时,我都想更新I输出,但是当它一次更改一个不同于null的值时,我不能再指定null,我想这是由于required=“true”,但我不知道如何仅在ajax请求中避免验证 代码如下: 豆子: xhtml: <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
selectOneMenu
的值时,我都想更新I输出
,但是当它一次更改一个不同于null的值时,我不能再指定null
,我想这是由于required=“true”
,但我不知道如何仅在ajax请求中避免验证
代码如下:
豆子:
xhtml:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui">
<h:head />
<h:body>
<h:outputText id="idOutput" value="#{probeNull.probe}" />
<h:form id="form">
<p:selectOneMenu id="select" required="true" value="#{probeNull.probe}">
<f:selectItem itemLabel="Select one" itemValue="#{null}" />
<f:selectItem itemLabel="Yes" itemValue="true" />
<f:selectItem itemLabel="No" itemValue="false" />
<p:ajax update=":idOutput" />
</p:selectOneMenu>
<p:commandButton value="Save" ajax="false" action="#{probeNull.save()}" />
</h:form>
<h:form>
<p:commandButton value="Show value" ajax="false" action="#{probeNull.show()}" />
</h:form>
</h:body>
</html>
如何避免它?通过使用两个remoteCommand标记和JavaScript,您可以做您想做的事情
<p:remoteCommand name="makeSelection" process="select" update=":idOutput" />
<p:remoteCommand name="clearSelection" process="@this" update="select,:idOutput" >
<f:setPropertyActionListener value="#{null}" target="#{probeNull.probe}" />
</p:remoteCommand>
现在您可以决定使用javascript函数调用哪个函数
<p:selectOneMenu id="select" required="true" value="#{probeNull.probe}" onchange="selectFunction(this)">
...
function selectFunction(el){
//if el.value is empty you call clearSelection();
//else you call makeSelection();
}
...
功能选择功能(el){
//如果el.value为空,则调用clearSelection();
//否则调用makeSelection();
}
不要忘记删除
为什么要分配null
?这是我的代码的简化版本,我想获得布尔值,根据值在树上添加不同的值节点,或者如果值为null,则不添加任何内容。如果未选择任何内容,则选项将为null
。一旦选择了某个内容,为什么要将其设置回null并销毁用户所做的选择?如果您希望用户能够进行“无选择”,则不应将字段设置为必填字段。不,我不希望用户在提交表单时选择了Select one
选项,但我希望始终使用托管bean值更新I输出
(在selectOneMenu
中的每次更改都由ajax修改)。
<p:selectOneMenu id="select" required="true" value="#{probeNull.probe}" onchange="selectFunction(this)">
...
function selectFunction(el){
//if el.value is empty you call clearSelection();
//else you call makeSelection();
}