[英]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.