2

In JSF2, how can i pass objects from one requestscoped bean to another requestscoped bean? I really don't want to make them sessionscoped.

Maybe can i inject one bean in the other?

Thank you.

Michael Bavin
  • 3,944
  • 6
  • 31
  • 35
  • Are you attempting to store state across requests without a session scoped bean? If so, check out 'flash' scope to see if it helps. – Vineet Reynolds Jul 02 '10 at 14:14

1 Answers1

2

Use the ManagedProperty annotation:

@ManagedBean(name="beanA") @RequestScoped
public class BeanA implements Serializable {
  @ManagedProperty(value="#{beanB}") 
  private BeanB beanB;
  public void setBeanB(BeanB b) { this.beanB = b; }
  public BeanB getBeanB() { return beanB; }
}

@ManagedBean(name="beanB") @RequestScoped
public class BeanB implements Serializable {}

I haven't tested that code. You can achieve similar results by defining managed properties in faces-config.xml.

McDowell
  • 107,573
  • 31
  • 204
  • 267
  • I'm interested in your answer as well. Say, if I want to call beanB method in the `@PostConstruct init()` method of beanA, can I do something like this `beanB#someMethod`? Can this method return a value from a form's submit action? – ChuongPham Apr 13 '11 at 10:53
  • @user463053 - it depends on where the value is coming from - if you inject it via an expression `#{param.foo}` straight off the parameter map, you can use it in `@PostConstruct`; if the value is bound to a control, then it won't be available until after the Update Model Values phase. If you need more detail, please open a new question with short code snippets showing what you're trying to achieve. – McDowell Apr 13 '11 at 11:01
  • Thank you for your feedback. I have created a new post here [http://stackoverflow.com/questions/5648299/jsf-2-manageproperty-inject-one-request-scoped-bean-into-another]. Can you please provide some advice as I'm stuck on this issue for a day now. – ChuongPham Apr 13 '11 at 11:20