Search code examples
javajsfjsf-2managed-bean

Pass Data between ManagedBeans


I have one Class called Foo. And one ManagedBean called FooBean. In a page (Facelet) I get a new Foo info from user and insert it to DB. After first submission of data the print button will be enabled. Now I need the user click the print button and see the other page (Facelet) which is the print page with PrintFooBean. How can I pass the ID of the recently inserted Foo to the PrintFooBean and load it and display its items on the printing page?

What is the best way? And I prefer the user to see the print page in a new browser window/tab.


Solution

  • Pass it as request parameter.

    <a href="print.jsf?id=#{foo.id}" target="_blank">print</a>
    

    or unobtrusively progressively enhanced by JS window.open

    <a href="print.jsf?id=#{foo.id}" target="_blank"
        onclick="window.open('print.jsf?id=#{foo.id}'); return false;">print</a>
    

    Populate the print bean with the id.

    @ManagedProperty(value="#{param.id}")
    private Long id;
    

    Preload the Foo data in same bean based on id.

    @PostConstruct
    public void init() {
        this.foo = fooDAO.find(id);
    }