2

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.

Community
  • 1
  • 1
Atmocreations
  • 9,923
  • 15
  • 67
  • 102

1 Answers1

4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yet another good response. Thanks! Well... when I had the method returning `void`, eclipse complained about it in the JSP editor... Will try again. – Atmocreations Jan 18 '11 at 11:02
  • Eclipse is a jerk. Ignore and run it. Those errors/warnings are configureable. You can eventually also just return `null`. – BalusC Jan 18 '11 at 11:09
  • Eclipse is cool =) It helped me with many points in the hard learning process of JSF. But yes, you're right, sometimes it complains about more than it should. Maybe this is depending on the implementation (Myfaces/Mojarra or how it's called). thanks! – Atmocreations Jan 18 '11 at 18:31
  • 1
    No, it's Eclipse itself. For some more configuration hints, check the latest few points and screens in [this chapter](http://balusc.blogspot.com/2008/01/jsf-tutorial-with-eclipse-and-tomcat.html#RunAndConfigureEclipse). I didn't mean to imply that Eclipse is bad, even more, I use it myself. But especially with EL (expression language, those `#{}` things) validation it can go havoc. I just turn off or lower those validations. – BalusC Jan 18 '11 at 18:33
  • Ah okay - good point! Well I guess I won't do any JSF anymore as my project's done. :P – Atmocreations Jan 18 '11 at 18:39
  • How would you do with Ajax4jsf? – Kayser Sep 13 '12 at 07:39