JSF 2:ajax 调用后的页面重定向

JSF 2: page redirect after ajax call(JSF 2:ajax 调用后的页面重定向)
本文介绍了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}"
/>

另见:

  • commandButton/commandLink/ajax action/listener 方法未调用或输入值未更新
  • 这篇关于JSF 2:ajax 调用后的页面重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Laravel 5.3 with Vuejs ajax call尝试使用 Vuejs 从数据库中获取一些数据。我的用户表中有一些虚拟数据。我想在我的视野中展示它们。问题是虽然页面加载,但...
Toggle v-navigation-drawer's expand-on-hover not resizing content我想为我的 v-navigation-drawer 实现一个pin功能,即在按下按钮时在迷你版本和普通版本...
Passing Data between react components(在Reaction组件之间传递数据)
How can I get the nearest date through moment.js?(怎样才能通过Moment.js获得最近的日期呢?)
Difference between two time using dayjs(使用DAYJS的两次时间之间的差异)
Tempus Dominus Bootstrap4 requires moment.js. (datetime picker)(Tempus Domus Bootstrap4需要minom.js。(日期时间选取器))