1

I'm retrieving the list of files in a folder when a button is clicked. The function that retrieves the folder works, and if I load it before the table is rendered the values are shown as they should. However if I try to populate the table with a commandbutton calling a function initializing the list of values, no changes are made to the datatable. Here's my code:

<h:form id="resultform"> 
    <p:dataTable id="resulttable" value="#{ViewBean.resultList}" var="result">
        <p:column>
            <f:facet name="header">
                <h:outputText value="GFEXP_NOM_BDOC" />
            </f:facet>
            <h:outputText value="#{result.name}" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="GFEXP_DATE_MODIF" />
            </f:facet>
            <h:outputText value="#{result.date}" />
        </p:column>
        <p:column>
            <f:facet name="header">
                <h:outputText value="GFEXP_PATH" />
            </f:facet>
            <h:outputText value="#{result.path}" />
        </p:column>
        <f:facet name="footer">
            <p:commandButton value="REFRESH exploitation" action="#{ViewBean.refresh}" update=":resultform:resulttable" ajax="true" />
        </f:facet>
    </p:dataTable>
</h:form>

Here's the backing bean code:

@ManagedBean(name = "ViewBean")
@ViewScoped
public class ViewBean implements Serializable {

    private static final long serialVersionUID = 9079938138666904422L;

    @ManagedProperty(value = "#{LoginBean.database}")
    private String database;

    @ManagedProperty(value = "#{ConfigBean.envMap}")
    private Map<String, String> envMap;

    private List<Result> resultList;

    public void initialize() {
        setResultList(Database.executeSelect(database));

    }

    public void refresh() {
        List<File> fileList = readDirectory();
        List<Result> tmpList = new ArrayList<Result>();
        for (File f : fileList) {
            Result result = new Result(f.getName().substring(0,
                    f.getName().lastIndexOf(".")), String.valueOf(f
                    .lastModified()), f.getPath().substring(0,
                    f.getPath().lastIndexOf("\\") + 1));
            tmpList.add(result);
        }
        setResultList(tmpList);
    }
//GETTERS AND SETTERS
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
noisegrrrl
  • 139
  • 12
  • What PrimeFaces version? – BalusC Aug 21 '13 at 12:58
  • See if yout method called with PF button – Programmer Aug 21 '13 at 13:00
  • Primefaces 3.5. And the method is being called, I added a check to see the size of the list and it prints to the console like it should (and it's not empty). – noisegrrrl Aug 21 '13 at 13:01
  • Are you absolutely positive that it's 3.5? Before 3.4, it was due to a bug not possible to ajax-update specifically the `` from inside the `` itself. Have you tried `update="@form"`? If that works, then you're certainly not running 3.5. – BalusC Aug 21 '13 at 13:06
  • I just tried it, it's not working either. And I'm positive it's 3.5. – noisegrrrl Aug 21 '13 at 13:11
  • It doesn't seem to be an issue with it being inside the datatable since it's not working if I create a second form. – noisegrrrl Aug 21 '13 at 13:39

1 Answers1

2

It look like that you're abusing preRenderView to initialize the state of a view scoped bean. However, as its name says, it runs right before the rendering of the view. You should instead be using the @PostConstruct annotation for that. When put on a method, then that method will be invoked after bean's construction and all managed property and dependency injection. It will not be invoked on all subsequent postback requests on the same view.

@PostConstruct
public void initialize() {
    resultList = Database.executeSelect(database);
}

Don't forget to remove the <f:event> altogether.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452