一系列页面要使用哪个范围,每个范围取决于上一页? [英] Which scope to use for a series of pages, each dependent on the previous page?

查看:109
本文介绍了一系列页面要使用哪个范围,每个范围取决于上一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有四个页面,每个页面都有自己的RequestScoped托管bean支持.下面是一个示例...

Currently I have four pages that are each backed by their own RequestScoped managed bean. An example of this would be the following...

支持search.xhtml

backing search.xhtml

@RequestScoped
SearchBean

支持result.xhtml

backing result.xhtml

@RequestScoped
ResultBean

支持detail.xhtml

backing detail.xhtml

@RequestScoped
DetailBean

backing action.xhtml

backing action.xhtml

@RequestScoped
ActionBean

在每个页面(搜索页面除外)中,我从前一页注入Bean来访问输入参数.例如...

In each page (except for the search page), I inject the bean from the previous page to access the input parameters. For instance...

@RequestScoped
public class Result {

    @ManagedProperty("#{search}")
    private Search search;

    private ResultData resultData;

    private Service service;

    public Result() {

    }

    @PostConstruct
    public void init() {
        resultData = service.getResultData(search);
    }

    // getters & setters

}

@RequestScoped
public class Detail {

    @ManagedProperty("#{result}")
    private Result result;

    private DetailData detailData;

    private Service service;

    public Detail() {

    }

    @PostConstruct
    public void init() {
        detailData = service.getDetailData(result);
    }

    // getters & setters

}

@RequestScoped
public class Action {

    @ManagedProperty("#{detail}")
    private Detail detail;

    private ActionData actionData;

    private Service service;

    public Action() {

    }

    @PostConstruct
    public void init() {
        actionData = service.getActionData(detail);
    }

    // getters & setters

}

我想使用重定向,以便能够使用后退按钮,但是这样做时,我丢失了请求中托管bean的输入数据.我可以对每个bean使用SessionScope,但是我觉得我会滥用它.另外,我担心到达操作页面时的开销.此时,由于每个bean都注入了前一个bean,因此在触发页面时将创建所有四个ManagedBean.有没有比使用RequestScoped bean更好的方法呢?我想我可以为一系列页面使用一个SessionScoped bean,并使用它在整个请求中传递输入参数.这也将允许我使用重定向功能,并且在最终点击操作页面时不会加载所有四个托管Bean.任何建议,将不胜感激.谢谢.

I want to use redirect so that I am able to use the back button, but while doing so I lose the input data from the managed bean in the request. I could use SessionScope for each bean, but I feel like I would be abusing it. Also, I am worried about the overhead by the time I get to the action page. At this point, since each bean injects the previous bean, all four ManagedBeans are created when the page is triggered. Is there a better way of doing this than using RequestScoped beans. I suppose I could have one SessionScoped bean for the series of pages and use it to carry input parameters across requests. This would also allow me to use the redirect feature and wouldn't load all four managed beans when the action page is finally hit. Any suggestions would be appreciated. Thanks.

推荐答案

根据您存储的数据的使用期限,页面是否实际上按照您列出的顺序出现以及您打算输入多少字,可以使用

Depending on the tenure of the data you're storing, whether the pages actually occur in the sequence you listed and how much typing you're inclined to, you could use either

  1. Flash范围 :对于在bean和页面之间严格传输 temporary 信息的情况,此范围是理想的选择.乍一看,此范围似乎非常适合您的用例.我必须强调存储在此范围内的数据的临时性质.指出此范围的实施的早期版本(有问题,因此请使用后果自负.我个人对此范围没有任何问题,因此它可以归结为您正在运行的JSF版本

  1. Flash scope: This is scope is ideal for transfer of strictly temporary information between beans and pages. At first blush, it appears this scope would be ideal for your use case. I must emphasise the temporary nature of the data stored in this scope. It's also probably a good thing to point out that early versions of the implementations of this scope (especially with mojarra) had problems, so use at your own peril. I personally have not had any problems with the scope, so it really boils down to the version of JSF you're running

使用Flash作用域:

Using the flash scope:

如何是否在页面重新加载时保留JSF Flash作用域参数?

@SessionScoped:这个作用域,因为您已经知道持续很长时间.如果您正在使用的数据可能会比您在此处列出的bean/页面的寿命更长,这就是方法.它也可以在刷新和重定向后继续存在而不会出现任何问题(并且还有更多的好处,即可以减少持久化和重复使用数据的工作量)

@SessionScoped: This scope, as you're already aware lasts really long. If the data you're using would likely outlast the beans/pages you've listed here, this is the way to go. It'll also survive refreshes and redirects without any problems (and there's the added benefit of doing less work to persist and reuse the data)

这篇关于一系列页面要使用哪个范围,每个范围取决于上一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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