f:viewParam在ajax调用后丢失 [英] f:viewParam lost after ajax call
问题描述
我有这个f:viewParam来设置值并在视图的back bean中进行搜索:
I have this f:viewParam to set value and do search in back bean in view:
<f:metadata>
<f:viewParam name="id"
value="#{editorBean.id}"
required="true" />
<f:event type="preRenderComponent"
listener="#{editorBean.search}" />
...
后备豆:
private String id; // getters setters
public void search(ComponentSystemEvent event) {
if (id != null) {
//search data in DB to construct TreeNode finBy(id)...
...
在浏览器中,我无法展开第二层树,因为在支持Bean中,id为null.
In browser I can't expand the second level of tree, because in backing Bean the id is null..
调试:
如何在所有通话中设置f:viewParam?
How to f:viewParam be set in all calls?
推荐答案
这是由于默认情况下< h:form>
提交给没有查询字符串的URL引起的.
It's caused because the <h:form>
submits by default to an URL without the query string.
将bean放入视图范围,
Either put the bean in the view scope,
@ManagedBean
@ViewScoped
public class EditorBean {
并在回发期间跳过prerenderview
and skip the prerenderview during postback
public void search(ComponentSystemEvent event) {
if (FacesContext.getCurrentInstance().isPostback()) {
return;
}
// ...
}
只要您与同一个视图进行交互,就可以在一个视图范围内使用bean,因此不需要一次又一次地初始化属性.
A view scoped bean lives as long as you interact with the same view and thus the properties doesn't need to be initialized again and again.
或使用 OmniFaces
Or make use of OmniFaces <o:form>
which offers an includeViewParams
attribute to include view parameters in form action URL:
<o:form includeViewParams="true">
另请参见:
- 在回发之间保留原始GET请求参数
- 如何在ajax调用中保持param?
- < f:metadata>< f:viewParam>和< f:viewAction>用于吗?
这篇关于f:viewParam在ajax调用后丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!