JSF 2 - commandButton在渲染后没有做动作

时间:2012-07-20 04:08:20

标签: ajax jsf-2 primefaces cdi

嘿我一整天都在苦苦挣扎,我还没有找到答案。所以我的问题是我的xhtml页面中有2个表单,第一个表示填充了我的数据库中的数据的primefaces数据表,第二个填充了来自所选用户的数据以进行编辑。问题是,第二种形式有2个commandButtons但是在我渲染之后(通过ajax调用),按钮似乎没有调用action属性中的方法。

这是我的代码,让它更清楚。

这是我在xhtml页面中的第一个表单

<h:form id="form" style="width:710px;">
    <p:dataTable id="dataTable" var="supervisor" value="#{tablaBean.modeloUsuario}"  
                             paginator="true" rows="15" paginatorPosition="bottom" selection="#{tablaBean.usuarioSel}"
                             paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {NextPageLink} {LastPageLink}"
                             rendered="#{tablaBean.showAll}">  
                    <f:facet name="header">  
                        <h:outputText value="Supervisores" />
                    </f:facet>  
                    <p:column  selectionMode="single" style="width:18px" />
                    <p:column headerText="Id" filterBy="#{supervisor.idUsuario}" filterStyle="width:35px;">  
                        <h:outputText value="#{supervisor.idUsuario}" />  
                    </p:column>   
                    <p:column headerText="Nombre" filterBy="#{supervisor.nombre}" id="nombreh">  
                        <h:outputText value="#{supervisor.nombre}" />  
                    </p:column>   
                    <p:column headerText="Ap. Paterno" filterBy="#{supervisor.apaterno}" id="apat">  
                        <h:outputText value="#{supervisor.apaterno}" />  
                    </p:column>   
                    <p:column headerText="Ap. Materno" filterBy="#{supervisor.amaterno}" id="amat">  
                        <h:outputText value="#{supervisor.amaterno}" />  
                    </p:column>   
                    <p:column headerText="Usuario" filterBy="#{supervisor.nombreUsuario}" id="user">  
                        <h:outputText value="#{supervisor.nombreUsuario}" />  
                    </p:column>    
                    <p:column headerText="Depto." filterBy="#{supervisor.departamento.nombre}" id="deptoh">  
                        <h:outputText value="#{supervisor.departamento.nombre}" />  
                    </p:column>
                    <f:facet name="footer">
                        <p:commandButton id="ver" value="Ver" icon="ui-icon-search" update=":form:userData" oncomplete="user.show()" />
                        <p:commandButton id="editar" value="Editar" icon="ui-icon-pencil" update=":form,:formEdit" action="#{tablaBean.edit()}" />
                    </f:facet>
                </p:dataTable>
            </h:form>

这是我的第二张表格中的代码

<h:form id="formEdit">
<p:growl />
<p:fieldset legend="Editar Usuario" style="margin: auto;" rendered="#{!tablaBean.showAll}">
    <h:panelGrid columns="3">
        <h:outputText value="Id" />
        <h:outputText value="#{tablaBean.usuarioSel.idUsuario}" id="id" />
        <p:message for="id" />
        <p:outputLabel for="nombre" value="Nombre" />
        <p:inplace>
            <p:inputText value="#{tablaBean.usuarioSel.nombre}" id="nombre" required="true" />
        </p:inplace>
        <p:message for="nombre" />
        <p:outputLabel for="appat" value="Apellido Paterno" />
        <p:inplace>
            <p:inputText value="#{tablaBean.usuarioSel.apaterno}" id="appat" required="true"/> 
        </p:inplace>
        <p:message for="appat" />
        <p:outputLabel for="apmat" value="Apellido Materno" />
        <p:inplace>
            <p:inputText value="#{tablaBean.usuarioSel.amaterno}" id="apmat" required="true"/> 
        </p:inplace>
        <p:message for="apmat" />
        <p:outputLabel for="usuario" value="Usuario" />
        <p:inplace>
            <p:inputText value="#{tablaBean.usuarioSel.nombreUsuario}" id="usuario" required="true"> 
                <f:ajax render="msgUsuario" event="blur" />
                <f:validator binding="#{userNameValidator}" />
            </p:inputText>
        </p:inplace>
        <p:message for="usuario" id="msgUsuario" />
        <p:outputLabel for="depto" value="Departamento" />
        <p:inplace>
            <p:inputText value="#{tablaBean.usuarioSel.departamento.nombre}" id="depto" required="true" /> 
        </p:inplace>
        <p:message for="depto" />
    </h:panelGrid>
    <p:commandButton value="Guardar" icon="ui-icon-disk" action="#{tablaBean.editarUsuario()}" />
    <p:commandButton id="editar" value="Cancelar" update=":form,:formEdit" icon="ui-icon-pencil" action="#{tablaBean.edit()}" />
</p:fieldset>

因此,当我调用方法“edit()”时,我更改了showAll变量的值,因此它呈现第二个表单并“隐藏”第一个表单。问题是在我这样做后,第二种形式的命令按钮什么都不做。 (甚至不输入方法)

这是我的豆子

    @Named(value = "tablaBean")
    @ConversationScoped

public class TablaBean implements Serializable {

    @Inject
    private UsuariosDao usuario;
    @Inject
    private Usuarios usuarioSel;
    @Inject
    private Conversation conversation;
    private UserDataModel modeloUsuario;
    private boolean showAll;

    /**
     * Creates a new instance of TablaBean
     */
    public TablaBean() {
    }

    @PostConstruct
    public void init() {
        start();
        modeloUsuario = new UserDataModel(usuario.findSupers());
        showAll = true;
    }

    public void start(){
        conversation.begin();
    }

    public void end(){
        conversation.end();
    }

    public Usuarios getUsuarioSel() {
        return usuarioSel;
    }

    public void setUsuarioSel(Usuarios usuarioSel) {
        this.usuarioSel = usuarioSel;
    }

    public UserDataModel getModeloUsuario() {
        return modeloUsuario;
    }

    public boolean isShowAll() {
        return showAll;
    }

    public void setShowAll(boolean showAll) {
        this.showAll = showAll;
    }

    public UsuariosDao getUsuario() {
        return usuario;
    }

    public void setUsuario(UsuariosDao usuario) {
        this.usuario = usuario;
    }

    public String edit() {
        System.out.println("holis");
        showAll = !showAll;
        return "";
    }

    public String editarUsuario(){
        System.out.println("hola");
        end();
        return "";
    }
}

对于我可能做错的任何建议表示赞赏,提前谢谢。

修改

我做了一些调试,我发现每次点击第一个表单中的编辑按钮时都会调用我的托管bean,所以我猜测问题是我的会话bean。

编辑2

我将支持bean的范围更改为Application,现在它在按下编辑按钮时没有被调用,但是命令按钮仍然不起作用

2 个答案:

答案 0 :(得分:0)

不确定您使用的是哪个版本的PrimeFaces。我已经看到在先前版本中使用PrimeFaces ajax正确传播CDI对话的问题。还看到了以前版本的Mojarra的问题。似乎这可能是相关的。

答案 1 :(得分:0)

发现我的错误,<f:validator binding="#{userNameValidator}" />行出错了,验证器中有一些NullPointerException错误(我已经修复过)但是......我现在感觉很蠢! xD,我不得不将commandButton转为无ajax实际上看到错误,因为它没有显示在服务器日志中。无论如何,谢谢你的回答。