0

I'm new to web applications with Primefaces and JSF and I'm stuck at some pretty common problem (or so I guess). I have a list of items that get fetched from my database. Selecting one of them will trigger a bean method to save the item into flash memory and get to a detail page:

    public String showDetails(Item item) {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("detailItem", item);
        return "/items/item-details";
    }

I then use another bean ItemDetailBean to extract infos from the chosen item and display it:

@SuppressWarnings("serial")
@Named
@ViewScoped
public class ItemDetailBean implements Serializable {

    private Item detailItem;
    private String itemDetails;


    @PostConstruct
    public void init() {

        detailItem = (Item) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("detailItem");

        if (detailItem != null) {
            itemDetails = detailItem.getSomeStringDetails();
        }
    }
    
    public String changeItemButton() {
        return "/items/edit-item";
    }
    
    public String getItemDetails() {
        return itemDetails;
    }
}

My detail page can print the details with itemDetailBean.itemDetails.

There are also a few buttons on the detail page to do stuff with that particular item, e.g. changeItemButton() (see above). On the new page, I have still access to that particular item through my bean. However I encounter a problem when I click "back" in my browser - I will get back to the detail page (with all item infos still rendered), but clicking the button again will this time lead to a page where I don't have access to the item anymore (since we left the bean scope, probably).

This problem can be mitigated by refreshing the page after clicking "back" and then using the "Confirm Form Resubmission" popup to reinitiate my bean with the same item. However there's no such popup when clicking "back".

What's the best solution for this problem? I checked out different scopes, but haven't really found a good one - conversation scope seemed promising, but I don't want to manually end the conversation at some point ... I'd prefer the item to stay in scope until the user would navigate back to the overview item list to pick a different one.

Thanks in advance and sorry in case this has an obvious solution.

Noel93
  • 135
  • 11
  • Have a look at https://stackoverflow.com/questions/10941664/dealing-with-back-button-and-resulting-phase-id – Jasper de Vries May 16 '21 at 07:43
  • Pass `#{item.id}` as request parameter in URL instead of via flash scope. See abovelinked duplicate for kickoff example. – BalusC May 17 '21 at 09:23

0 Answers0