Commandbutton不会执行actionListener属性

时间:2015-01-30 10:17:17

标签: jsf primefaces commandbutton

我对HTML / Javascript / Java相对较新,到目前为止,我找到了一个适用于我所有"初学者"问题...但这已经花了我几个小时的时间"尝试和错误": 在我的应用程序中,客户端填写一个表单,然后通过提交按钮传输到服务器bean。

问题是,p:commandButton(在下面的代码中标记)不会从actionListener执行方法表达式。在oncomplete或onsuccess属性中执行javascript代码有效,但由于我想在表单提交后调用支持的bean方法,我认为actionListener属性将是正确的选择..如果它工作。可能很重要的是,表单位于主要对话窗口内。

如果您需要更多信息,例如javascript或java代码,请告诉我。

有人知道问题是什么吗? 提前谢谢。

下面是一些代码来说明我的问题..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

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

    <h:head>
        <f:facet name="first">
            <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
            <title>Title</title>
        </f:facet>

        <style>

        </style>
    </h:head>

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>

    </script>

    <h:body id="body">

        <p:growl id="growl" showDetail="true" />

        <p:layout fullPage="true">

            <p:layoutUnit position="north" size="100" resizable="true" closable="true" collapsible="true">
                Ihre Aufgabe besteht darin, 5 Ladesäulen zu setzten wo es ihnen gefällt auf der Karte.
            </p:layoutUnit>

            <p:layoutUnit position="south" size="175">                
                    <h:outputLabel id="outp" value="#{mainController.testparam}" />
            </p:layoutUnit>

            <p:layoutUnit position="west" size="175" collapsible="true">

            </p:layoutUnit>

            <p:layoutUnit position="center" id='idcenter'>

                <div id="map-canvas"></div>   

                <p:dialog id="dlg" modal="true" closeOnEscape="false" fitViewport="true" widgetVar="dlg" closable="false" resizable="false" width="50%" minWidth="500" appendTo="body">
                    <!--><h:form prependId="false" onsubmit="return cancel()"><-->

                    <h:form prependId="false" id="form1">                        
                        <h:panelGrid columns="3" cellpadding="10">
                            <h:outputLabel for="xfield" value="Koordinaten" />
                            <h:inputText disabled="true" id="xfield" value="#{mainController.currentXCoordinate}"/>
                            <h:inputText disabled="true" id="yfield" value="#{mainController.currentYCoordinate}"/>


                            <h:outputLabel for="location" value="Abfrage: PositionDescription" />
                            <p:inputTextarea id="descriptionPosition" autoResize="true" class="textinput" required="false" counter="display1" maxlength="1000" value="#{mainController.currentPositionDescription}" counterTemplate="{0} Zeichen übrig"/>
                            <h:outputText id="display1" class="display"/>
                            <h:outputText value="#{mainController.currentPositionDescription}" rendered="false"/>

                            <h:outputLabel for="reason" value="Abfrage: Reason" />
                            <p:inputTextarea id="reason" autoResize="true" class="textinput" required="false" counter="display2" maxlength="250" value="#{mainController.currentReason}" counterTemplate="{0} Zeichen übrig"/>
                            <h:outputText id="display2" class="display"/>
                            <h:outputText value="#{mainController.currentReason}" rendered="false"/>


                            <h:outputLabel for="ratingImportance" value="Abfrage: Rating Importance" />
                            <p:selectOneMenu id="ratingImportance" value="#{mainController.currentRatingImportance}" required="false">
                                <f:selectItem itemLabel="Rating Importance" itemValue="0" />
                                <f:selectItem itemLabel="Sehr wichtig" itemValue="4" />
                                <f:selectItem itemLabel="Wichtig" itemValue="3" />
                                <f:selectItem itemLabel="Eher unwichtig" itemValue="2" />
                                <f:selectItem itemLabel="Unwichtig" itemValue="1" />
                            </p:selectOneMenu>
                            <h:outputText value="#{mainController.currentRatingImportance}" rendered="true"/>

                            <h:outputLabel for="numberOfChargingPoints" value="Abfrage: NumberOfChargingPoints" />
                            <p:selectOneMenu id="numberOfChargingPoints" value="#{mainController.currentNumberOfChargingPoints}" required="false">
                                <f:selectItem itemLabel="1" itemValue="1" />
                                <f:selectItem itemLabel="2" itemValue="2" />
                                <f:selectItem itemLabel="3" itemValue="3" />
                                <f:selectItem itemLabel="4" itemValue="4" />
                            </p:selectOneMenu>
                            <h:outputText value="#{mainController.currentNumberOfChargingPoints}" rendered="true"/>

                            <h:outputLabel for="chargingStandard" value="Abfrage: Charging Standard" />
                            <p:selectOneMenu id="chargingStandard" value="#{mainController.currentChargingStandard}" required="false">
                                <f:selectItem itemLabel="ChaDeMo" itemValue="ChaDeMo" />
                                <f:selectItem itemLabel="CCS" itemValue="CCS" />
                                <f:selectItem itemLabel="Typ3" itemValue="Typ3" />
                                <f:selectItem itemLabel="Typ2" itemValue="Typ2" />
                                <f:selectItem itemLabel="HPC" itemValue="HPC" />
                                <f:selectItem itemLabel="keine Angabe" itemValue="ka" />
                            </p:selectOneMenu>
                            <h:outputText value="" rendered="true"/>

                            <h:outputLabel for="rating1" value="Abfrage: Rating 1" />
                            <p:inputTextarea id="rating1" class="textinput" required="false" counter="display3" maxlength="250" value="#{mainController.currentRating1}" counterTemplate="Noch {0} Zeichen"/>
                            <h:outputText id="display3" class="display"/>
                            <h:outputText value="#{mainController.currentRating1}" rendered="false"/>

                            <h:outputLabel for="rating2" value="Abfrage: Rating 2" />
                            <p:inputTextarea id="rating2" class="textinput" required="false" counter="display4" maxlength="250" value="#{mainController.currentRating2}" counterTemplate="Noch {0} Zeichen"/>
                            <h:outputText id="display4" class="display"/>
                            <h:outputText value="#{mainController.currentRating2}" rendered="false"/>

                            <h:outputLabel for="rating3" value="Abfrage: Rating 3" />
                            <p:inputTextarea id="rating3" class="textinput" required="false" counter="display5" maxlength="250" value="#{mainController.currentRating3}" counterTemplate="Noch {0} Zeichen"/>
                            <h:outputText id="display5" class="display"/>
                            <h:outputText value="#{mainController.currentRating3}" rendered="false"/>

                            <!-->This commandbutton does not execute the methodExpression<-->
                            <p:commandButton value="Submit" update="form1" icon="ui-icon-check" rendered="true" id="button1" actionListener="#{mainController.saveData}"/>
                            <p:commandButton type="reset" value="Cancel" onclick="return cancel()"/>
                        </h:panelGrid>
                        <h:inputHidden value="#{mainController.currentId}" />

                    </h:form>

                </p:dialog >   
            </p:layoutUnit>

        </p:layout>

    </h:body>


</html>

1 个答案:

答案 0 :(得分:1)

我在你的代码中发现了两个问题。

  1. 您应该使用styleClass属性,它是容器元素的Style类,而不是类。
  2. 您的xhtml代码中找不到位置ID。
  3. 由于您没有发布托管bean,我无法指定您的问题。但是,我尝试自己创建managedbean如下。它的工作正常。

    XHTML

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:p="http://primefaces.org/ui">
    
        <h:head>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
                <title>Title</title>
            </f:facet>
    
            <style>
    
            </style>
        </h:head>
    
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
        <script>
    
        </script>
    
        <h:body id="body">
    
            <p:growl id="growl" showDetail="true" />
    
            <p:layout fullPage="true">
    
                <p:layoutUnit position="north" size="100" resizable="true" closable="true" collapsible="true">
                    Ihre Aufgabe besteht darin, 5 Ladesäulen zu setzten wo es ihnen gefällt auf der Karte.
                </p:layoutUnit>
    
                <p:layoutUnit position="south" size="175">                
                    <h:outputLabel id="outp" value="#{mainController.testparam}" />
                </p:layoutUnit>
    
                <p:layoutUnit position="west" size="175" collapsible="true">
    
                </p:layoutUnit>
    
                <p:layoutUnit position="center" id='idcenter'>
    
                    <div id="map-canvas"></div>   
                    <p:commandButton value="Basic" type="button" onclick="PF('dlg').show();" />
                    <p:dialog id="dlg" modal="true" closeOnEscape="false" 
                              fitViewport="true" widgetVar="dlg" 
                              closable="false" resizable="false" 
                              width="50%" minWidth="500" appendTo="body">
                        <!--><h:form prependId="false" onsubmit="return cancel()"><-->
    
                        <h:form prependId="false" id="form1">                        
                            <h:panelGrid columns="3" cellpadding="10">
                                <h:outputLabel for="xfield" value="Koordinaten" />
                                <h:inputText disabled="true" id="xfield" 
                                             value="#{mainController.currentXCoordinate}"/>
                                <h:inputText disabled="true" id="yfield" 
                                             value="#{mainController.currentYCoordinate}"/>
    
    
                                <h:outputLabel for="descriptionPosition" 
                                               value="Abfrage: PositionDescription" />
                                <p:inputTextarea id="descriptionPosition" 
                                                 autoResize="true" styleClass="textinput" 
                                                 required="false" counter="display1" 
                                                 maxlength="1000" 
                                                 value="#{mainController.currentPositionDescription}" 
                                                 counterTemplate="{0} Zeichen übrig"/>
                                <h:outputText id="display1" styleClass="display"/>
                                <h:outputText value="#{mainController.currentPositionDescription}" 
                                              rendered="false"/>
    
                                <h:outputLabel for="reason" value="Abfrage: Reason" />
                                <p:inputTextarea id="reason" autoResize="true" styleClass="textinput" 
                                                 required="false" counter="display2" maxlength="250" 
                                                 value="#{mainController.currentReason}" 
                                                 counterTemplate="{0} Zeichen übrig"/>
                                <h:outputText id="display2" styleClass="display"/>
                                <h:outputText value="#{mainController.currentReason}" 
                                              rendered="false"/>
    
    
                                <h:outputLabel for="ratingImportance" 
                                               value="Abfrage: Rating Importance" />
                                <p:selectOneMenu id="ratingImportance" 
                                                 value="#{mainController.currentRatingImportance}" 
                                                 required="false">
                                    <f:selectItem itemLabel="Rating Importance" itemValue="0" />
                                    <f:selectItem itemLabel="Sehr wichtig" itemValue="4" />
                                    <f:selectItem itemLabel="Wichtig" itemValue="3" />
                                    <f:selectItem itemLabel="Eher unwichtig" itemValue="2" />
                                    <f:selectItem itemLabel="Unwichtig" itemValue="1" />
                                </p:selectOneMenu>
                                <h:outputText value="#{mainController.currentRatingImportance}" 
                                              rendered="true"/>
    
                                <h:outputLabel for="numberOfChargingPoints" 
                                               value="Abfrage: NumberOfChargingPoints" />
                                <p:selectOneMenu id="numberOfChargingPoints" 
                                                 value="#{mainController.currentNumberOfChargingPoints}" 
                                                 required="false">
                                    <f:selectItem itemLabel="1" itemValue="1" />
                                    <f:selectItem itemLabel="2" itemValue="2" />
                                    <f:selectItem itemLabel="3" itemValue="3" />
                                    <f:selectItem itemLabel="4" itemValue="4" />
                                </p:selectOneMenu>
                                <h:outputText value="#{mainController.currentNumberOfChargingPoints}" 
                                              rendered="true"/>
    
                                <h:outputLabel for="chargingStandard" 
                                               value="Abfrage: Charging Standard" />
                                <p:selectOneMenu id="chargingStandard" 
                                                 value="#{mainController.currentChargingStandard}" 
                                                 required="false">
                                    <f:selectItem itemLabel="ChaDeMo" itemValue="ChaDeMo" />
                                    <f:selectItem itemLabel="CCS" itemValue="CCS" />
                                    <f:selectItem itemLabel="Typ3" itemValue="Typ3" />
                                    <f:selectItem itemLabel="Typ2" itemValue="Typ2" />
                                    <f:selectItem itemLabel="HPC" itemValue="HPC" />
                                    <f:selectItem itemLabel="keine Angabe" itemValue="ka" />
                                </p:selectOneMenu>
                                <h:outputText value="" rendered="true"/>
    
                                <h:outputLabel for="rating1" value="Abfrage: Rating 1" />
                                <p:inputTextarea id="rating1" styleClass="textinput" 
                                                 required="false" counter="display3" 
                                                 maxlength="250" value="#{mainController.currentRating1}" 
                                                 counterTemplate="Noch {0} Zeichen"/>
                                <h:outputText id="display3" styleClass="display"/>
                                <h:outputText value="#{mainController.currentRating1}" 
                                              rendered="false"/>
    
                                <h:outputLabel for="rating2" value="Abfrage: Rating 2" />
                                <p:inputTextarea id="rating2" styleClass="textinput" 
                                                 required="false" counter="display4" 
                                                 maxlength="250" value="#{mainController.currentRating2}" 
                                                 counterTemplate="Noch {0} Zeichen"/>
                                <h:outputText id="display4" styleClass="display"/>
                                <h:outputText value="#{mainController.currentRating2}" 
                                              rendered="false"/>
    
                                <h:outputLabel for="rating3" value="Abfrage: Rating 3" />
                                <p:inputTextarea id="rating3" styleClass="textinput" 
                                                 required="false" counter="display5" 
                                                 maxlength="250" value="#{mainController.currentRating3}" 
                                                 counterTemplate="Noch {0} Zeichen"/>
                                <h:outputText id="display5" styleClass="display"/>
                                <h:outputText value="#{mainController.currentRating3}" rendered="false"/>
    
                                <!-->This commandbutton does not execute the methodExpression<-->
                                <p:commandButton value="Submit" update="@form" icon="ui-icon-check" 
                                                 rendered="true" id="button1" 
                                                 actionListener="#{mainController.saveData}"/>
                                <p:commandButton type="reset" value="Cancel" 
                                                 onclick="return cancel()"/>
                            </h:panelGrid>
                            <h:inputHidden value="#{mainController.currentId}" />
    
                        </h:form>
    
                    </p:dialog >   
                </p:layoutUnit>
    
            </p:layout>
        </h:body>
    </html>
    

    managedbeam

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package com.sample.view;
    
    import java.io.Serializable;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ViewScoped;
    import javax.faces.event.ActionEvent;
    
    /**
     *
     * @author Wittakarn
     */
    @ViewScoped
    @ManagedBean(name = "mainController")
    public class MainController implements Serializable {
        private String testparam;
        private String currentXCoordinate;
        private String currentYCoordinate;
        private String currentPositionDescription;
        private String currentReason;
        private String currentRatingImportance;
        private String currentNumberOfChargingPoints;
        private String currentChargingStandard;
        private String currentRating1;
        private String currentRating2;
        private String currentRating3;
        private String currentId;
    
        public MainController(){}
    
        public String getTestparam() {
            return testparam;
        }
    
        public void setTestparam(String testparam) {
            this.testparam = testparam;
        }
    
        public String getCurrentXCoordinate() {
            return currentXCoordinate;
        }
    
        public void setCurrentXCoordinate(String currentXCoordinate) {
            this.currentXCoordinate = currentXCoordinate;
        }
    
        public String getCurrentYCoordinate() {
            return currentYCoordinate;
        }
    
        public void setCurrentYCoordinate(String currentYCoordinate) {
            this.currentYCoordinate = currentYCoordinate;
        }
    
        public String getCurrentPositionDescription() {
            return currentPositionDescription;
        }
    
        public void setCurrentPositionDescription(String currentPositionDescription) {
            this.currentPositionDescription = currentPositionDescription;
        }
    
        public String getCurrentReason() {
            return currentReason;
        }
    
        public void setCurrentReason(String currentReason) {
            this.currentReason = currentReason;
        }
    
        public String getCurrentRatingImportance() {
            return currentRatingImportance;
        }
    
        public void setCurrentRatingImportance(String currentRatingImportance) {
            this.currentRatingImportance = currentRatingImportance;
        }
    
        public String getCurrentNumberOfChargingPoints() {
            return currentNumberOfChargingPoints;
        }
    
        public void setCurrentNumberOfChargingPoints(String currentNumberOfChargingPoints) {
            this.currentNumberOfChargingPoints = currentNumberOfChargingPoints;
        }
    
        public String getCurrentChargingStandard() {
            return currentChargingStandard;
        }
    
        public void setCurrentChargingStandard(String currentChargingStandard) {
            this.currentChargingStandard = currentChargingStandard;
        }
    
        public String getCurrentRating1() {
            return currentRating1;
        }
    
        public void setCurrentRating1(String currentRating1) {
            this.currentRating1 = currentRating1;
        }
    
        public String getCurrentRating2() {
            return currentRating2;
        }
    
        public void setCurrentRating2(String currentRating2) {
            this.currentRating2 = currentRating2;
        }
    
        public String getCurrentRating3() {
            return currentRating3;
        }
    
        public void setCurrentRating3(String currentRating3) {
            this.currentRating3 = currentRating3;
        }
    
        public String getCurrentId() {
            return currentId;
        }
    
        public void setCurrentId(String currentId) {
            this.currentId = currentId;
        }
    
        public void saveData(ActionEvent event){
            System.out.println("saveData");
        }
    }