我是JSF的新手,并且需要通过一个JSF应用程序。设置字段在jsf验证后为空
我想验证一个空白字符串的密码字段。
我知道,而不是在Javascript中做它,我打电话验证功能。
我的需要是,如果密码字段的值是空的,那么它将从验证器函数验证到现在它正确地运行,但是在验证之后,当它到达时= UI bav = ck那时密码字段应该是空的。
如果密码字段为空(仅包含空格),那么我希望它将其设置为空白或保持原样。
我尝试到现在为止, JSF视图页面singin.xhtml
<h:outputText styleClass="outputBox" style="margin: 3px;"
value="Password" />
<h:inputSecret id="password" required="true"
requiredMessage="Please enter your password"
value="#{userActionManager.dto.password}"
validator="#{userActionManager.validateSamePassword}">
<a4j:ajax event="change" execute="@this" bypassUpdates="true" />
</h:inputSecret>
验证方法。
public void validateSamePassword(FacesContext fc, UIComponent component, Object obj) {
System.out.println("UserActionManager.validateSamePassword()");
String confirmPassword = (String)obj;
System.out.println("Password is :" + confirmPassword);
if(confirmPassword!=null && confirmPassword.trim().equals("")) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password cannot be blank", " detail Passwords do not match!");
// System.out.println(component.getFamily());
// String field1Id = (String) component.getAttributes().get("password");
// Find the actual JSF component for the client ID.
// UIInput textInput = (UIInput) fc.getViewRoot().findComponent("password");
// textInput.setValue("");
dto.setPassword(null);
throw new ValidatorException(message);
}else{
dto.setPassword(confirmPassword);
}
}
我试过选项dto.setPassword(null);但是当它返回查看时,密码字段中的空格仍然存在,我必须手动删除。
我在想什么?
component.resetValue
(); UIComponent上没有这种方法。我尝试像((UIInput)组件).resetValue(),我发现在UIInput中有resetValue。仍然没有成功。空白值仍然存在于Field中,我想说清楚。 – Jayesh 2013-03-17 07:54:08
@JayeshPatel,其实这是有道理的。我的错。从技术上讲,只有抛出'ValidationException'时,验证才会失败。我提出的当前解决方案是过早地重置值。查看我的更新以获取更适合的时机。 – kolossus 2013-03-17 08:34:17
'resetValue()'未在'UIComponent'中定义。它在'EditableValueHolder'接口中定义,它由其他'UIInput'实现。 – BalusC 2013-03-17 15:07:41