只需实现一个Validator自己.
@ManagedBean
@RequestScoped
public class UserNameAvailableValidator implements Validator {
@EJB
private UserService userService;
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
String userName = (String) value;
if (!userService.isUsernameAvailable(userName)) {
throw new ValidatorException(new FacesMessage("Username not avaliable"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
(请注意,它是一个@ManagedBean而不是@FacesValidator因为需要注入@EJB;如果你不使用EJB,你可以@FacesValidator改为使用它)
使用方法如下:
<h:inputText id="username" value="#{register.user.name}" required="true">
<f:validator binding="#{userNameAvailableValidator}" />
<f:ajax event="blur" render="username_message" />
</h:inputText>
<h:message id="username_message" for="username" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1180 次 |
| 最近记录: |