如果仅声明操作方法,@PostConstruct 不起作用
伙计们,我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道。该文章提到模型通常放置在会话范围内。这实际上是一个糟糕的方法。如果会话范围的 bean 是登录用户并且请求范围的 bean 绑定到表单,则在请求范围的 bean 中注入会话范围的 bean 是有意义的。
在您的情况下,您应该将模型 bean 设为控制器 bean 的属性,并使用
#{gestisciUtentiController.utenteBean.someProperty}
而不是#{utenteBean.someProperty}
。我之前有一些“JSF 设计”问题,您可能会发现它们也很有用:
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: