selectOneMenu上的valueChangeListener和ajax执行顺序问题
我目前遇到一个奇怪的现象,即在执行 valueChangeListener 之前为模型设置值。
基本上我希望发生的是:
- 我更改选择菜单
- 使用Ajax提交新值来更新myBean(使用f:ajax)
- 根据新提交的值执行查询(使用valueChangeListener)
- 根据最后一个查询的结果
但是当我运行它时会发生什么按这个顺序,1 - 3 - 2 - 4(不是我想象的1 - 2 - 3 - 4)
是UI的部分:
<h:selectOneMenu label="budget" id="budget"
converter="genericConverter"
value="#{myBean.budget}"
valueChangeListener="#{myBean.actionSearch}">
<f:ajax render="myGrid" />
<f:selectItem itemLabel="Choose one .." noSelectionOption="true" />
<f:selectItems ... />
</h:selectOneMenu>
这 这是 bean 的摘录:
public void actionSearch() {
System.out.println("searching with this.budget == " + this.budget);
...
}
public void setBudget(String budget) {
System.out.println("setting budget : " + budget);
this.budget = budget;
}
这是输出:
searching with this.budget == xxxx
setting budget : yyyy
我想要的是,首先根据 UI 中选择的新值设置预算,然后开始根据新 UI 进行搜索。但我不知道如何实现这一目标。
我正在使用 Tomcat 7 以及这些:
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
我想知道我做错了什么?
谢谢 !
I'm currently experiencing a strange phenomena, where setting the value to the model happens before executing valueChangeListener.
Basically what i would like to happen is :
- I change the select menu
- The new value get submitted using Ajax to update the myBean (using f:ajax)
- Execute a query based on the newly submitted value (using valueChangeListener)
- Render the table based on the result from the last query
But what happens when i run it is in this order, 1 - 3 - 2 - 4 (not 1 - 2 - 3 - 4 as i imagined)
Here's the part of the UI :
<h:selectOneMenu label="budget" id="budget"
converter="genericConverter"
value="#{myBean.budget}"
valueChangeListener="#{myBean.actionSearch}">
<f:ajax render="myGrid" />
<f:selectItem itemLabel="Choose one .." noSelectionOption="true" />
<f:selectItems ... />
</h:selectOneMenu>
And this is the excerpt from the bean :
public void actionSearch() {
System.out.println("searching with this.budget == " + this.budget);
...
}
public void setBudget(String budget) {
System.out.println("setting budget : " + budget);
this.budget = budget;
}
And this is the output :
searching with this.budget == xxxx
setting budget : yyyy
What i would like is, setting the budget first from the new value selected in the UI, and then start searching based on the new UI. But i dont know how to achieve that.
And im using Tomcat 7 along with these :
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.4-b09</version>
<scope>compile</scope>
</dependency>
I wonder what i did wrong ?
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
valueChangeListener
在验证阶段、更新模型值阶段之前执行。它的目的是能够获取旧值和新值的句柄,以便您可以在必要时根据真实更改执行一些业务操作(日志记录?) 。然而,在 JSF 1.x 中,这通常(ab)用于仅对下拉列表更改调用操作,但它必须与onchange="submit()"
、结合使用>立即=“真”,
FacesContext#renderResponse()
和其他东西。所选值将通过ValueChangeEvent 获取#getNewValue()
(ValueChangeEvent
应该定义为方法的参数)。对于 JSF 2.x,在这种情况下您不再需要
valueChangeListener
。无论如何,您对旧值也不感兴趣。使用的
相反。listener
属性>这将在调用操作阶段、更新模型值阶段之后执行。
The
valueChangeListener
is executed during Validations phase, before the Update Model Values phase. It is intented to be able to get a handle of both the old and new value so that you can if necessary do some business stuff (logging?) based on the real change. In JSF 1.x this was however more than often (ab)used to invoke actions on a dropdown change only, but it has to be used in combination withonchange="submit()"
,immediate="true"
,FacesContext#renderResponse()
and other stuffs. The selected value is to be obtained byValueChangeEvent#getNewValue()
(theValueChangeEvent
is supposed to be definied as method's argument).For JSF 2.x you don't need the
valueChangeListener
anymore in such case. You're also not interested in the old value anyway. Use thelistener
attribute of<f:ajax>
instead.This will be executed during Invoke Action phase, after Update Model Values phase.