JSF复合组件UICommand操作不能与多个p:commandLink一起使用

时间:2015-05-25 19:18:43

标签: jsf primefaces composite-component commandlink

我试图创建一个与primefaces rowEdit铅笔图标类似的复合,但是对于rowRemove。它需要像这样工作:

  1. 首先,它显示带有垃圾桶图标的commandLink
  2. 当用户点击垃圾桶图标时,它必须隐藏垃圾箱并显示两个新的commandLinks:一个检查图标以确认删除操作,一个取消图标取消操作并再次显示垃圾桶图标。
  3. 但问题是,在呈现确认图标后,它们无法正常工作。它们在动态渲染后不会调用任何动作,除了在我点击两个按钮(确认或取消)后渲染垃圾桶图标,我只是不知道为什么在没有调用动作的情况下更改渲染按钮

    那么,我做错了什么?也许我无法将我的组件扩展到UICommand?但如果这是问题,我该扩展什么?

    哦,我工作的项目有 primefaces-2-2-1.jar spring-webmvc-3.0.2.RELEASE.jar 作为依赖。

    以下是我的代码:

    网络/资源/测试/ removeDataTableRow.xhtml

    <?xml version='1.0' encoding='ISO-8859-1' ?>
    <!DOCTYPE composition 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:p="http://primefaces.prime.com.tr/ui"
          xmlns:composite="http://java.sun.com/jsf/composite"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core">
    
        <composite:interface componentType="removeDataTableRow">
            <composite:attribute name="action" required="false" 
                                 method-signature="void listener()" />
    
            <composite:attribute name="update" required="true"
                                 type="java.lang.String" />
        </composite:interface>
    
        <composite:implementation>
            <h:panelGroup id="removeButtons">
                <p:commandLink id="btnTrash" actionListener="#{cc.componentAction}" 
                               rendered="#{!cc.flagShowConfirmationIcon}" 
                               update="removeButtons">
                    <span class="ui-icon ui-icon-trash" />
                </p:commandLink>
                <p:commandLink id="btnConfirm" actionListener="#{cc.action}"
                               rendered="#{cc.flagShowConfirmationIcon}" 
                               update="removeButtons #{cc.attrs.update}">
                    <span class="ui-icon ui-icon-check"/>
                </p:commandLink>
                <p:commandLink id="btnCancel" actionListener="#{cc.componentAction}" 
                               rendered="#{cc.flagShowConfirmationIcon}" 
                               update="removeButtons">
                    <span class="ui-icon ui-icon-close" />
                </p:commandLink>
            </h:panelGroup>
        </composite:implementation>
    </html>
    

    RemoveDataTableRowComponent.java

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.el.ELContext;
    import javax.el.MethodExpression;
    import javax.faces.application.Application;
    import javax.faces.component.FacesComponent;
    import javax.faces.component.NamingContainer;
    import javax.faces.component.UICommand;
    import javax.faces.component.UINamingContainer;
    import javax.faces.context.FacesContext;
    
    @FacesComponent("removeDataTableRow")
    public class RemoveDataTableRowComponent extends UICommand implements NamingContainer {
    
        private boolean flagShowConfirmationIcon = false;
    
        private final Logger LOG = Logger.getLogger(RemoveDataTableRowComponent.class.getName());
    
        public void componentAction() {
            flagShowConfirmationIcon = !flagShowConfirmationIcon;
    
            LOG.log(Level.INFO, 
                    "Setting flagShowConfirmationIcon to inverse value");
        }
    
        public void removeAction() {
            LOG.log(Level.INFO, "Calling the removeAction()");
    
            componentAction();
    
            MethodExpression methodExpression = getActionExpression();
    
            FacesContext facesContext = FacesContext.getCurrentInstance();
            Application application = facesContext.getApplication();
            ELContext elContext = facesContext.getELContext();
    
            methodExpression.invoke(elContext, new Object[0]);
        }
    
        @Override
        public String getFamily() {
            return UINamingContainer.COMPONENT_FAMILY;
        }
    
        public boolean isFlagShowConfirmationIcon() {
            return flagShowConfirmationIcon;
        }
    }
    

    TestBean.java

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import org.springframework.context.annotation.Scope;
    
    @Scope(value = "view")
    @ManagedBean
    public class TestBean implements Serializable {
        private List<String> listStrings;
    
        private final Logger LOG = Logger.getLogger(TestBean.class.getName());
    
        @PostConstruct
        public void init() {
            listStrings = new ArrayList<String>();
            listStrings.add("A");
            listStrings.add("B");
            listStrings.add("C");
        }
    
        public void removeItem(String item) {
            listStrings.remove(item);
    
            LOG.log(Level.INFO, "The item " + item + " was removed.");
        }
    
        public List<String> getListStrings() {
            return listStrings;
        }
    }
    

    网/ test.xhtml

    <?xml version='1.0' encoding='ISO-8859-1' ?>
    <!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:p="http://primefaces.prime.com.tr/ui"
          xmlns:t="http://java.sun.com/jsf/composite/test">
    
        <h:body>
            <h:form id="testForm">
                <p:dataTable id="dataTable" var="item" value="#{testBean.listStrings}">
                    <p:column headerText="Value">
                        <h:outputText value="#{item}" />
                    </p:column>
    
                    <p:column headerText="Remove">
                        <t:removeDataTableRow
                            update="dataTable" />
                    </p:column>
                </p:dataTable>
            </h:form>
        </h:body>
    </html>
    

0 个答案:

没有答案