This question already has an answer here:
这个问题已经有了答案:
- commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated 9 answers
- 命令按钮/commandLink/ajax操作/侦听器方法未调用或输入值不设置/更新9个答案。
I am using Primefaces 3.4.2.
我用的是黄金版的3.4.2。
I have the following in my JSF page
在我的JSF页面中有如下内容。
<p:selectOneMenu id="emp" value="#{mymb.emp.employeeName}"
valueChangeListener="#{mymb.handleChange}"
required="true"
style="width: 150px;">
<f:selectItem noSelectionOption="true"
itemLabel="Please Select"/>
<f:selectItems value="#{mymb.employeeList}" var="emp"
itemLabel="#{emp.employeeName}"
itemValue="#{emp.employeeNumber}"/>
<p:ajax update="sublist"/>
</p:selectOneMenu>
and in ManagedBean
而在ManagedBean
public void handleChange(ValueChangeEvent event){
System.out.println("here "+event.getNewValue());
}
The problem is valueChangeListener
is not firing, i.e. handleChange
method is not getting invoked. I tried with the following, but it is not working either.
问题是valueChangeListener并没有被触发,即handleChange方法没有被调用。我试了一下,但也没有用。
<p:ajax update="sublist" listener="#{mymb.handleChange}" />
Separate JSF page:
独立的JSF页面:
<ui:composition template="/templates/layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:define name="content">
<h:head>
</h:head>
<h:body>
<h:form id="form">
<p:panelGrid columns="6">
<h:outputLabel value="Employees" for="employees" />
<p:selectOneMenu id="employees"
value="#{mymb.employeesList}"
required="true">
<f:selectItems value="#{mymb.employeesList}" var="emp"
itemLabel="#{emp.employeeName}" />
<p:ajax listener="#{mymb.handleChange}" />
</p:selectOneMenu>
</p:panelGrid>
</h:form>
</h:body>
</ui:define>
</ui:composition>
8 个解决方案
#1
41
If you want to use valueChangeListener
, you need to submit the form every time a new option is chosen. Something like this:
如果您想使用valueChangeListener,您需要每次选择新选项时提交表单。是这样的:
<p:selectOneMenu value="#{mymb.employee}" onchange="submit()"
valueChangeListener="#{mymb.handleChange}" >
<f:selectItems value="#{mymb.employeesList}" var="emp"
itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeID}" />
</p:selectOneMenu>
public void handleChange(ValueChangeEvent event){
System.out.println("New value: " + event.getNewValue());
}
Or else, if you want to use <p:ajax>
, it should look like this:
或者,如果你想使用
<p:selectOneMenu value="#{mymb.employee}" >
<p:ajax listener="#{mymb.handleChange}" />
<f:selectItems value="#{mymb.employeesList}" var="emp"
itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeID}" />
</p:selectOneMenu>
private String employeeID;
public void handleChange(){
System.out.println("New value: " + employee);
}
One thing to note is that in your example code, I saw that the value
attribute of your <p:selectOneMenu>
is #{mymb.employeesList}
which is the same as the value
of <f:selectItems>
. The value
of your <p:selectOneMenu>
should be similar to my examples above which point to a single employee, not a list of employees.
需要注意的一点是,在您的示例代码中,我看到了
#2
15
The valueChangeListener
is only necessary, if you are interested in both the old and the new value.
如果您对旧值和新值都感兴趣,那么valueChangeListener是必需的。
If you are only interested in the new value, the use of <p:ajax>
or <f:ajax>
is the better choice.
如果您只对新值感兴趣,那么使用
There are several possible reasons, why the ajax call won't work. First you should change the method signature of the handler method: drop the parameter. Then you can access your managed bean variable directly:
有几个可能的原因,为什么ajax调用不起作用。首先,您应该更改处理程序方法的方法签名:删除参数。然后可以直接访问托管bean变量:
public void handleChange(){
System.out.println("here "+ getEmp().getEmployeeName());
}
At the time, the listener is called, the new value is already set. (Note that I implicitly assume that the el expression mymb.emp.employeeName
is correctly backed by the corresponding getter/setter methods.)
在那个时候,监听器被调用,新的值已经设置好了(注意,我默认的是el表达式mymb.emp)。employeeName得到相应的getter/setter方法的正确支持。
#3
10
<p:ajax listener="#{my.handleChange}" update="id of component that need to be rerender after change" process="@this" />
import javax.faces.component.UIOutput;
import javax.faces.event.AjaxBehaviorEvent;
public void handleChange(AjaxBehaviorEvent vce){
String name= (String) ((UIOutput) vce.getSource()).getValue();
}
#4
10
Another solution is to mix valueChangeListener, ajax and process:
另一个解决方案是混合valueChangeListener、ajax和流程:
<p:selectManyCheckbox id="employees" value="#{employees}" columns="1" layout="grid" valueChangeListener="#{mybean.fireSelection}" >
<f:selectItems var="employee" value="#{employeesSI}" />
<p:ajax event="valueChange" immediate="true" process="@this"/>
</p:selectManyCheckbox>
Method in mybean is just :
方法在mybean中是:
public void fireSelection(ValueChangeEvent event) {
log.debug("New: "+event.getNewValue()+", Old: "+event.getOldValue());
}
Like this, valueChangeEvent is very light !
像这样,valueChangeEvent是非常轻的!
PS: Works fine with PrimeFaces 5.0
PS:对prime - faces 5.0有很好的效果。
#5
4
All can be defined as in f:ajax
attiributes.
所有这些都可以定义为f:ajax attiributes。
i.e.
即。
<p:selectOneMenu id="employees" value="#{mymb.employeesList}" required="true">
<f:selectItems value="#{mymb.employeesList}" var="emp" itemLabel="#{emp.employeeName}" />
<f:ajax event="valueChange" listener="#{mymb.handleChange}" execute="@this" render="@all" />
</p:selectOneMenu>
event: it can be normal DOM Events like click
, or valueChange
事件:它可以是普通的DOM事件,如click或valueChange。
execute: This is a space separated list of client ids of components that will participate in the "execute" portion of the Request Processing Lifecycle.
执行:这是一个空间分隔的组件客户id列表,它将参与请求处理生命周期的“执行”部分。
render: The clientIds of components that will participate in the "render" portion of the Request Processing Lifecycle. After action done, you can define which components should be refresh. Id, IdList or these keywords can be added: @this
, @form
, @all
, @none
.
呈现:将参与请求处理生命周期的“呈现”部分的组件的clientIds。完成操作之后,您可以定义应该刷新哪些组件。Id, IdList或这些关键字可以添加:@this, @form, @all, @none。
You can reache the whole attribute list by following link: http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/ajax.html
您可以通过以下链接来重新定义整个属性列表:http://docs.oracle.com/javaee/6/javaserverfaces/vdldocs/facelets/f/ajax.html。
#6
2
Try using p:ajax with event attribute,
尝试使用p:ajax与事件属性,
#7
2
My problem were that we were using spring securyty, and the previous page doesn't call the page using faces-redirect=true, then the page show a java warning, and the control doesn't fire the change event.
我的问题是,我们使用spring securyty,而前一页没有使用facres -redirect=true来调用页面,然后页面显示java警告,而控件并没有触发更改事件。
Solution: The previous page must call the page using, faces-redirect=true
解决方案:前一页必须使用,faces-redirect=true。
#8
0
this works for me:
这工作对我来说:
It can be used inside the dialog, but the dialog can´t be inside any componet such as panels, accordion, etc.
里面可以使用该对话框,但是对话框´t可以在任何组合如面板、手风琴等。
#1
41
If you want to use valueChangeListener
, you need to submit the form every time a new option is chosen. Something like this:
如果您想使用valueChangeListener,您需要每次选择新选项时提交表单。是这样的:
<p:selectOneMenu value="#{mymb.employee}" onchange="submit()"
valueChangeListener="#{mymb.handleChange}" >
<f:selectItems value="#{mymb.employeesList}" var="emp"
itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeID}" />
</p:selectOneMenu>
public void handleChange(ValueChangeEvent event){
System.out.println("New value: " + event.getNewValue());
}
Or else, if you want to use <p:ajax>
, it should look like this:
或者,如果你想使用
<p:selectOneMenu value="#{mymb.employee}" >
<p:ajax listener="#{mymb.handleChange}" />
<f:selectItems value="#{mymb.employeesList}" var="emp"
itemLabel="#{emp.employeeName}" itemValue="#{emp.employeeID}" />
</p:selectOneMenu>
private String employeeID;
public void handleChange(){
System.out.println("New value: " + employee);
}
One thing to note is that in your example code, I saw that the value
attribute of your <p:selectOneMenu>
is #{mymb.employeesList}
which is the same as the value
of <f:selectItems>
. The value
of your <p:selectOneMenu>
should be similar to my examples above which point to a single employee, not a list of employees.
需要注意的一点是,在您的示例代码中,我看到了
#2
15
The valueChangeListener
is only necessary, if you are interested in both the old and the new value.
如果您对旧值和新值都感兴趣,那么valueChangeListener是必需的。
If you are only interested in the new value, the use of <p:ajax>
or <f:ajax>
is the better choice.
如果您只对新值感兴趣,那么使用
There are several possible reasons, why the ajax call won't work. First you should change the method signature of the handler method: drop the parameter. Then you can access your managed bean variable directly:
有几个可能的原因,为什么ajax调用不起作用。首先,您应该更改处理程序方法的方法签名:删除参数。然后可以直接访问托管bean变量:
public void handleChange(){
System.out.println("here "+ getEmp().getEmployeeName());
}
At the time, the listener is called, the new value is already set. (Note that I implicitly assume that the el expression mymb.emp.employeeName
is correctly backed by the corresponding getter/setter methods.)
在那个时候,监听器被调用,新的值已经设置好了(注意,我默认的是el表达式mymb.emp)。employeeName得到相应的getter/setter方法的正确支持。
#3
10
<p:ajax listener="#{my.handleChange}" update="id of component that need to be rerender after change" process="@this" />
import javax.faces.component.UIOutput;
import javax.faces.event.AjaxBehaviorEvent;
public void handleChange(AjaxBehaviorEvent vce){
String name= (String) ((UIOutput) vce.getSource()).getValue();
}
#4
10
Another solution is to mix valueChangeListener, ajax and process:
另一个解决方案是混合valueChangeListener、ajax和流程:
<p:selectManyCheckbox id="employees" value="#{employees}" columns="1" layout="grid" valueChangeListener="#{mybean.fireSelection}" >
<f:selectItems var="employee" value="#{employeesSI}" />
<p:ajax event="valueChange" immediate="true" process="@this"/>
</p:selectManyCheckbox>
Method in mybean is just :
方法在mybean中是:
public void fireSelection(ValueChangeEvent event) {
log.debug("New: "+event.getNewValue()+", Old: "+event.getOldValue());
}
Like this, valueChangeEvent is very light !
像这样,valueChangeEvent是非常轻的!
PS: Works fine with PrimeFaces 5.0
PS:对prime - faces 5.0有很好的效果。
#5
4
All can be defined as in f:ajax
attiributes.
所有这些都可以定义为f:ajax attiributes。
i.e.
即。
<p:selectOneMenu id="employees" value="#{mymb.employeesList}" required="true">
<f:selectItems value="#{mymb.employeesList}" var="emp" itemLabel="#{emp.employeeName}" />
<f:ajax event="valueChange" listener="#{mymb.handleChange}" execute="@this" render="@all" />
</p:selectOneMenu>
event: it can be normal DOM Events like click
, or valueChange
事件:它可以是普通的DOM事件,如click或valueChange。
execute: This is a space separated list of client ids of components that will participate in the "execute" portion of the Request Processing Lifecycle.
执行:这是一个空间分隔的组件客户id列表,它将参与请求处理生命周期的“执行”部分。
render: The clientIds of components that will participate in the "render" portion of the Request Processing Lifecycle. After action done, you can define which components should be refresh. Id, IdList or these keywords can be added: @this
, @form
, @all
, @none
.
呈现:将参与请求处理生命周期的“呈现”部分的组件的clientIds。完成操作之后,您可以定义应该刷新哪些组件。Id, IdList或这些关键字可以添加:@this, @form, @all, @none。
You can reache the whole attribute list by following link: http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/f/ajax.html
您可以通过以下链接来重新定义整个属性列表:http://docs.oracle.com/javaee/6/javaserverfaces/vdldocs/facelets/f/ajax.html。
#6
2
Try using p:ajax with event attribute,
尝试使用p:ajax与事件属性,
#7
2
My problem were that we were using spring securyty, and the previous page doesn't call the page using faces-redirect=true, then the page show a java warning, and the control doesn't fire the change event.
我的问题是,我们使用spring securyty,而前一页没有使用facres -redirect=true来调用页面,然后页面显示java警告,而控件并没有触发更改事件。
Solution: The previous page must call the page using, faces-redirect=true
解决方案:前一页必须使用,faces-redirect=true。
#8
0
this works for me:
这工作对我来说:
It can be used inside the dialog, but the dialog can´t be inside any componet such as panels, accordion, etc.
里面可以使用该对话框,但是对话框´t可以在任何组合如面板、手风琴等。