p:commandButton / p中的action / ActionListener:未触发commandLink

时间:2011-12-29 01:12:36

标签: xhtml jsf-2 primefaces facelets

我读过这篇文章:commandButton/commandLink/ajax action/listener method not invoked or input value not updated

但我仍然无法理解为什么我的问题没有解决。 我担心我对英语解释的理解并不完全正确。所以我会在这里放下我的代码片段。

是的,我仍然对中英文有一定的了解。我的英语能力一般。

我正在使用Primefaces 3.0.RC1,EL库2.2,JSF 2.1,JSTL 1.1,GlassFish 3.x,用Netbeans 7.0.1编码

现在我们来了代码:

的template.xhtml          

<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <h:outputStylesheet name="default.css" library="css"/>
    <h:outputStylesheet name="cssLayout.css" library="css"/>
    <h:outputStylesheet name="style.css" library="css"/>
    <title>MFI Core Banking System</title>
</h:head>

<h:body>
    <f:view>
        <div id="top">
            <ui:include src="header.xhtml"/>
        </div>

        <div id="menuBar">
            <ui:include src="menubar.xhtml"/>
        </div>

        <div id="content" class="center_content">
            <ui:insert name="content">Content</ui:insert>
        </div>

        <div id="bottom">
            <ui:include src="footer.xhtml"/>
        </div>
    </f:view>


</h:body>

header.xhtml和footer.xhtml只有UI:Composition声明以及<div/><a href/>标记。

menubar.xhtml如下:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:p="http://primefaces.org/ui">
<f:view>
    <h:form>
        <p:menubar >
            <p:menuitem value="Home" action="/index.xhtml?faces-redirect=true" icon="ui-icon ui-icon-home"/>
            <p:submenu label="CIF" icon="ui-icon ui-icon-bookmark">
                <p:menuitem value="Manajemen CIF" action="/modul/cif/cifManagement?faces-redirect=true"/>
                <p:submenu label="CIF Grouping ">
                    <p:menuitem value="Search" action="/modul/cif/SearchGroupNew.xhtml?faces-redirect=true"/>
                    <p:menuitem value="Create" action="/modul/cif/CreateGroupNew.xhtml?faces-redirect=true"/>
                </p:submenu>
                <p:menuitem value="Analyze" url="#"/>
                <p:menuitem value="Directorate " action="/modul/userManagement/Struktural.Directorate.xhtml?faces-redirect=true"/>
            </p:submenu>
        </p:menubar>
    </h:form>
</f:view>
</ui:composition>

这是我的示例页面(理事会):

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
            template="./../../template.xhtml"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:p="http://primefaces.org/ui">

<ui:define name="top">
    Directorate
</ui:define>

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

<ui:define name="content">
    <f:view contentType="text/html">
        <h:form prependId="false">
            <p:panel toggleable="true" closable="true" header="Direktorat">
                <p:fieldset legend="Tambah Direktorat"
                            toggleable="TRUE"
                            collapsed="#{directorateBean.fieldsetCollapse}"
                            toggleSpeed="500"
                            >

                    <h:panelGrid columns="3">
                        <h:outputLabel for="directorateCode" value="Kode Direktorat*"/>
                        <p:inputText id="directorateCode" value="#{directorateBean.directorate.directoratecode}" required="true" label="Kode Direktorat" maxlength="4"/>
                        <p:message for="directorateCode"/>

                        <h:outputLabel for="directorateName" value="Nama Direktorat*"/>
                        <p:inputText id="directorateName" value="#{directorateBean.directorate.directoratename}" required="true" label="Nama Direktorat" maxlength="40"/>
                        <p:message for="directorateName"/>

                        <h:outputLabel for="directorateStatus" value="Status Direktorat"/>
                        <p:selectOneRadio id="directorateStatus" value="#{directorateBean.directorate.directoratestatus}">
                            <f:selectItem itemValue="true" itemLabel="Aktif"/>
                            <f:selectItem itemValue="false" itemLabel="Tidak Aktif"/>
                        </p:selectOneRadio>
                        <p:spacer/>

                        <p:commandButton id="btnSubmitDirectorate" value="Submit" update="@form" action="#{directorateBean.add()}"/>
                        <p:commandButton value="Reset" type="reset"/>

                    </h:panelGrid>

                </p:fieldset>

                <p:spacer/><p:spacer/>

                <p:dataTable
                    id="tableDirectorate"
                    emptyMessage="Tidak Ada Direktorat"
                    value="#{directorateBean.directorateList}"
                    var="dir"
                    paginator="true"
                    paginatorPosition="bottom"
                    rows="10"
                    rowsPerPageTemplate="10,20,50"
                    >
                    <f:facet name="header">Daftar Direktorat</f:facet>

                    <p:column sortBy="#{dir.directoratecode}" filterBy="#{dir.directoratecode}" filterMatchMode="contains" >
                        <f:facet name="header"><h:outputText value="Kode Direktorat"/></f:facet>
                        <h:outputText value="#{dir.directoratecode}"/>
                        <h:commandLink value="#{dir.directoratecode}" actionListener="#{directorateBean.test}"/>
                    </p:column>

                    <p:column sortBy="#{dir.directoratename}" filterBy="#{dir.directoratename}" filterMatchMode="contains"  >
                        <f:facet name="header"><h:outputText value="Nama Direktorat"/></f:facet>
                        <h:outputText value="#{dir.directoratename}"/>
                    </p:column>

                    <p:column style="width: 100px;">
                        <f:facet name="header"><h:outputText value="Direktorat Status"/></f:facet>
                        <p:commandButton icon="ui-icon ui-icon-check" title="Active" rendered="#{dir.directoratestatus == true}"/>
                        <p:commandButton icon="ui-icon ui-icon-closethick" title="Inactive" rendered="#{dir.directoratestatus == false}"/>
                    </p:column>

                    <p:column style="width: 100px;">
                        <p:commandButton icon="ui-icon ui-icon-search" title="View #{dir.directoratename} Details"/>
                        <p:commandButton icon="ui-icon ui-icon-pencil" title="Edit #{dir.directoratename}" action="#{directorateBean.edit(dir)}" update="panelDetail" oncomplete="updateDlg.show();"/>
                        <p:commandButton icon="ui-icon ui-icon-trash" title="Delete #{dir.directoratename}"/>
                    </p:column>
                </p:dataTable>
            </p:panel>


            <p:dialog header="Corfirmation" widgetVar="confirmDialog" resizable="false" id="confirm" showEffect="fade" modal="true" >
                <h:outputText value="Confirm Delete ?"/>
                <h:panelGrid columns="2">
                    <p:commandButton value="Cancel" update="@form" />
                    <p:commandButton value="Delete" update="@form"/>
                </h:panelGrid>
            </p:dialog>

            <p:dialog header="Directorate Detail" widgetVar="updateDlg" resizable="false" id="updateDialog" showEffect="fade" modal="true" >
                <h:panelGrid id="panelDetail" columns="3" cellpadding="2">
                    <h:outputLabel for="editDirectorateCode" value="Kode Direktorat*"/>
                    <p:inputText id="editDirectorateCode" value="#{directorateBean.selectedDirectorate.directoratecode}" required="true" label="Kode Direktorat" maxlength="4"/>
                        <p:message for="editDirectorateCode"/>

                        <h:outputLabel for="editDirectorateName" value="Nama Direktorat*"/>
                        <p:inputText id="editDirectorateName" value="#{directorateBean.selectedDirectorate.directoratename}" required="true" label="Nama Direktorat" maxlength="40"/>
                        <p:message for="editDirectorateName"/>

                        <h:outputLabel for="editDirectorateStatus" value="Status Direktorat"/>
                        <p:selectOneRadio id="editDirectorateStatus" value="#{directorateBean.selectedDirectorate.directoratestatus}">
                            <f:selectItem itemValue="true" itemLabel="Aktif"/>
                            <f:selectItem itemValue="false" itemLabel="Tidak Aktif"/>
                        </p:selectOneRadio>
                        <p:spacer/>

                        <p:commandButton id="btnUpdateDirectorate" value="Simpan" update="@form" action="#{directorateBean.update()}"/>
                        <p:commandButton value="Reset" type="reset"/>
                </h:panelGrid>
            </p:dialog>
        </h:form>
    </f:view>
</ui:define>

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

</ui:composition>

这是托管bean:

@ManagedBean(name = "directorateBean")
@ViewScoped
public class structuralDirectorateManagedBean implements Serializable {

private DirectorateDAO directorateService;
/* Initialize variable */
private List<Directorate> directorateList;
private Directorate directorate = new Directorate();
private Directorate selectedDirectorate = new Directorate();
private static final String Creator;
private boolean fieldsetCollapse = true;
private boolean DirectorateStatus = false;

/* init */
@PostConstruct
public void init() throws SQLException {
    if (directorateService == null) {
        directorateService = FactoryDAO.getDirectorateDAO();
    }
    directorateList = directorateService.findAll();
    directorate.setDirectoratestatus(DirectorateStatus);
    directorate.setCreator(Creator);
}

/* Basic Method */
public void add() throws SQLException {
    System.out.println("Directorate Code : " + directorate.getDirectoratecode());
    directorateService.create(directorate); // insert record to datatabse
    directorateList.add(directorate); // insert object to List<Object>
    directorate = new Directorate(); // Reset Form
    directorate.setDirectoratestatus(DirectorateStatus); // set default
    directorate.setCreator(Creator); // set default
}

public  void test(){
    System.out.println("TEST!!");
}

public void edit(Directorate directorate) {
    System.out.println("Directorate code to edit : " + directorate.getDirectoratecode());
    this.selectedDirectorate = directorate;
}

public void delete(Directorate directorate) throws SQLException {
    directorateService.remove(directorate); // delete record within Database
    directorateList.remove(directorate); // delete record within List
}

public void update(ActionEvent event) throws SQLException {
    directorateService.edit(selectedDirectorate); // update record to database
    selectedDirectorate = new Directorate(); // reset form
}

/** Getter Setter */
public List<Directorate> getDirectorateList() throws SQLException {

    return directorateList;
}

public Directorate getDirectorate() {
    return directorate;
}

public boolean isFieldsetCollapse() {
    return fieldsetCollapse;
}

public void setFieldsetCollapse(boolean fieldsetCollapse) {
    this.fieldsetCollapse = fieldsetCollapse;
}

public Directorate getSelectedDirectorate() {
    return selectedDirectorate;
}

public void setSelectedDirectorate(Directorate selectedDirectorate) {
    this.selectedDirectorate = selectedDirectorate;
}

}

一切顺利,从数据库显示记录,p:selectOneMenu运行良好。

此按钮运行良好:

<p:commandButton id="btnSubmitDirectorate" value="Submit" update="@form" action="#{directorateBean.add()}"/>

但是,这个按钮和commandLink不起作用,Action和ActionListener都没有被触发:

<p:commandButton icon="ui-icon ui-icon-pencil" title="Edit #{dir.directoratename}" action="#{directorateBean.edit(dir)}" update="panelDetail" oncomplete="updateDlg.show();"/>

<p:commandLink value="#{dir.directoratecode}" actionListener="#{directorateBean.test}"/>

我很抱歉,如果我问得太多,也许我在这里很蠢...... XD

但我无法克服它,只是开始感到沮丧..

感谢您的回复...... :) 祝你有美好的一天..

1 个答案:

答案 0 :(得分:4)

“Directorate”视图中有一个大的<h:form>,有很多不同的部分。当您在其中一个部分中调用操作时,也将提交(并验证!)所有其他部分的数据。看起来您没有注意到那些“值是必需的”验证消息,因为默认情况下这些部分是折叠/隐藏的。

你需要删除“上帝”<h:form>并给出字段集,数据表和两个对话框,每个对话框都有<h:form>。所以你应该有4个单独的表格,每个表格都有自己的责任。但是,它们可以毫无问题地绑定到同一个bean。

顺便说一句,<f:view>不属于包含文件,也不属于模板客户端。主模板中应该只有一个。