如何显示特定UI组件的验证器消息?

时间:2014-04-19 17:56:21

标签: jsf-2 richfaces

无法在xhtml页面中显示面部消息 - 它在控制台中显示。 在forgotPassword链接中,喜欢检查用户是否存在

<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代码:

@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();
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您只是创建消息,您还需要将其添加到某处(消息与异常的工作方式不同),在这种情况下,您将其添加到上下文中:

FacesContext fc = FacesContext.getCurrentInstance();
UIComponent root = fc.getViewRoot();
UIComponent component = root.findComponent("unameId");
fc.addMessage(component.getClientId(fc), new FacesMessage("Username does not exist."));