0

I want to put a h:commandLink in a h:dataTable to delete a single row. Here I have a code which does not work and I hope you can help me. This code doesn't even call my delete method. The data for the h:dataTable comes from a database.

XHTML

        <h:form>
            <div class="center">
                <h:dataTable id="list" value="#{firstBackingBean.list}"
                    var="first" styleClass="center">

                    <!-- other columns (take data from firstBackingBean) -->

                    <h:column>
                        <f:facet name="header" />
                        <h:commandLink value="delete" action="#{secondBackingBean.delete}" >
                            <f:ajax execute="@form" render="list" />
                            <f:param name="id" value="#{first.id}"/>
                        </h:commandLink>
                    </h:column>
                </h:dataTable>
            </div>
        </h:form>

FirstBackingBean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class FirstBackingBean {
    public void delete() {
        System.out.println("\nBUTTON CLICKED\n");
    }
}

SecondBackingBean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class SecondBackingBean {
    // ...
}
thomas.st
  • 393
  • 2
  • 5
  • 17

2 Answers2

3

When an UICommand component inside an UIData component doesn't invoke its action, then it often means that the model behind the value attribute of the UIData component has incompatibly changed between the request of displaying the form with the table and the subsequent request of submitting the form.

This can happen if the backing bean is request scoped and the model is preinitialized based on a request scoped variable (such as a request parameter), which isn't present during the form submit request anymore.

Placing the bean in the view scope should fix it.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @ViewScoped doesn't solve my problems. What should I try next? – thomas.st Sep 25 '12 at 19:01
  • @user1586660 Read the links inside and check if you have any of those errors. – Luiggi Mendoza Sep 25 '12 at 19:04
  • @LuiggiMendoza I get no errors and thats why it is so strange. My delete() method just never gets called. JBoss displays all new SELECTs from the database, but not the System.out.println() from my method. – thomas.st Sep 25 '12 at 19:17
  • @user1586660 clean all your assets, then build the project and try again. – Luiggi Mendoza Sep 25 '12 at 21:53
  • @LuiggiMendoza that doesn't solve the problem. Again the delete() method doesn't gets called. That's really strange. – thomas.st Sep 26 '12 at 05:36
  • @user1586660 have you tried using a different method than your `delete` one? Have you tried just displaying the parameter in your delete method? Have you isolated the case to check that there is a problem using this approach or there could be another problem in your code? Saying *it doesn't work* it's pretty vague, and to solve another potential problems in your code is out of the scope of the question/answer. Remember that the links provided by BalusC are very explanatory. – Luiggi Mendoza Sep 26 '12 at 06:17
  • @LuiggiMendoza Yes, I've tried using a differnet method. But I can't even println() something. Without the h:dataTable the method-call works, but when I use it in the h:dataTable it does nothing, just SELECTs. – thomas.st Sep 26 '12 at 06:33
  • @user1586660 there is something wrong in your ``. Edit your question and post the full code to check for any error. – Luiggi Mendoza Sep 26 '12 at 06:35
  • [Please show a real SSCCE instead of a carelessly oversimplified and untested snippet](http://stackoverflow.com/tags/jsf/info). Show the bean with 1 property and 1 action method and the view with a datatable with 1 column with therein the commandlink. All other columns and properties are irrelevant to this problem. By the way, `rowIndexVar` doesn't exist on `h:dataTable` at all. – BalusC Sep 26 '12 at 09:52
  • This code is not complete. I can't copy'n'paste'n'run it without making unobvious changes. The table remains empty and it complains that `list` property can't be found. Also the `delete()` method seems to be in the completely wrong backing bean class. Once again, please provide a real SSCCE. Click the link if you don't understand what I was asking. – BalusC Sep 26 '12 at 15:30
0

I've the answer!

I've 2 BackingBeans, the firstBackingBean needs the @SessionScoped annotation and the secondBackingBean needs the @ViewScoped annotation.

Thanks a lot for the other answers.

thomas.st
  • 393
  • 2
  • 5
  • 17