JSF 2 ViewScope 问题

发布于 2024-10-28 12:28:22 字数 824 浏览 1 评论 0原文

引用这篇好文章

新的视图范围应该解决 正是这些问题。 @ViewScoped 豆子会和你一样长寿 将表单提交到同一视图 一次又一次。换句话说,如 只要操作方法 返回 null 甚至 void,该 bean 将在下一个请求中出现。 一旦您导航到不同的视图, 那么这个 Bean 将被丢弃。

想到这些问题:

  1. 如果我当前的视图是 index.xhtml,并且如果我明确指定 return "index";,这基本上返回到相同的视图,bean将再次重新创建..为什么?
  2. viewscoped bean 能否在重定向后继续存在?
  3. 如果可以的话,我该如何指定它?我无法想象做类似 return "?faces-redirect=true",是的,我想跳过使用 faces-config.xml 中定义导航。

Quoting from this nice article,

The new view scope should solve
exactly those issues. A @ViewScoped
bean will live as long as you're
submitting the form to the same view
again and again. In other words, as
long as when the action method(s)
returns null or even void, the bean
will be there in the next request.
Once you navigate to a different view,
then the bean will be trashed.

And these questions come into mind:

  1. If my current view is index.xhtml, and if I specify explicitly return "index"; or <h:commandButton action="index.xhtml" ..>, which is basically returning to the same view, the bean will be recreated again .. why?
  2. Can a viewscoped bean survive a redirection?
  3. And if it can, how can I specify it? I can't imagine doing something like return "?faces-redirect=true" or <h:commandButton action="?faces-redirect=true" ..>, and yes, I would like to skip defining the navigation in faces-config.xml using the <redirect/>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何必那么矫情 2024-11-04 12:28:22

如果我当前的视图是index.xhtml,并且如果我明确指定返回“index”;或者,基本上返回到相同的视图,bean 将再次重新创建..为什么?

如果您显式指定结果(读取:视图),那么将创建一个新视图。您必须从操作方法返回 null 或 void(或者只省略命令组件的 action 属性)。

我必须承认,我理解您的困惑,并且根据上下文,“视图”一词可以有不同的解释。我想我迟早会修改链接文章中的措辞。

Viewscoped bean 能否在重定向中幸存?

不能。只有会话范围的 bean 可以以及 flash 范围(重定向完成后立即结束,这可能是您在询问此问题时实际上需要的功能要求)问题)。

If my current view is index.xhtml, and if i specify explicitly return "index"; or , which is basically returning to the same view, the bean will be recreated again .. why ?

If you explicitly specify an outcome (read: a view), then a new view will be created. You have to return null or void from the action method (or just leave out the action attribute of the command component).

I must admit that I understand your confusion and that the term "view" can be interpreted differently, depending on the context. I think I'll revise the wording in the linked article sooner or later.

Can a Viewscoped bean survive a redirection ?

No. Only session scoped beans can and objects in the flash scope also (which ends immediately once the redirect has been finished, that's maybe which you actually need for the functional requirement you had in mind when asking this question).

夜清冷一曲。 2024-11-04 12:28:22

如果提供了 javax.faces.ViewState,视图状态可以在重定向后继续存在。目前我发现的最简单的解决方案是实现导航处理程序。

在 faces-config.xml 中:

<faces-config>
    <application>
        ...
        <navigation-handler>com.intersult.jsf.util.RedirectNavigationHandler</navigation-handler>
    </application>
    ...
</faces-config>

Java 类:

public class RedirectNavigationHandler extends ConfigurableNavigationHandler {
    private NavigationHandler parent;

    public RedirectNavigationHandler(NavigationHandler parent) {
        this.parent = parent;
    }

    @Override
    public void handleNavigation(FacesContext context, String from, String outcome) {
        if (!context.getPartialViewContext().isPartialRequest()) {
            if (outcome == null)
                outcome = context.getViewRoot().getViewId();
            if (!outcome.endsWith("?faces-redirect=true"))
                outcome += "?faces-redirect=true";
            String viewState =
                context.getExternalContext().getRequestParameterMap().get(ResponseStateManager.VIEW_STATE_PARAM);
            outcome += "&javax.faces.ViewState=" + viewState;
        }
        parent.handleNavigation(context, from, outcome);
    }
}

这会产生一些影响:每个重定向不再是独立的请求。这取决于视图状态,不一定可以恢复。因此,如果过期,请制定后备策略。重定向查看过期异常。

The view state can survive redirection if the javax.faces.ViewState is supplied. The currently most simple solution I found, is implementing a navigation handler.

In faces-config.xml:

<faces-config>
    <application>
        ...
        <navigation-handler>com.intersult.jsf.util.RedirectNavigationHandler</navigation-handler>
    </application>
    ...
</faces-config>

The Java class:

public class RedirectNavigationHandler extends ConfigurableNavigationHandler {
    private NavigationHandler parent;

    public RedirectNavigationHandler(NavigationHandler parent) {
        this.parent = parent;
    }

    @Override
    public void handleNavigation(FacesContext context, String from, String outcome) {
        if (!context.getPartialViewContext().isPartialRequest()) {
            if (outcome == null)
                outcome = context.getViewRoot().getViewId();
            if (!outcome.endsWith("?faces-redirect=true"))
                outcome += "?faces-redirect=true";
            String viewState =
                context.getExternalContext().getRequestParameterMap().get(ResponseStateManager.VIEW_STATE_PARAM);
            outcome += "&javax.faces.ViewState=" + viewState;
        }
        parent.handleNavigation(context, from, outcome);
    }
}

This has some impact: Each redirect is not an independent request anymore. It depends on the view state, which can not neccesarily be restored. So have a fallback strategy, if it is expired eg. redirect on view expired exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文