繁体   English   中英

突出 <p:inputText> 使用PrimeFaces验证

[英]Highlight <p:inputText> using PrimeFaces validation

我试图根据某些条件突出显示<p:inputText> ,但最终我什么也没得到。 这是我的xhtml代码:

<p:inputText value="#{loginTo.userName}" id="username" required="true" label="username" maxlength="20"  requiredMessage="#{appLoginParameter['AppLoginNameRequiredMsg']}">
</p:inputText>

这是我的Java代码:

ResourceBundle appLoginBundle = ResourceBundle.getBundle("app/AppLogin");
        FacesContext facesContext = FacesContext.getCurrentInstance();
        UIComponent uiComponent = UIComponent.getCurrentComponent(facesContext);
        String message = "";
        try{
            message = appLoginBundle.getString(userLoginDetailTO.getLoginRemarks());
        }catch(Exception exp){
            message =appLoginBundle.getString("UnknownException");
        }
        FacesMessage facesMessage = new FacesMessage(message);
        facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
        facesContext.addMessage("", facesMessage);
        UIInput username= (UIInput) uiComponent.getAttributes().get("username");
        username.setValid(false);

但是我得到了空指针异常。

尝试这个:

public UIComponent findComponent(final String id){
    FacesContext context = FacesContext.getCurrentInstance(); 
    UIViewRoot root = context.getViewRoot();
    final UIComponent[] found = new UIComponent[1];
    root.visitTree(new FullVisitContext(context), new VisitCallback() {     
        @Override
        public VisitResult visit(VisitContext context, UIComponent component) {
            if(component.getId().equals(id)){
                found[0] = component;
                return VisitResult.COMPLETE;
            }
            return VisitResult.ACCEPT;              
        }
    });
    return found[0];

}

此代码将仅在树中找到具有您传递的ID的第一个组件。 如果树中有2个具有相同名称的组件(如果它们在2个不同的命名容器下,则可能会发生),您将必须执行一些自定义操作,或者您还可以在表单中设置prependId="false"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM