【问题标题】:Jsf2.0 preRenderViewJsf2.0 preRenderView
【发布时间】:2012-05-11 11:01:10
【问题描述】:

我正在使用 jsf 2.0

我对 PreRenderView 有疑问。

在我的 Bean 中,我有类似的方法

public void init() throws Exception
    {

        FacesContext.getCurrentInstance().getExternalContext().redirect("/To My Page");
        if(!FacesContext.getCurrentInstance().isPostback())
        {
            System.out.println("Kshitij");
        }
    }

当这个方法执行时,它也会在 servler 日志中打印“Kshitij”。

然后重定向到页面。

为什么?我认为它必须先重定向到页面。

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    为什么你认为实际的重定向是首先执行的?该方法必须首先完成运行,然后服务器才能继续控制请求/响应。在全新的请求和线程中,中途暂停代码执行然后在完全相同的位置继续执行代码是不可能的。

    redirect() 调用基本上设置了Location 响应标头。只有当方法返回时,服务器才会发送响应,然后浏览器才会在该位置发送新的请求。

    如果您想在需要重定向时跳过打印,请添加 return 语句或 if/else。

    if (youNeedToRedirect) {
        FacesContext.getCurrentInstance().getExternalContext().redirect("/To My Page");
    }
    else {
        if (!FacesContext.getCurrentInstance().isPostback()) {
            System.out.println("Kshitij");
        }
    }
    

    这一切都与 JSF 或 preRenderView 无关。这只是基本的 Java 和 HTTP。

    相关:

    【讨论】:

    • 谢谢。好答案。现在我明白我们为什么使用 return;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2011-08-12
    • 2014-08-20
    • 2011-08-25
    • 2012-06-04
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多