与h:commandLink / h:commandButton关联的actionListener未被调用

时间:2014-12-25 06:06:52

标签: jsf commandbutton commandlink

我有两个包含4个按钮的表单,每个按钮都有一个actionListener属性设置,用于调用请求范围bean中的单个方法。此方法检查以确定执行操作的按钮,然后根据该信息(创建,读取,更新或删除)执行一些操作。问题是,在这四个按钮中,有三个工作。最后一个(更新)按钮未调用该操作。

This问题有点帮助,因为在阅读之后,我设法让第四个按钮工作一次(断点和变量观察确定) - 但后来我停止了程序,做了一个小改动因为页面无法呈现页面的一部分(取决于'编辑'变量),看着它再次失败,然后忘记我做了什么让它工作。我已经确定问题不是嵌套表单,我正在导入正确的事件类,没有验证错误,没有迭代标记,按钮正在渲染但未被禁用,或任何@BalusC在回答这个问题时提到的其他问题除了一个 - 当我使用<h:commandLink>时,JS会在返回false的页面上呈现,尽管更改为<h:commandButton>仍然无法调用{ {1}}。但奇怪的是,曾经有一次工作并且调用了行动我使用的是actionListener

有谁看到我做错了什么?

表格1(创建;作品):

<h:commandLink>

表单2(阅读(从重定向到此列表的列表页面中单击的详细信息按钮)可用,更新;不起作用,删除;工作):

    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <h:head></h:head>

    <h:body>
        <ui:composition template="../../template.xhtml">
            <ui:define name="top">
                New Location View
            </ui:define>

            <ui:define name="left"></ui:define>

            <ui:define name="content">
                <f:view>
                    <h:form>
                        <h2><h:outputText value="Create a Location"/></h2>
                        <h:panelGrid columns="2">
                            <h:outputLabel for="locationName" value="Name:"/>
                            <h:inputText id="locationName" value="#{locationBean.locationName}"/>
                            <h:outputLabel for="locationDesc" value="Description:"/>
                            <h:inputText id="locationDesc" value="#{locationBean.locationDescription}"/>
                            <h:commandButton value="Add Location" id="createact" actionListener="#{locationBean.doAction}"/>
                        </h:panelGrid>
                    </h:form>
                </f:view>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

doAction方法(从所有四个按钮调用):

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>

    </h:head>
    <h:body>
        <ui:composition template="../../../template.xhtml">
            <ui:define name="top">
                Location Details View
            </ui:define>

            <ui:define name="left">

            </ui:define>

            <ui:define name="content">
                <c:set var="location" value="#{locationBean.l}"/>

                <h:form>
                    <h:panelGrid columns="2" rendered="#{not locationBean.editing}" >
                        <h:outputLabel value="Name:"/>
                        <h:outputText  id="nameout" value="#{location.locationname}"/>
                        <h:outputLabel value="Description:" />
                        <h:outputText  id="descout" value="#{location.description}"/>
                        <h:commandButton id="delact"  actionListener="#{locationBean.doAction}" value="Delete"/>
                        <h:commandButton id="editact"  actionListener="#{locationBean.doAction}" value="Edit"/>
                    </h:panelGrid>
                    <h:panelGrid columns="2" rendered="#{locationBean.editing}">
                        <h:outputLabel value="Name:" />
                        <h:inputText   id="namein" value="#{location.locationname}" required="true" requiredMessage="There must be a name."/>
                        <h:outputLabel value="Description:" />
                        <h:inputText  id="descin" value="#{location.description}" required="true" requiredMessage="There must be a description."/>
                        <h:commandLink id="cancelact" actionListener="#{locationBean.doAction}" value="Cancel"/>
                        <h:commandLink id="saveact" actionListener="#{locationBean.doAction}" value="Save"/>
                    </h:panelGrid>
                    <h:inputHidden value="#{location.identifier}" id="viewid" />
                    <c:catch var="error">
                        <h:outputLabel value="#{error.message}" />
                    </c:catch>
                </h:form>
                <h:outputText value="will also show location hours and days open and employees at this location."/>
            </ui:define>
        </ui:composition>
    </h:body>
</html>

模板文件:

public void doAction(ActionEvent e) {
    String callingID = e.getComponent().getId();
    try {
        if (callingID.isEmpty()) {//id is empty
            FacesContext.getCurrentInstance().addMessage(callingID, new FacesMessage(
                    "What are you doing wrong? Actions must have a valid id.",
                    "Empty Strings are not a valid id."));
        } else if (callingID.endsWith("delact")) {//component id is delete
            this.l = lsb.getLocationToView();
            lsb.delete(l);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/EmployeeScheduler-war/webpages/view/locations.xhtml");
        } else if (callingID.endsWith("editact")) {//component id is edit
            this.editing = true;
            this.l = lsb.getLocationToView();
        } else if (callingID.endsWith("saveact")) {//component id is save, usually after an edit action
            this.editing = false;
            lsb.edit(l);
        } else if (callingID.endsWith("cancelact")) {//component id is cancel. usuallys after edit action
            this.editing = false;
        } else if (callingID.endsWith("createact")) {//component id is create. always create a new location
            FacesContext.getCurrentInstance().getExternalContext().redirect("/EmployeeScheduler-war/webpages/view/locations.xhtml");
            lsb.create(l);
        } else if (callingID.endsWith("detailsact")) {//component id is details. alwas from list view
            this.l = lsb.getLocationToView();
        } else {
            FacesContext.getCurrentInstance().addMessage(callingID, new FacesMessage(
                    "What are you doing wrong? Actions must have a valid id.",
                    callingID + " is not not valid."));
        }
        //catch any failures
    } catch (RollbackFailureException ex) {
        FacesContext.getCurrentInstance().addMessage(callingID, new FacesMessage(
                "An error occured trying to rollback the deletion of the location: " + ex.getMessage(),
                "Details: " + ex.getCause().getMessage()));
    } catch (Exception ex) {
        FacesContext.getCurrentInstance().addMessage(callingID, new FacesMessage(
                "An error occured trying to delete the location: " + ex.getMessage(),
                "Details: " + (ex.getCause() == null ? "none" : ex.getCause().getMessage())));
    }
}

0 个答案:

没有答案
相关问题