Question

Using a DataModel<MyObject>, I fill a data table according to this response

Now at the end of each line I have a delete button which calls a certain method of my bean. The data gets cleanly deleted. But as the data has been changed after the deletion, I'd like to reload the page in order to reflect the changes.

My attempt was to add a navigation rule in faces-config.xml:

overview /overview.jsp deletedsubscription /overview.jsp

If I have <redirect /> or not, either way it's not reloading or doing anything else at all. The data is deleted, therefore the bean's method is actually called.

My button looks like this:

<h:dataTable border="1" value="#{overviewBean.overviewModel }" var="item" first="0">
    <h:column id="column13">
        <f:facet name="header">
            <h:outputText value="#{messages.overviewDeleteItem }"></h:outputText>
        </f:facet>
        <h:commandButton action="#{overviewBean.deleteItem}" value="X"/>
    </h:column>
</h:dataTable>

Setting the attribute type="submit" actually solves the problem. The pages gets reloaded.

My question now: is the submit really required? Doesn't JSF (Apache MyFaces) have some mechanism of using AJAX to eliminate the line just deleted?

Thanks for trying to help a JSF-newbie.

Was it helpful?

Solution

Just remove the item from the backing list of the datamodel. The changes will be reflected in the model.

Basically:

private List<MyObject> overviewList;
private DataModel overviewModel;

public OverviewBean() {
    overviewList = overviewDAO.list();
    overviewModel = new ListDataModel(overviewList);
}

public void deleteItem() {
    MyObject myObject = (MyObject) overviewModel.getRowData();
    overviewDAO.delete(myObject);
    overviewList.remove(myObject); // See?
}

A redirect thereafter is not necessary. If the action method returns void (or null), it will go to the same page anyway.

Ajaxical capabilities have been introduced in JSF 2.0, but if I am not wrong, you're still on JSF 1.x. Your best bet is then adopting a 3rd party component library like Ajax4jsf (currently part of RichFaces), for the case that you consider this necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top