如何验证自定义转换器?
我有一个自定义转换器,它是这样的:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该实现
Validator
< /a>.例如,
您可以通过输入组件的
validator
属性或通过
标签(是的,您可以有多个;它们将在顺序将它们附加到组件)转换器与此处无关。它已经完成了将
String
转换为Measure
的工作。验证器将仅检索Measure
。您在那里的@ManagedProperty
也不会以这种方式工作。它基本上会使用request.getParameter("measure")
的结果设置属性,我不认为您的表单提交了这样的参数。You should implement a
Validator
.E.g.
You can declare it by the input component's
validator
attributeor 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)The converter is not relevant here. It has already done its job of converting
String
toMeasure
. The validator will just retrieve theMeasure
. The@ManagedProperty
which you've there will also not work that way. It will basically set the property with result ofrequest.getParameter("measure")
and I don't think that there's such a parameter submitted by your form.