0

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());
        }
    }
Roberto de Santis
  • 868
  • 5
  • 15
  • 26
  • How exactly is `@PostConstruct` useful if you don't have anything which renders to the view? You have basically nothing to prepare then. What kind of code do you have there? What's the functional requirement? – BalusC Apr 22 '11 at 21:49
  • I'm trying to have different type of managed bean, for example i have ModelManagedBean that have all the properties of the page and ControllerManagedBean that have action Method. Now i think that in the ControllerManagedBean I must call a postConstruct method that initialize, for example, ModelManagedBean's List object – Roberto de Santis Apr 22 '11 at 21:53
  • Make the model a property of the controller. It shouldn't be a `@ManagedBean` then. – BalusC Apr 22 '11 at 21:59
  • 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? – Roberto de Santis Apr 22 '11 at 22:03

1 Answers1

0

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555