用于 valueChangeListener 的 Ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14750185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax for valueChangeListener
提问by Danubian Sailor
I'm using the p:ajaxlistener to handle value change events (because valueChangeListeneris launched on form submit):
我正在使用p:ajax侦听器来处理值更改事件(因为valueChangeListener在表单提交时启动):
<p:ajax event="change" listener="#{bean.onNameChanged}"/>
Handle method:
处理方法:
public void onNameChanged(final AjaxBehaviorEvent event)
The problem is, I can't find in AjaxBehaviorEventnor its class hierarchy the place to read the old valueof the input. Neither could I find hint in google, how to get the old value...
问题是,我在AjaxBehaviorEvent它的类层次结构中也找不到读取输入旧值的地方。我也无法在谷歌中找到提示,如何获得旧值...
How to access the old value in the p:ajax onChangeevent?
如何访问p:ajax onChange事件中的旧值?
回答by BalusC
The problem is, I can't find in AjaxBehaviorEvent nor its class hierarchy the place to read the old value of the input. Neither could I find hint in google, how to get the old value...
问题是,我在 AjaxBehaviorEvent 及其类层次结构中都找不到读取输入旧值的地方。我也无法在谷歌中找到提示,如何获得旧值...
Use a valueChangeListener.
使用一个valueChangeListener.
Unfortunatelly, valueChangeListener is invoked before p:ajax, so I don't have actual data from forms in that method, so in theory I could use valueChangeListener to remember the old value and then wait for p:ajax to process...
不幸的是,valueChangeListener 在 p:ajax 之前被调用,所以我在该方法中没有来自表单的实际数据,所以理论上我可以使用 valueChangeListener 来记住旧值,然后等待 p:ajax 处理......
Queue the value change event to the invoke application phase.
将值更改事件排队到调用应用程序阶段。
public void valueChangeListenerMethod(ValueChangeEvent event) {
if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
event.queue();
return;
}
// Do your original job here.
// It will only be invoked when current phase ID is INVOKE_APPLICATION.
}
回答by Matt Handy
The ValueChangeListener should work this way:
ValueChangeListener 应该这样工作:
The view:
风景:
<h:form>
<h:inputText value="#{sessionBean.hello}"
valueChangeListener="#{sessionBean.valueChangeListener}">
<p:ajax/>
</h:inputText>
</h:form>
The bean:
豆:
public void valueChangeListener(ValueChangeEvent e) {
System.out.println("valueChangeListener invoked:"
+ " OLD: " + e.getOldValue()
+ " NEW: " + e.getNewValue());
}
The above code will print if I change the text field from "hello" to "world":
如果我将文本字段从“hello”更改为“world”,则会打印上面的代码:
valueChangeListener invoked: OLD: hello NEW: world
valueChangeListener 调用:旧:你好新:世界
回答by kolossus
You could try the following:
您可以尝试以下操作:
Implement the value change event in your bean
public void processValueChange(ValueChangeEvent e){ //foo the bar }Define a
valueChangeListeneron your selection component<p:selectOneMenu value="#{yourBean.value}" onchange="submit()" valueChangeListener="{#yourBean.processValueChange}">The key piece there is the
submit()bit that processes the enclosing form on change of the value. You can thengetNewValue()andgetOldValue()as necessary.
在 bean 中实现值更改事件
public void processValueChange(ValueChangeEvent e){ //foo the bar }valueChangeListener在您的选择组件上定义 a<p:selectOneMenu value="#{yourBean.value}" onchange="submit()" valueChangeListener="{#yourBean.processValueChange}">关键是
submit()在值改变时处理封装形式的位。然后getNewValue(),您可以getOldValue()根据需要。
EDIT: Now that I think about it, I see no reason why you cannot leave your setup as-is and simply define the valueChangeListener. It should still be processed during the changeevent in the <p:ajax/>, in fact, it will be processed before the listener for the ajax event itself.
编辑:现在我考虑了一下,我看不出为什么您不能按原样保留设置并简单地定义valueChangeListener. 应该还是在change事件期间处理的<p:ajax/>,实际上它会在ajax事件本身的监听器之前处理。
回答by Abed Kanbar
you can use this:
你可以使用这个:
public void onNameChanged(AjaxBehaviorEvent event)
{
String myVal = (String) ((UIOutput) event.getSource()).getValue();
System.out.println("myVal: " + myVal);
}

