0

when is a bean initialized? I used @RequestScoped managed bean annotation. I have a page - products.xhtml and I am redirecting on the same page depending on the version a clicks. enter image description here

For example, user clicks 2019.000, it redirects to the same page~ /products.xhtml?faces-redirect=true&ver=2019.000

My problem is I have a dialog that opens when a user clicks on a p:commandLink. The bean needed to populate the dialog's contents is DownloadView.java::getRoot(). Here's a snippet of my DownloadView.java.

@ManagedBean(name = "DownloadView")
@ViewScoped
public class DownloadView implements Serializable {
    private TreeNode root;
    private String path;

    public TreeNode getRoot() {     
        DownloadBI downloadBI = new DownloadBI(modifyPath(this.path));
        this.root = downloadBI.getFilesTreeNode();
        if (root.getChildCount() != 0)
            this.root.getChildren().get(0).setExpanded(true);
        return this.root;
    }
. . .

The default selection when page loads is 2020.000. The dialog works perfectly fine with the correct target links when loaded from the default selection 2020.000. But, when I redirect to 2019.000 and try to open a dialog, it seems that the bean retained the data from 2020.000. Here is a snippet of my p:commmandLink calling the dialog from products.xhtml.

<p:commandLink value="#{startup.name}" title="#{startup.name}"
    process="@form" update=":download-form:downloadPanel"
    oncomplete="PF('downloadDialog').show()" >
    <f:setPropertyActionListener target="#{DownloadView.path}" value="#{startup.url}"/>
</p:commandLink>

I was assuming that the value of startup.url should change every time the commandlink is clicked. It does but, its the same value from 2020.000. While testing its behavior, I noticed that the form id is the same from 2020.000 and 2019.000.

From chrome source:

enter image description here

j_idt161:0:j_idt163:1:download-view

is the same when selecting from 2019.000. They should be different since they have different urls.

I thought that this is scoping issue, but I tried all scopes. But, it is still not working. I am thinking that I should create my DownloadView.java bean only when the commandLink is clicked, and when the dialog is closed, it should also be destroyed. But, I am not sure how to do this as well. :(

I have been debugging this for days now and I'm stuck. I hope someone could give some ideas how to resolve this. I am new to JSF/Primefaces, by the way. Please help!

auvy
  • 116
  • 8
  • 1
    How do you pass the `&ver=2019.000` into the RequestScoped bean (which is not visible here)? – Selaron Feb 06 '20 at 08:25
  • In my products.xhtml, I have a function that get the value of ver. FacesContext fc=FacesContext.getCurrentInstance(); Map params=fc.getExternalContext().getRequestParameterMap(); String ver=params.get("ver"); – auvy Feb 06 '20 at 08:28
  • 1
    Please add that bean with all code relevant to reproduce the issue just like you added `DownloadView `. Try making it a [mcve] – Selaron Feb 06 '20 at 09:04
  • And please read https://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – Kukeltje Feb 06 '20 at 09:24
  • I couldn't really understand your problem, you mean that everytime you load the page some values are still the same as before but you need them to be refreshed? Thats strange that isn't happening by itself but my suggestion is to refresh or "clean" everything you need to in a Postconstruct method, clean session variables, etc. – BugsForBreakfast Feb 06 '20 at 14:52
  • Relevant: https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope – Jasper de Vries Feb 06 '20 at 15:35
  • Did you debug your setter of `path` on click of the link. What is the value? Did you tried using only a single bean with request or view scope? What about using `f:viewParam` to inject the `ver` into the bean instead of manually grabbing it. We are missing code here. – djmj Feb 26 '20 at 02:07
  • You need to check the getter logic, maybe its not changing value for some validation? perhaps that validation needs another statement so that it changes its value when needed, add more code auvy – BugsForBreakfast Mar 02 '20 at 16:54

0 Answers0