Frage

Unable to show faces message in xhtml page - its showing in console. In forgotPassword link, like to check if user exist or not

<h:outputText value="Enter User Name" />
<h:inputText  value="#{loginBean.technicianName}" required="true" 
    requiredMessage="user name is required" id="unameId"  >
    <f:validator validatorId="com.beans.UserNameAvailableValidator" />
    <f:ajax event="blur" render="username_message" />
</h:inputText>
<rich:message for="unameId" id="username_message"/>

bean code:

@FacesValidator("com.beans.UserNameAvailableValidator")
@RequestScoped
public class UserNameAvailableValidator implements Validator {
  UserdetailsDAO userdetailsDAO = null;

  @Override
  public void validate(FacesContext fc, UIComponent uic, Object value) throws ValidatorException {
    String userName = (String) value;
    userdetailsDAO = new UserdetailsDAOImpl();
    try {
      if(userdetailsDAO.getUserdetails(userName)!= null) {
        System.out.println("user exist");
      } else {
        throw new ValidatorException(new FacesMessage("Username doesnot exist "));
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}
War es hilfreich?

Lösung

You are only creating the message, you also need to add put it somewhere (the message does not work the same way as exception), in this case you add it to the context:

FacesContext fc = FacesContext.getCurrentInstance();
UIComponent root = fc.getViewRoot();
UIComponent component = root.findComponent("unameId");
fc.addMessage(component.getClientId(fc), new FacesMessage("Username does not exist."));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top