Faces ActionEvent.getSource始终为null [英] Faces ActionEvent.getSource always null

查看:119
本文介绍了Faces ActionEvent.getSource始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Faces ActionEvent返回一个源的问题,但是对!= null的检查返回false。
疯狂的事实是,在调试模式下,我收到一个填充的ActionEvent变量。



我已将代码分解为重要的部分



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());
}
}

查看:

 < 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 =cssname =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 =deleteactionListener =#{hibernateUserHandling ['deleteUser']}image =delete.jpg/>
< / h:column>
< / h:dataTable>
< / h:form>
< / ui:define>
< / ui:composition>
< / body>
< / html>

我没有收到错误,它只是跳过If块,因为ev.getSource ()!= null。



提前感谢



TLS

解决方案

ActionEvent 不能为 null 。这将是使用JSF实现的一个巨大的bug。但是, ActionEvent#getSource()可以永远不会是这个 HtmlDataTable 的一个实例特殊情况。这是一个 HtmlCommandButton 的实例,因为您从< h:commandButton> 中调用它。所以整个如果块将永远不会通过。



这就是说,这是一个非常笨拙的方法来获取当前个人删除。也许你专注于完全过时的基于JSF 1.x的书籍/教程/资源。有更好的方法来获取当前的 Person ,导致这一点:

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

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



另请参见:




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

解决方案

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:

这篇关于Faces ActionEvent.getSource始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆