如何验证自定义转换器?

发布于 2024-12-13 16:01:00 字数 1723 浏览 3 评论 0原文

我有一个自定义转换器,它是这样的:

@ManagedBean
@RequestScoped
public class MeasureConverter implements Converter {

    @EJB
    private EaoMeasure eaoMeasure;

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (!(value instanceof Measure) || ((Measure) value).getId() == null) {
            return null;
        }

        return String.valueOf(((Measure) value).getId());
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (value == null || !value.matches("\\d+")) {
            return null;
        }

        Measure measure = eaoMeasure.find(Integer.valueOf(value));

        if (measure == null) {
            throw new ConverterException(new FacesMessage("Unknown Measure ID: " + value));
        }

        return measure;
    }

但我想验证这一点,就像我对其他字段所做的那样,就像这样:

<h:outputLabel for="name" value="Name:" />
<h:inputText id="name" value="#{bean.name}">
<f:ajax event="blur" listener="#{validator.name}" render="m_name" />
</h:inputText>
<rich:message id="m_name" for="name" ajaxRendered="false" />

因此,当 blur 事件发生时,会出现一个图标,指示是否选择是否正确(上面的代码就是我所说的)

但我想在同一个自定义转换器中应用这种验证,只是为了通过我的项目对各种输入或排序保持标准验证的。

我已经尝试在 RequestScope 中使用 @ManagaProperty 但没有成功:

@ManagedProperty("#{param.measure}")
private String measure;

因此,当发生模糊事件时,它会调用我的 validate bean,该 bean 具有requestScope 正如 BalusC 所说这里,但它带来的是一个空字符串。

有什么想法吗?

I have a custom converter which is this :

@ManagedBean
@RequestScoped
public class MeasureConverter implements Converter {

    @EJB
    private EaoMeasure eaoMeasure;

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (!(value instanceof Measure) || ((Measure) value).getId() == null) {
            return null;
        }

        return String.valueOf(((Measure) value).getId());
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {

        if (value == null || !value.matches("\\d+")) {
            return null;
        }

        Measure measure = eaoMeasure.find(Integer.valueOf(value));

        if (measure == null) {
            throw new ConverterException(new FacesMessage("Unknown Measure ID: " + value));
        }

        return measure;
    }

But I would like to validate this as I'm doing with the other fields, just like this for example :

<h:outputLabel for="name" value="Name:" />
<h:inputText id="name" value="#{bean.name}">
<f:ajax event="blur" listener="#{validator.name}" render="m_name" />
</h:inputText>
<rich:message id="m_name" for="name" ajaxRendered="false" />

So when the blur event occurs an icon appears indicating if the selection in correct or not (the code above do this what I'm talking )

But I want to apply this kind of validation in this same custom converter, just to keep the standard validation through my project to the all kinds of inputs or sorts of.

I already try to use a @ManagaProperty in a RequestScope but without success:

@ManagedProperty("#{param.measure}")
private String measure;

So when the blur events occurs it calls my validate bean which has the requestScope as BalusC said here, but what it comes is a empty string.

Any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2024-12-20 16:01:00

您应该实现 Validator< /a>.

例如,

@FacesValidator("measureValidator")
public class MeasureValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Measure measure = (Measure) value;

        // ...

        if (!valid) { 
            throw new ValidatorException(new FacesMessage("Not valid"));
        }
    }

}

您可以通过输入组件的 validator 属性

<h:inputText validator="measureValidator">

或通过 标签(是的,您可以有多个;它们将在顺序将它们附加到组件)

<h:inputText>
    <f:validator validatorId="measureValidator" />
</h:inputText>

转换器与此处无关。它已经完成了将 String 转换为 Measure 的工作。验证器将仅检索Measure。您在那里的 @ManagedProperty 也不会以这种方式工作。它基本上会使用 request.getParameter("measure") 的结果设置属性,我不认为您的表单提交了这样的参数。

You should implement a Validator.

E.g.

@FacesValidator("measureValidator")
public class MeasureValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        Measure measure = (Measure) value;

        // ...

        if (!valid) { 
            throw new ValidatorException(new FacesMessage("Not valid"));
        }
    }

}

You can declare it by the input component's validator attribute

<h:inputText validator="measureValidator">

or by the <f:validator> tag (yes, you can have multiple of them; they will be invoked in the order they are been attached to the component)

<h:inputText>
    <f:validator validatorId="measureValidator" />
</h:inputText>

The converter is not relevant here. It has already done its job of converting String to Measure. The validator will just retrieve the Measure. The @ManagedProperty which you've there will also not work that way. It will basically set the property with result of request.getParameter("measure") and I don't think that there's such a parameter submitted by your form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文