本文介绍了JSF 2:ajax 调用后的页面重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了类似于 this 的导航案例问题.简而言之,我正在尝试使用 ajax 呈现的 h:commandLink 将导航从一个页面重定向到另一个页面.这是支持 bean

I'm stuck in a navigation case problem similar to this one.In a few words, I'm trying to redirect navigation from one page to another, using an ajax rendered h:commandLink.Here's the backing bean

@ManagedBean
public class StartBean {

    public void search(){
        FacesContext
            .getCurrentInstance()
            .getExternalContext()
            .getFlash()
            .put("result", "hooray!")
        ;
    }

    public String showResult(){
        return "result?faces-redirect=true";
    }
}

和起始页

<h:body>
    <h:form prependId="false">
        <h:commandButton value="Click" action="#{startBean.search}">
            <f:ajax execute="@this" render="@form"/>
        </h:commandButton>

        <br/>

        <h:commandLink
            action="#{startBean.showResult()}"
            rendered="#{flash.result != null}"
            value="#{flash.result}"
        />

    </h:form>
</h:body>

result 页面只是显示一条消息.两个页面都在 Web 模块上下文根目录中.碰巧 h:commandLink 在ajax提交后正确显示,但单击它会导致页面刷新.它不会像预期的那样重定向到 result 页面.之后,如果页面被重新加载(F5),result 页面就会显示出来.这似乎是一个渲染周期的问题.

whereas result page is just showing a message. Both pages are on web module context root.It happens that the h:commandLink is correctly displayed after ajax submit, but clicking on it causes a page refresh. It doesn't redirect towards the result page, as expected.After it, if page is reloaded (F5), result page is shown. It seems to be a rendering cycle matter.

有什么建议吗?

提前致谢.

推荐答案

所有输入和命令组件的 rendered 属性在提交表单时重新评估.因此,如果它评估 false,则 JSF 根本不会调用该操作.search() 方法的请求/响应完成后,Flash 作用域终止.当您发送 showResult() 的请求时,它不再存在于 Flash 范围内.我建议将 bean 放在视图范围内,并将 rendered 属性绑定到它的属性.

The rendered attribute of all input and command components is re-evaluated when the form is submitted. So if it evaluates false, then JSF simply won't invoke the action. The Flash scope is terminated when the request/response of the search() method is finished. It isn't there in the Flash scope anymore when you send the request of the showResult(). I suggest to put the bean in the view scope and bind the rendered attribute to its property instead.

@ManagedBean
@ViewScoped
public class StartBean {

    private String result;

    public void search(){
        result = "hooray";
    }

    public String showResult(){
        return "result?faces-redirect=true";
    }

    public String getResult() {
        return result;
    }

}

<h:commandLink
    action="#{startBean.showResult}"
    rendered="#{startBean.result != null}"
    value="#{startBean.result}"
/>

另见:

08-21 14:11