行动listerner没有开火

时间:2017-02-17 07:56:57

标签: jsf primefaces jsf-2

我在项目中使用Primefaces Ultima template。我遇到了试图在页面上触发动作监听器的问题。

我使用ultima template.xhtml作为我的应用程序的模板。

的template.xhtml

<!DOCTYPE html>
<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 http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="theme-color" content="#{guestPreferences.themeColors[guestPreferences.theme]}" />
    </f:facet>
    <title><ui:insert name="title"/></title>
    <h:outputScript name="js/nanoscroller.js" library="ultima-layout" />
    <h:outputScript name="js/layout.js" library="ultima-layout" />
    <h:outputScript name="js/ripple.js" library="ultima-layout" />
    <h:outputScript name="js/swipe.js" library="ultima-layout" />
    <h:outputStylesheet library="css" name="pfcrud.css"/>
    <h:outputScript library="scripts" name="pfcrud.js"/>
    <ui:insert name="head"/>
</h:head>

<h:body styleClass="main-body #{guestPreferences.compact ? 'layout-compact' : null}">

    <p:growl id="growl" life="3000"/>

    <div class="layout-wrapper #{guestPreferences.menuLayout} #{guestPreferences.orientationRTL ? 'layout-rtl' : null}">
        <ui:include src="./topbar.xhtml" />
        <ui:include src="./menu.xhtml" />

        <div class="layout-main">
            <ui:insert name="content"/>

            <ui:include src="./footer.xhtml" />
        </div>
    </div>

    <p:ajaxStatus style="width:32px;height:32px;position:fixed;right:7px;bottom:7px">
       <f:facet name="start">
           <i class="fa fa-circle-o-notch fa-spin ajax-loader" aria-hidden="true"></i>
       </f:facet>

       <f:facet name="complete">
           <h:outputText value="" />
       </f:facet>
    </p:ajaxStatus>

    <h:outputStylesheet name="css/nanoscroller.css" library="ultima-layout" />
    <h:outputStylesheet name="css/animate.css" library="ultima-layout" />
    <h:outputStylesheet name="css/ripple.css" library="ultima-layout" />
    <h:outputStylesheet name="css/my_layout.css" library="ultima-layout" />
</h:body>

我遇到问题的页面有以下代码。

profile.xhtml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition 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"
            template="/WEB-INF/template.xhtml">

<ui:define name="title">
    Profile
</ui:define>

<ui:define name="content">
    <div class="ui-g ui-fluid">
        <div class="ui-g-12 ui-lg-6">
            <!-- Left Side -->
            <h:form id="personalForm">
                <div class="card card-w-title">                    
                    <h1>Personal Info</h1>
                    <h:panelGroup id="personalInfo" rendered="#{loginController.currentUser != null}">
                        <p:panelGrid columns="2" layout="grid" styleClass="ui-panelgrid-blank form-group">
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="accessCode" value="#{loginController.tempUser.accessCode}" title="#{messages.CreateUserTitle_firstName}" required="true"
                                             requiredMessage="#{messages.CreateUserRequiredMessage_firstName}" maxlength="50" disabled="true"/>
                                <label>Access Code</label>
                            </h:panelGroup>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="firstName" value="#{loginController.tempUser.firstName}" title="#{messages.CreateUserTitle_firstName}" maxlength="50"/>
                                <label for="firstName">First Name</label>
                            </h:panelGroup>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="lastName" value="#{loginController.tempUser.lastName}" title="#{messages.CreateUserTitle_lastName}" maxlength="50"/>
                                <label for="lastName">Last Name</label>
                            </h:panelGroup>
                            <p:selectOneMenu id="country" value="#{loginController.tempUser.country}" required="true" 
                                             requiredMessage="#{messages.CreateUserRequiredMessage_country}" converter="countryConverter" style="width: 100%">
                                <f:selectItem itemLabel="#{messages.CreateUserLabel_country}"/>
                                <f:selectItems value="#{countryController.items}"
                                               var="countryItem"
                                               itemValue="#{countryItem}"
                                               itemLabel="#{countryItem.id.toString()}"
                                               />
                            </p:selectOneMenu>
                            <p:selectOneMenu id="nationality" value="#{loginController.tempUser.nationality}" required="true"
                                             requiredMessage="#{messages.CreateUserRequiredMessage_nationality}" converter="countryConverter" style="width: 100%">
                                <f:selectItem itemLabel="#{messages.CreateUserLabel_nationality}"/>
                                <f:selectItems value="#{countryController.items}"
                                               var="nationalityItem"
                                               itemValue="#{nationalityItem}"
                                               itemLabel="#{nationalityItem.id.toString()}"
                                               />
                            </p:selectOneMenu>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="city" value="#{loginController.tempUser.city}" title="#{messages.CreateUserTitle_city}" required="true" 
                                             requiredMessage="#{messages.CreateUserRequiredMessage_city}" size="50" maxlength="50" style="width: 100%"/>
                                <label for="city">City</label>
                            </h:panelGroup>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="email" value="#{loginController.tempUser.email}" title="#{messages.CreateUserTitle_email}" required="true" requiredMessage="#{messages.CreateUserRequiredMessage_email}" size="50" maxlength="50"/>
                                <label for="email">Email</label>
                            </h:panelGroup>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="telephone1" value="#{loginController.tempUser.telephone1}" title="#{messages.CreateUserTitle_telephone1}" 
                                             required="true" requiredMessage="#{messages.CreateUserRequiredMessage_telephone1}" size="20" maxlength="20" style="width: 100%"/>
                                <label for="telephone1">Telephone 1</label>
                            </h:panelGroup>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:inputText id="telephone2" value="#{loginController.tempUser.telephone2}" title="#{messages.CreateUserTitle_telephone2}"
                                             size="20" maxlength="20" style="width: 100%"/>
                                <label for="telephone2">Telephone 2</label>
                            </h:panelGroup>

                        </p:panelGrid>
                        <p:commandButton actionListener="#{loginController.updateProfile(event)}" value="Update" update="personalForm:personalInfo, :growl" icon="fa fa-paper-plane-o" >
                            <p:confirm header="#{messages.ConfirmationHeader}" message="#{messages.ConfirmCreateMessage}"/>
                        </p:commandButton>
                    </h:panelGroup>
                </div>
            </h:form>
        </div>
        <div class="ui-g-12 ui-lg-6">
            <!-- Right Side -->
            <h:form id="passwordForm">
                <div class="card card-w-title">
                    <h1>Change Password</h1>
                    <h:panelGroup id="passwordInfo" rendered="#{loginController.currentUser != null}">
                        <p:panelGrid columns="2" layout="grid" styleClass="ui-panelgrid-blank form-group">
                            <h:panelGroup styleClass="md-inputfield">
                                <p:password id="password" required="true" requiredMessage="Please fill out the new password" 
                                            value="#{loginController.password}" match="confirmPassword"/>
                                <label for="password">New Password</label>
                            </h:panelGroup>
                            <h:panelGroup styleClass="md-inputfield">
                                <p:password id="confirmPassword" value="#{loginController.password}" required="true" 
                                            requiredMessage="Please repeate the new password"/>
                                <label for="confirmPassword">Confirm New Password</label>
                            </h:panelGroup>

                        </p:panelGrid>
                            <p:commandButton actionListener="#{loginController.updatePassword(event)}" value="Update" update="passwordForm:passwordInfo, :growl" icon="fa fa-paper-plane-o" >
                               <p:confirm header="#{messages.ConfirmationHeader}" message="#{messages.ConfirmCreateMessage}"/>
                           </p:commandButton>   
                       </h:panelGroup>
                   </div>
               </h:form>
           </div>
       </div>
   </ui:define>
</ui:composition>

具有动作侦听器的会话bean具有以下代码:

LoginController.java

@ManagedBean(name = "loginController")
@SessionScoped
public class LoginController implements Serializable {

private static final long serialVersionUID = 1L;

private User currentUser = new User();
private User tempUser = new User();
private User oldUserValue = new User();
private String accessCode;
private String password;
private String telephone;
private String email;
private String userName;

@Inject
RoleFacade roleFacade;

private enum UsersActions {
    SIGN_UP,
    CHANGE_PWD,
    UPDATE
}

@Inject
private UserFacade userFacade;

public String getAccessCode() {
    return accessCode;
}

public void setAccessCode(String accessCode) {
    this.accessCode = accessCode;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getTelephone() {
    return telephone;
}

public void setTelephone(String telephone) {
    this.telephone = telephone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public User getCurrentUser() {
    return currentUser;
}

 public void setCurrentUser(User currentUser) {
    FacesContext.getCurrentInstance().
            getExternalContext().getSessionMap().put("currentUser", currentUser);
    this.currentUser = currentUser;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public User getOldUserValue() {
    return oldUserValue;
  }

  public void setOldUserValue(User oldUserValue) {
    this.oldUserValue = oldUserValue;
  }

  public User getTempUser() {
    return tempUser;
  }

  public void setTempUser(User tempUser) {
    this.tempUser = tempUser;
  }

  public String authenticate() throws NoSuchAlgorithmException {
    User user = userFacade.findByAccessCode(accessCode);
    if (user != null && user.getPassword().equals(MyStringUtil.toSHA256(password))) {
        this.setCurrentUser(user);
        StringBuilder strBuilder = new StringBuilder();
        String firstName = getNonNullValue(this.getCurrentUser().getFirstName());
        String lastName = getNonNullValue(this.getCurrentUser().getLastName());

        strBuilder.append(firstName);
        strBuilder.append((lastName.isEmpty() ? "" : " " + lastName));
        this.setUserName(strBuilder.toString());
        return "/dashboard";
    }
    return "#";
  }

  public String signOut() {
    this.setCurrentUser(null);
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.invalidateSession();
    return "/login";
  }

  private String getNonNullValue(String value) {
    return (value == null || value.isEmpty() ? "" : value);
  }

  public void forgotPassword(ActionEvent event) {
    if (accessCode != null && email != null && telephone != null) {
        User user = userFacade.findByEmailAndTelephone(accessCode, email, telephone);
        if (user != null) {

            try {
                String clearPwd = MyStringUtil.generatePwd();
                User user2 = (User) (new PersistenceCloner(user)).generateCopyToPersist();

                user.setPassword(MyStringUtil.toSHA256(clearPwd));
                // outgoing message information
                String mailTo = user.getEmail();
                String subject = "My Way Investracker Password updated";

                // message contains HTML markups
                String message = "<i>Hello Investor!</i><br><br>";
                message += "<p><b>You new password is:</b><br>";
                message += "<i></i> <b><h2><font color=red>" + clearPwd + "</font></h2></b>";
                message += "<i>Please kindly change it upon next login.</i><br><br></p>";
                message += "<font color=red>My Way Sarl</font>";

                System.out.println("User 1 = " + user);
                System.out.println("User 2 = " + user2);

                try {
                    this.persist(user, user2, UsersActions.CHANGE_PWD,
                            "A mail has been sent to you with the new password.");
                    MailUtil.sendHtmlEmail(mailTo, subject, message);
                } catch (Exception ex) {
                    JsfUtil.addErrorMessage("Sorry there was an error handling this, please try again!");
                    Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (NoSuchAlgorithmException ex) {
                JsfUtil.addErrorMessage("Sorry there was an error handling this, please try again!");
                Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            JsfUtil.addErrorMessage("Sorry the email and telephone number doesn't match!");
        }
    }
  }

  public void signUp(ActionEvent event) throws NoSuchAlgorithmException {
    String access = MyStringUtil.generateAccessCode();
    Role role = new Role();
    role.setId("NONE");
    role.setName("NONE");
    this.tempUser.setIdRole(role);
    this.tempUser.setAccessCode(access);
    this.tempUser.setPassword(MyStringUtil.toSHA256(this.tempUser.getPassword()));
    this.tempUser.setActivityStatus(false);
    persist(this.tempUser, this.tempUser, LoginController.UsersActions.SIGN_UP, "You have successfully registered, check your email for your access code.");
  }

  public void updateProfile(ActionEvent event) {
    System.out.println("UPDATE PROFILE LAUNCHED.");
    System.out.println("UPDATE PROFILE COMPLETED.");
  }

  public void updatePassword(ActionEvent event) {
    System.out.println("UPDATE PASSWORD LAUNCHED");
    System.out.println("UPDATE PASSWORD COMPLETED.");
  }

  private void persist(User newValue, User oldValue,   LoginController.UsersActions actionName, String successMessage) {
     System.out.println("PERSISTENCE METHOD ALREADY RAN.");
  }     
}

到目前为止,我已就此问题进行了一些研究,并且遇到了与@BalusC提出的answer类似的问题,但我无法找到问题。我需要你的帮助!

1 个答案:

答案 0 :(得分:2)

您正在将p:confirm与p:commandButton结合使用,但未定义全局p:confirmDialog。

您有两种选择:

  1. 从p:commandButton中删除p:confirm子项。
  2. 向您的页面添加全局p:confirmDialog(请参阅PrimeFaces-Showcase中的p:confirmDialog示例)。然后在按下确认对话框中的“是”按钮时调用actionListener。