如果仅声明操作方法,@PostConstruct 不起作用

发布于 2024-11-02 19:28:27 字数 1082 浏览 1 评论 0原文

伙计们,我正在使用 jsf 2.0 和 spring。 我已经用 @PostConstruc 在托管 bean 中注释了一个方法,但是如果在 bean 中没有连接到 jsf 页面的字段,则不会调用 @PostConstruct 方法,即使在 jsf 页面中有一个操作方法连接到豆。 先感谢您。

添加了解释代码:

this si my BackingManagedBean

@ManagedBean(name="utenteBean")
@ViewScoped
public class UtenteBean extends SupportBean implements Serializable

While this is my ControllerManagedBean

@ManagedBean(name="gestisciUtentiController")
@ViewScoped
public class GestisciUtentiController extends MessageSupportBean implements Serializable {

@ManagedProperty(value="#{utenteBean}")
private UtenteBean utenteBean;
public void setUtenteBean(UtenteBean utenteBean) {
    this.utenteBean = utenteBean;
}

    @PostConstruct
    public void loadBean()
    {
        try
        {
            utenteBean.setUtentis(getFacadeFactory().getUtenteFacade().readAllOrdered(Utente.class, "username"));
        }
        catch (ApplicationException e)
        {
            setExceptionMessage(e.getLocalizedMessage(), e.getLocalizedMessageDetail());
        }
    }

guys i'm using jsf 2.0 with spring.
I have annotated a method in a managed bean with @PostConstruc, but if in the bean there aren't field connected to the jsf page the @PostConstruct method isn't called even if in the jsf page there is an action method connected to the Bean.
Thank you in advance.

Added code for explaination:

this si my BackingManagedBean

@ManagedBean(name="utenteBean")
@ViewScoped
public class UtenteBean extends SupportBean implements Serializable

While this is my ControllerManagedBean

@ManagedBean(name="gestisciUtentiController")
@ViewScoped
public class GestisciUtentiController extends MessageSupportBean implements Serializable {

@ManagedProperty(value="#{utenteBean}")
private UtenteBean utenteBean;
public void setUtenteBean(UtenteBean utenteBean) {
    this.utenteBean = utenteBean;
}

    @PostConstruct
    public void loadBean()
    {
        try
        {
            utenteBean.setUtentis(getFacadeFactory().getUtenteFacade().readAllOrdered(Utente.class, "username"));
        }
        catch (ApplicationException e)
        {
            setExceptionMessage(e.getLocalizedMessage(), e.getLocalizedMessageDetail());
        }
    }

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

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

发布评论

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

评论(1

烈酒灼喉 2024-11-09 19:28:27

http://blog.icefaces.org/blojsom/blog/default/2009/04/23/Making-distinctions- Between- Different-kinds-of-JSF-management-beans/我正在尝试使用这个方法。您认为这种做法不正确吗? -

我不知道。该文章提到模型通常放置在会话范围内。这实际上是一个糟糕的方法。如果会话范围的 bean 是登录用户并且请求范围的 bean 绑定到表单,则在请求范围的 bean 中注入会话范围的 bean 是有意义的。

在您的情况下,您应该将模型 bean 设为控制器 bean 的属性,并使用 #{gestisciUtentiController.utenteBean.someProperty} 而不是 #{utenteBean.someProperty}

我之前有一些“JSF 设计”问题,您可能会发现它们也很有用:

http://blog.icefaces.org/blojsom/blog/default/2009/04/23/Making-distinctions-between-different-kinds-of-JSF-managed-beans/ i'm trying to use this approch. You think that that approch isn't correct? –

I'm not sure. That article mentions that the model is typically to be placed in the session scope. This is actually a poor approach. Injecting a session scoped bean in a request scoped bean makes sense if the session scoped one is for example the logged-in user and the request scoped one is bound to the form.

In your case you should just make the model bean a property of the controller bean and use #{gestisciUtentiController.utenteBean.someProperty} instead of #{utenteBean.someProperty}.

I've some "JSF design" questions before, you may find them useful as well:

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