如何在EJB中验证时如何在jsf中显示多错误消息? [英] How to show multi error message in jsf while validation is in EJB?

查看:114
本文介绍了如何在EJB中验证时如何在jsf中显示多错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个问题的答案,我明白如果有是一个错误,EJB将抛出一个异常,它将在支持bean中被捕获,并且支持bean将基于异常捕获向用户显示错误消息。



我的问题是如果theres多于一个错误怎么办?如何向用户显示多个错误消息,而EJB每次只能抛出一个异常?



例如,在注册表中,用户需要输入电子邮件地址,名称,密码和重新密码,不能为空。如果所有数据都有效,但给定的电子邮件地址已经存在,则EJB将抛出EntityExistException,并将通知用户该电子邮件地址已被注册。如果密码多重错误和重新密码不匹配,名称为空,该怎么办?而且我想向用户显示这两个错误。 EJB会抛出什么异常?注意:验证必须在EJB中

解决方案

您不应该在支持bean操作方法中进行验证,而是在正常的验证器中。



例如

 < h:inputText value =#{register.email}required =true validator =#{emailValidator}/> 
< h:inputSecret binding =#{password}value =#{register.password}required =true/>
< h:inputSecret required =truevalidator =confirmPasswordValidator>
< f:attribute name =passwordvalue =#{password.value}/>
< / h:inputSecret>
...

#{emailValidator} 是这样的:

  @MangedBean 
public class EmailValidator implements Validator {

@EJB
private UserService userService;

@Override
public void validate(FacesContext context,UIComponent component,Object value)throws ValidatorException {
if(value == null){
return; //让required =true句柄。
}

if(userService.existsEmail((String)value)){
throw new ValidatorException(Messages.createError(Email already exists));
}
}

}

请注意EJB不应该在这里抛出异常。只有当有一个致命和不可恢复的错误(如DB down或错误的表/列定义)时才应该这样做。



而$ code confirmPasswordValidator 是这样的一个

  @FacesValidator(confirmPasswordValidator)
public class ConfirmPasswordValidator implements Validator {

@Override
public void validate(FacesContext context,UIComponent component,Object value)throws ValidatorException {
Object password = component.getAttributes()。get(password);

if(value == null || password == null){
return; //让required =true句柄。
}

if(!password.equals(value)){
throw new ValidatorException(Messages.createError(Password does not match));
}
}

}


Based on the answer from this question, i understand that if there is an error, EJB will throw an exception which will be catch in the backing bean and backing bean will show user an error message based on exception its catch.

My question is, what if theres more than one error? How can i show multiple error message to the user, while the EJB can only throw one exception at a time?

For example, at registration form user will need to input email address, name, password, and re-password, and must not be null. If all the data is valid but the given email address is already exist, EJB will throw EntityExistException and the user will be notified that email address is already registered. What if theres multiple error like password and re-password not match and the name is empty? And i want to show these two error to the user. What exception should EJB throw? What approach i can take to achieve this?

Note: the validation must be in EJB

解决方案

You shouldn't be doing validation in a backing bean action method, but in a normal Validator.

E.g.

<h:inputText value="#{register.email}" required="true" validator="#{emailValidator}" />
<h:inputSecret binding="#{password}" value="#{register.password}" required="true" />
<h:inputSecret required="true" validator="confirmPasswordValidator">
    <f:attribute name="password" value="#{password.value}" />
</h:inputSecret>
...

with the #{emailValidator} being something like this:

@MangedBean
public class EmailValidator implements Validator {

    @EJB
    private UserService userService;

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value == null) {
            return; // Let required="true" handle.
        }

        if (userService.existsEmail((String) value)) {
            throw new ValidatorException(Messages.createError("Email already exists"));
        }
    }

}

Note that the EJB shouldn't throw an exception here. It should only do that when there's a fatal and unrecoverable error such as DB down or wrong table/column definitions.

And the confirmPasswordValidator being something like this

@FacesValidator("confirmPasswordValidator")
public class ConfirmPasswordValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Object password = component.getAttributes().get("password");

        if (value == null || password == null) {
            return; // Let required="true" handle.
        }

        if (!password.equals(value)) {
            throw new ValidatorException(Messages.createError("Password do not match"));
        }
    }

}

这篇关于如何在EJB中验证时如何在jsf中显示多错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆