0

I am new with JSF/PrimeFacesand now, I'm trying to solve this problem for days. I have a page with a commandLink which opens a dialog with a TreeTable that displays values from the backing bean. The value of the bean is updated depending on the product.url. My problem is, I get a successful result when I click on the commandLink from the main page or landing page(/products.xhtml?...ver=2019.000), but when I'm on the other redirected links(/products.xhtml?...ver=2018.000), it just won't work. The passed #{DownloadView.path} is null.

Here's a snippet of my .xhtml with the commandLink:

<div id="features">
  <h:form id="form">
    <div class="feature-icon fadeInDown animated" style="margin:0px 35px">
      <p:commandLink action="/products.xhtml?faces-redirect=true&amp;ver=2019.000" value="2019.000" style="#{fn:startsWith(ProductsView.actVer, '2019.000') ? 'background-color: #e91e63;' : ''}" />
    </div>
    <div class="feature-icon fadeInDown animated" style="margin:0px 35px">
      <p:commandLink action="/products.xhtml?faces-redirect=true&amp;ver=2018.000" value="2018.000" style="#{fn:startsWith(ProductsView.actVer, '2018.000') ? 'background-color: #e91e63;' : ''}" />
    </div>
  </h:form>
</div>
        
<div id="promotion" class="clearfix">
  . . .
    <ui:fragment rendered="#{!empty product.url}">
      <ui:fragment rendered="#{fn:startsWith(product.url, 'file')}">
        <h:outputText value="#{product.url}"/>
          <p:commandLink action="#{DownloadView.listFiles}" update=":download-form:downloadPanel" oncomplete="PF('downloadDialog').show()" title="Details" >
            <h:outputText value="#{product.name}" />
          <f:setPropertyActionListener target="#{DownloadView.path}" value="#{product.url}" />
          </p:commandLink>
      </ui:fragment>
    </ui:fragment>
  . . .
</div>

Here's my dialog:

<h:form id="download-form" >  
    <p:dialog id="dialog" header="Details" showEffect="fade" widgetVar="downloadDialog" modal="true" resizable="false" dynamic="true">
        <p:outputPanel id="downloadPanel">
         <p:treeTable value="#{DownloadView.root}" var="download" style="margin-top:0" scrollable="true" scrollHeight="300">
          <f:facet name="header">
              Document Viewer
          </f:facet>
          <p:column headerText="Name">
           <a href="#{download.path}" > <h:outputText value="#{download.name}" /> </a>
          </p:column>
          <p:column headerText="Location">
              <h:outputText value="#{download.path}" />
          </p:column>          
      </p:treeTable>
        </p:outputPanel>
        <p:ajax event="open" listener="#{DownloadView.reset()}"/>
    </p:dialog>
</h:form>

Here's my DownloadView class:

    @SessionScoped
    public class DownloadView {

    private TreeNode root;

    private String path;

    private DownloadBI downloadBI;

    @PostConstruct
    public void init() {
        downloadBI = new DownloadBI();
    }

    public void listFiles() {
        downloadBI.setPath(path);
        this.root = downloadBI.getFilesTreeNode();
    }

    public TreeNode getRoot() {
        return root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void reset() {
        this.path = "";
        this.root = null;
    }
}

When I click on the commandLink from the landing page, the TreeTable is populated and checking the logs, the path is set: enter image description here

But, if I click on the commandLink, other than the landing page, the TreeTable is empty and path is not set.

Any idea to resolve this problem is greatly appreciated! Thank you!

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
auvy
  • 116
  • 8
  • You need a scope on your DownoadView class? – Melloware Aug 27 '19 at 11:08
  • Yes, I already added @SessionScoped annotation on my DownloadView class. – auvy Aug 27 '19 at 23:50
  • auvy try @ViewScoped and also take a look at https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope it might help – BugsForBreakfast Aug 28 '19 at 13:54
  • @BugsForBreakfast you're right, I must use "@ViewScoped". Though it does not fully resolve the issue, it actually is the right scope to use. Thanks! – auvy Aug 29 '19 at 04:45
  • @auvy you are welcome :), the issue is still exactly the same? or something different happening? – BugsForBreakfast Aug 29 '19 at 13:19
  • @auvy Because if it still being null, sometimes it is neccesary to add some logic on the getters that is returning you that null (which is path right?), like "if path is null, then calculate again that path" and outside that if return the path, which is no longer going to be null :) – BugsForBreakfast Aug 29 '19 at 13:25
  • @auvy https://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times see this, explanation why is above an option, sometimes is neccesary – BugsForBreakfast Aug 29 '19 at 16:05
  • @BugsForBreakfast Thank you! You've been very helpful! That's exactly what's been happening. I have resolved the problem now adding some logic on the getter. – auvy Aug 30 '19 at 04:17
  • @auvy glad to know, you are welcome =) – BugsForBreakfast Aug 30 '19 at 13:08

0 Answers0