0

I know this question is asked many times, but I have checked all suggested solutions and they didn't work for me. I am working with jsf and primefaces version 10.0. The action listener of my commandButton in the jsf file doesn't call the method in the bean. None of the system.out.println statements inside the method is executed. Even if inside the method there is just a system.out.println, the statement is not executed. This makes me think that the problem is in the jsf file, but everything looks right to me, although I'm a beginner in the front end side.

My code:

<div class="card crud-demo">
  <h:form id="form">
     <p:growl id="messages" showDetail="true"/>

     <p:outputPanel id="manage-customer-content" class="ui-fluid">
     <p:outputPanel rendered="#{not empty customerBean.selectedUser}">
        <div class="p-field">
           <p:outputLabel for="username">Username</p:outputLabel>
           <p:inputText id="username" value="#{customerBean.selectedUser.username}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="password">Password</p:outputLabel>
           <p:password id="password"  value="#{customerBean.selectedUser.password}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="repeatPassword">Repeat Password</p:outputLabel>
           <p:password id="repeatPassword" value="#{customerBean.samePassword}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="lastname">Last Name</p:outputLabel>
           <p:inputText id="lastname" value="#{customerBean.selectedUser.lastName}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="firstname">First Name</p:outputLabel>
           <p:inputText id="firstname" value="#{customerBean.selectedUser.firstName}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="email">Email</p:outputLabel>
           <p:inputText id="email" value="#{customerBean.selectedUser.email}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="phone">Phone</p:outputLabel>
           <p:inputText id="phone" value="#{customerBean.selectedUser.phone}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel  for="addressLine1">Address Line 1</p:outputLabel>
           <p:inputTextarea id="addressLine1" value="#{customerBean.selectedUser.addressLine1}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="addressLine2">Address Line 2</p:outputLabel>
           <p:inputTextarea id="addressLine2" value="#{customerBean.selectedUser.addressLine2}" required="false"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="city">City</p:outputLabel>
           <p:inputText id="city"  value="#{customerBean.selectedUser.city}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="postalCode">Postal Code</p:outputLabel>
           <p:inputNumber id="postalCode" value="#{customerBean.selectedUser.postalCode}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="country">Country</p:outputLabel>
           <p:inputText id="country" value="#{customerBean.selectedUser.country}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="securityQuestion">Security Question</p:outputLabel>
           <p:inputTextarea id="securityQuestion" value="#{customerBean.selectedUser.securityQuestion}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="answerOfQuestion">Answer of Question</p:outputLabel>
           <p:password id="answerOfQuestion" value="#{customerBean.selectedUser.answerOfQuestion}" required="true"/>
        </div>
        <div style="margin-top: 1rem" class="p-field">
           <p:outputLabel for="repeatAnswerOfQuestion">Repeat Answer</p:outputLabel>
           <p:password id="repeatAnswerOfQuestion" value="#{customerBean.sameAnswer}" required="true"/>
        </div>
     </p:outputPanel>
     </p:outputPanel>


       <p:commandButton style="margin-top: 1rem" value="Submit" actionListener="#{customerBean.onSave}"
        update="manage-customer-content" process="@this"/>
     
  </h:form>

My customerBean code


import com.example.MyWebshop.model.User;
import com.example.MyWebshop.service.UserService;
import com.example.MyWebshop.service.UserServiceImpl;
import org.primefaces.PrimeFaces;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


@ManagedBean
@ViewScoped
public class CustomerBean implements Serializable {
    private static final long serialVersionUID = 1L;

    public UserService userService = new UserServiceImpl();
    private User selectedUser;
    private String samePassword;
    private String sameAnswer;



    @PostConstruct
    public void init() {
        selectedUser = new User();
        //System.out.println("Customer is getting registered: " + selectedUser);

    }
    public void onSave() {
        System.out.println("I'm here");
        this.selectedUser = new User();
        if (this.selectedUser != null) {

            if (selectedUser.getPassword().equals(samePassword) && selectedUser.getAnswerOfQuestion().equals(sameAnswer)) {
                userService.addNewCustomer(selectedUser);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Customer Added"));

            } else if (selectedUser.getPassword() != samePassword)
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Passwords don't match"));

             else
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Answers don't match"));

        }
            else
            System.out.println("value null");

    }

Any possible ideas what can be wrong? Thank you in advance.

Edit: it looks the problem is with the initialization of selectedUser in the init method of the bean. If I remove the comment from system.out.println in the init method, so the selected user can be printed, I get a nullPointerException although I don't understand why. The selected user is initialized before the print and in user model there is the overriden toString() method which should print the selectedUser even with null values for all the fields.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
mfo12002
  • 41
  • 5

0 Answers0