JSF:在 backingBean 仍在工作时显示页面

发布于 2024-10-11 01:36:03 字数 505 浏览 3 评论 0原文

也许这个标题不太准确,但问题就在这里。 我正在做一些 JSF 项目,我需要以下内容:

当用户登录我的应用程序时(即,当他导航到主页时),需要一种从互联网和/或数据库获取一些货币汇率的方法叫。该方法的执行(页面解析、数据保存等)大约持续 5-6 秒。我不希望从登录到主页的导航持续 5-6 秒,我希望发生以下其中之一(尚未决定哪一个):

1)当用户登录时,他会看到主页(立即,而不是5-6 秒后)并且能够在方法在后台工作时单击和/或导航。

2)当用户登录时,他(立即)看到主页,一些加载登录到页面的一部分,并且在执行方法后,他看到带有汇率的数据表。

问题是,如何做到这一点? 如果我将该方法放在 HomePageBean 构造函数中,则在执行方法之前不会显示页面。也许我可以在主页上放置一些不可见的元素,并在某些 Getter 中调用该方法,但我不知道这是否是正确的方法(看起来像 hack)或者它实际上是否有效。

我正在使用 JSF 2.0 和 Primefaces。 提前致谢

maybe this title is not really precise, but here's the problem.
I'm doing some JSF project and I need a following:

When user logs on my application (ie. when he navigates to Home page), a method, which gets some money exchange rates from the internet and/or database, needs to be called. The execution of that method (page parsing, data saving etc.) lasts about 5-6 seconds. I don't want that navigation from Login to Home page lasts 5-6 seconds, i want one of these (didn't decide which one yet) to happen:

1) When user logs in, he sees Home page (immediately, not after 5-6 seconds) and is able to click and/or navigate while method is working in background.

2) When user logs in, he sees Home page (immediately), some loading sign in one part of the page and after method is executed, he sees dataTable with exchange rates.

Question is, how to do any of this?
If I put that method in HomePageBean constructor, page wont be displayed until method is executed. Maybe i can put some invisible element on Home page and call that method within some Getter, but i don't know if that's the right way to do (seems like hack) or will it actually work.

I'm using JSF 2.0 and Primefaces.
Thanks in advance

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

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

发布评论

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

评论(1

自此以后,行同陌路 2024-10-18 01:36:03

使用 ApplicationScoped bean,如下例所示:

@ManagedBean(name = "exchRateBean", eager = true)
@ApplicationScoped
public class ExchRateBean  {

  public List<Rate> getRates(Date date) {
    // cached data or DAO methods here
  }

  @PostConstruct
  public void init() {
    // Initial web-app initialization, will run once upon deploy
  }
}

这是访问应用程序范围 bean 的支持 bean。

@RequestScoped
public class MyBean  {
  @ManagedProperty(value="#{exchRateBean}")
  private ExchRateBean exchRateBean;

  // your JSF methods
  public List<Rate> getRates(Date date) {
    return getExchRateBean().getRates(date);
  }

  // G&S
  public ExchRateBean getExchRateBean() {
    return exchRateBean;
  }

  public void setExchRateBean(ExchRateBean exchRateBean) {
    this.exchRateBean = exchRateBean;
  }
}

Use ApplicationScoped bean like in example below:

@ManagedBean(name = "exchRateBean", eager = true)
@ApplicationScoped
public class ExchRateBean  {

  public List<Rate> getRates(Date date) {
    // cached data or DAO methods here
  }

  @PostConstruct
  public void init() {
    // Initial web-app initialization, will run once upon deploy
  }
}

And here is your backing bean accessing application-scoped bean.

@RequestScoped
public class MyBean  {
  @ManagedProperty(value="#{exchRateBean}")
  private ExchRateBean exchRateBean;

  // your JSF methods
  public List<Rate> getRates(Date date) {
    return getExchRateBean().getRates(date);
  }

  // G&S
  public ExchRateBean getExchRateBean() {
    return exchRateBean;
  }

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