2

I have the problem that my Faces ActionEvent returns a source, but the check on != null returns false. The crazy fact is, that in debug mode I get a filled ActionEvent variable.

I have broken down the code to the important sections

Bean:

public class HibernateUserHandling 
{
    public void deleteUser(ActionEvent ev)
    {
        if (ev.getSource() != null && ev.getSource() instanceof HtmlDataTable) {
            HtmlDataTable objHtmlDataTable = (HtmlDataTable) ev.getSource();
            setRowOfUserToDelete(objHtmlDataTable.getRowIndex());
            setPersonsCopy(HibernateDataOutput.persons);
            setUserToDelete(getPersonsCopy()[getRowOfUserToDelete()]);
            setUserIdToDelete((Integer) getUserToDelete().getUserId());
        }
}

View:

<html>
    <body>
        <ui:composition template="./generalTemplate.xhtml"
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets">
        <ui:define name="content">
            <h:outputStylesheet library="css" name="cssHibernate.css" />
                <h:form id="main">
                    <h:dataTable value="#{hibernateDataOutput.persons}" var="list"
                                styleClass="order-table"
                                headerClass="order-table-header"
                                rowClasses="order-table-odd-row,order-table-even-row">
                        <h:column>
                            <f:facet name="header">Delete</f:facet>
                            <h:commandButton id="delete" actionListener="#{hibernateUserHandling['deleteUser']}" image="delete.jpg"/>
                        </h:column>
                    </h:dataTable>
                </h:form>
            </ui:define>
        </ui:composition>
    </body>
</html>

I don't get an error, it just skips the If block because of the "ev.getSource() != null".

Thanks in advance,

TLS

  • Are you sure it is because of null and not instanceof check? Maybe the source is a UICommand? – mrembisz Feb 15 '12 at 11:52
  • I splitted the checks into 2 if blocks and the instanceof passed while the != null failed ;) –  Feb 15 '12 at 12:05
  • Your `deleteUser` method is not being called directly from the JSF, but appears to be called implicitly from `hibernateUserHandling` passing what seems to be a method name argument for using reflection to invoke the bean argument. If anything I would say that is the cause of your problem. Why not just call the managed bean deleteUser action listener method directly? – maple_shaft Feb 15 '12 at 12:30
  • @maple_shaft In my environment the direct call HibernateUserHandling.deleteUser() doesn't work, so I have to call it like HibernateUserHandling['deleteUser'] –  Feb 15 '12 at 13:20
  • @TLS According to your stacktrace for an actionListener you do not need the parentheses. It assumes that the ActionEvent will be passed. – maple_shaft Feb 15 '12 at 13:29

1 Answers1

2

The ActionEvent can't be null. It would be a huge bug in the JSF implementation used. However, the ActionEvent#getSource() can never be an instance of HtmlDataTable in this particular case. It's an instance of HtmlCommandButton since you're calling it from a <h:commandButton>. So the whole if block will never pass.

That said, this is a pretty clumsy way to get the current Person to delete. Perhaps you focused too much on completely outdated JSF 1.x based books/tutorials/resources. There are much better ways to get the current Person, leading with this:

<h:dataTable value="#{hibernateDataOutput.persons}" var="person">
    <h:column>
        <h:commandButton action="#{hibernateUserHandling.deleteUser(person)}" image="delete.jpg" />
    </h:column>
</h:dataTable>

with

public void deleteUser(User user) {
    someUserService.delete(user);
}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • When I try to call the method directly it leads to Stacktrace error i edited in topic –  Feb 15 '12 at 13:22
  • So you have still not fixed your previous problem? I've already answered how that is caused and how you can fix that: http://stackoverflow.com/questions/9278986/command-button-with-el-method-argument-does-not-invoke-action-method For alternatives and other in-depth explanation, please follow the "See also" link. – BalusC Feb 15 '12 at 13:25
  • Thank you for your solution! I checked it again and this time it skipped the block with instanceof. The last times I tried it it always skipped the != null block. But the problem is in my company we work on a sort of bigger project implementations and use tomcat 6. Think our manager won't change it for a tomcat 7 server –  Feb 15 '12 at 13:27
  • 1
    I strongly recommend you to fix the EL problem or at least to follow the "See also" link for other ways. The way as you originally had is too clumsy. For example, rather use `` instead. – BalusC Feb 15 '12 at 13:28
  • Thank you so much now its working fine! The setPropertyActionListener was the solution. –  Feb 15 '12 at 13:38
  • Was there any reason why you can't use EL 2.2? Are you running an outdated servletcontainer? If so, is there any reason why you can't use JBoss EL as suggested in "See also" link of my answer on your previous question so that you can still pass method arguments in EL 2.1. – BalusC Feb 15 '12 at 13:41