【问题标题】:How to evaluate which page to show in PostConstruct?如何评估在 PostConstruct 中显示哪个页面?
【发布时间】:2015-01-22 07:48:58
【问题描述】:

想象一下这种情况:

我有一个 SessionScoped bean(名为 TheSessionBean),它是另一个 bean(名为 AnotherBean)的 ManagedProperty。

@ManagedBean
@ViewScoped
public class AnotherBean implements Serializable {

@PostConstruct
public void init() {
    //Evaluate here!
}

@ManagedProperty(value = "#{theSessionBean}")
private TheSessionBean theSessionBean;
    //Getter and Setter...
}

我需要评估 ManagedProperty (theSessionBean) 的值以了解是否可以在没有用户交互的情况下在页面显示之前显示页面

据我了解,这必须在 PostConstruct 方法中进行评估(这样我才能获得 ManagedProperty 的 Session 的值)。

TheSessionBean 只有一个名为权限的字符串属性。

所以首先我需要知道:

  • theSessionBean.getPermission() == null 以重定向到名为 one 的页面
  • theSessionBean.getPermission().equals("two") 以重定向到名为 two 的页面
  • More evaluations...

问题是PostConstruct方法必须是void,我需要重定向到对应的页面。

我该怎么做?

【问题讨论】:

  • 无论您使用哪种技术(是 Spring 还是完整的 JEE),使用内置安全性不是更好吗?否则,设置一个可用于选择如何显示页面的布尔属性就足够了吗?使用FacesContext@PostConstruct 中进行程序化重定向对我来说没有任何意义,如果可能的话。

标签: jsf jsf-2


【解决方案1】:

您可以在您的PostConstruct 方法中使用ExternalContext.redirect

if (someCondition)) {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath() + "/one");
}

【讨论】:

    【解决方案2】:

    最佳实践是使用 JSF 系统事件 - PreRenderViewEvent PostConstructApplicationEvent PreDestroyApplicationEvent JSF events

    【讨论】:

      猜你喜欢
      • 2021-02-05
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 2018-04-22
      • 1970-01-01
      • 2012-08-03
      相关资源
      最近更新 更多