a4j:commandButton actionListener未被调用

时间:2014-04-22 14:42:50

标签: java-ee richfaces

我遇到一个问题,即服务器上没有调用actionListener上的<a4j:commandButton>。我已经制作了我的真实代码的简化版本,并确认它证明了这个问题。

有人可以解释一下为什么没有调用actionListener吗?

使用了3个文件:index.xhtml,MyManagedBean.java和MyEntity.java。以下是列表:

的index.xhtml

<a4j:commandButton>

MyManagedBean.java

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<f:metadata>
    <f:event type="preRenderView" listener="#{myManagedBean.preRenderView}" />
</f:metadata>

<h:head>
    <title>A simple example</title>
</h:head>

<h:body>
    <rich:panel id="treePanel" header="Tree Panel">
        <h:form>
            <rich:tree var="entity" selectionType="ajax" selectionChangeListener="#{myManagedBean.onSelectionChanged}"
                render="treePanel,editPopup">
                <rich:treeModelAdaptor nodes="#{myManagedBean.someEntities}">
                    <rich:treeNode>
                        <h:outputText value="#{entity.someString}" />
                    </rich:treeNode>
                </rich:treeModelAdaptor>
            </rich:tree>
            <a4j:commandButton value="Rename..." onclick="#{rich:component('editPopup')}.show()"
                disabled="#{empty myManagedBean.selectedEntity}" />
        </h:form>
    </rich:panel>

    <rich:popupPanel id="editPopup" modal="true" autosized="true" resizeable="false"
        rendered="#{not empty myManagedBean.selectedEntity}" domElementAttachment="form">
        <f:facet name="header">
            <h:outputText value="Edit entity" />
        </f:facet>
        <h:form>
            <h:panelGrid columns="2">
                <h:outputLabel for="id" value="ID:" />
                <h:outputText id="id" value="${myManagedBean.selectedEntity.id}" />

                <h:outputLabel for="someString" value="Some string:" />
                <h:inputText id="someString" value="${myManagedBean.selectedEntity.someString}"
                    validatorMessage="Alphanumerics and spaces only.">
                    <f:validateRegex pattern="^[ 0-9A-Za-z]*$" />
                    <rich:validator />
                </h:inputText>
                <h:outputText />
                <rich:message for="someString" />
            </h:panelGrid>

            <a4j:commandButton actionListener="#{myManagedBean.updateSelectedEntity}" value="Update"
                oncomplete="#{rich:component('editPopup')}.hide()" render="treePanel" />
            <a4j:commandButton value="Cancel" onbegin="#{rich:component('editPopup')}.hide()" />
        </h:form>
    </rich:popupPanel>

</h:body>
</html>

MyEntity.java

package com.example.simple;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import org.richfaces.component.UITree;
import org.richfaces.event.TreeSelectionChangeEvent;

@ManagedBean
@SessionScoped
public class MyManagedBean {

    private List<MyEntity> someEntities;

    private MyEntity selectedEntity = null;

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager em;

    @Resource
    UserTransaction utx;

    public void preRenderView() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
        System.out.println( "preRenderView executed" );
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<MyEntity> cq = cb.createQuery( MyEntity.class );
        Root<MyEntity> entity = cq.from( MyEntity.class );
        cq.select( entity );
        TypedQuery<MyEntity> q = em.createQuery( cq );
        someEntities = q.getResultList();

        if ( someEntities.isEmpty() ) {
            utx.begin();

            for ( int i = 0; i < 10; i++ ) {
                MyEntity newEntity = new MyEntity();
                newEntity.setSomeString( new StringBuilder( "Entity number " ).append( i ).toString() );
                em.persist( newEntity );
                someEntities.add( newEntity );
            }


            em.flush();
            utx.commit();
        }
    }

    public List<MyEntity> getSomeEntities() {
        return someEntities;
    }

    public void setSomeEntities( List<MyEntity> someEntities ) {
        this.someEntities = someEntities;
    }

    public MyEntity getSelectedEntity() {
        return selectedEntity;
    }

    public void setSelectedEntity( MyEntity selectedEntity ) {
        this.selectedEntity = selectedEntity;
    }

    public void onSelectionChanged( TreeSelectionChangeEvent event ) {
        List<Object> selection = new ArrayList<Object>( event.getNewSelection() );
        Object newSelectionKey = selection.get( 0 );
        UITree tree = (UITree) event.getSource();
        tree.setRowKey( newSelectionKey );
        setSelectedEntity( (MyEntity) tree.getRowData() );
    }

    public void updateSelectedEntity() throws NotSupportedException, SystemException, SecurityException, IllegalStateException, RollbackException, HeuristicMixedException, HeuristicRollbackException {
        utx.begin();
        em.merge( selectedEntity );
        em.flush();
        utx.commit();
    }
}

1 个答案:

答案 0 :(得分:0)

似乎你有嵌套的表格,摆脱它们(即只保留一个)。

顺便说一下,这看起来并不像简化版。 <my:editForm>会以任何方式影响问题吗?