f:viewParam在ajax调用后丢失 [英] f:viewParam lost after ajax call

查看:43
本文介绍了f:viewParam在ajax调用后丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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">

另请参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆