转换器已实现,但仍然出现错误“转换错误设置值”对于“空转换器” ”
我对 Converter 类有一个大问题。我正在使用 h:selectOneMenu 为我的 Enetity“产品”选择“类别”。当我提交表单时,我收到以下错误:
•“空转换器”的转换错误设置值“”。
我找不到我的实施有什么问题。 我也在寻找如何使用 Seam-Faces 来做到这一点...... 有什么想法吗? 谢谢。
我正在使用 JSF Mojarra 2.1.2 (FCS 20110613)、GlassFish v 3.1、PrimeFaces 2.x、PrettyFaces 3.x 和 JPA 2.0。
我的 JSF 页面:
<h:selectOneMenu id="selectCategory"
value="#{productController.category}">
<f:selectItems value="#{categoryController.listCategory()}" var="category" itemLabel="#{category.name}" itemValue="#{category}"/>
<f:converter converterId="categoryConverter" />
</h:selectOneMenu>
我的 Converter 类:
@FacesConverter(forClass=Category.class, value="categoryConverter")
public class CategoryConverter implements Converter {
private CategoryController ctrl;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
ctrl = (CategoryController) context.getApplication().getELResolver().getValue(
context.getELContext(), null, "categoryController");
Category category = ctrl.findById(Integer.valueOf(value));
return category;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return "" + ((Category) value).getCategoryid();
}
}
我的 ProductController 的某些部分班级 :
@ManagedBean(name = "productController")
@RequestScoped
ProductController class
public ProductController{
private Category category;
//getters :: setters
I have a big problem with Converter class. I am using h:selectOneMenu to select a "category" for my Enetity "Product". When i submit form, then i get following error :
•Conversion Error setting value '' for 'null Converter'.
I can not find what is wrong with my implementation.
Also i am searching how to do that working with Seam-Faces...
Any ideas?
Thanx.
I am using JSF Mojarra 2.1.2 (FCS 20110613), GlassFish v 3.1, PrimeFaces 2.x, PrettyFaces 3.x and JPA 2.0.
MY JSF page:
<h:selectOneMenu id="selectCategory"
value="#{productController.category}">
<f:selectItems value="#{categoryController.listCategory()}" var="category" itemLabel="#{category.name}" itemValue="#{category}"/>
<f:converter converterId="categoryConverter" />
</h:selectOneMenu>
MY Converter class :
@FacesConverter(forClass=Category.class, value="categoryConverter")
public class CategoryConverter implements Converter {
private CategoryController ctrl;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
ctrl = (CategoryController) context.getApplication().getELResolver().getValue(
context.getELContext(), null, "categoryController");
Category category = ctrl.findById(Integer.valueOf(value));
return category;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return "" + ((Category) value).getCategoryid();
}
}
some part of my ProductController Class :
@ManagedBean(name = "productController")
@RequestScoped
ProductController class
public ProductController{
private Category category;
//getters :: setters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢,但我已经用 system.outs 尝试过了。通过提交正确发送值,并返回给定值 (#ID) 的正确类别对象。但仍然有同样的错误。
无论如何....我已经发现问题了。转换器按预期工作正常。
导致错误的唯一原因是 field Product."image" 。我没有图像字段转换器。当我从 JSF 表单中删除这一行时,它的提交没有错误。
图像属性的类型是字节,因此它也需要在 JSF 中进行转换,例如其他非字符串字段。
我知道这只是一个初学者错误:)
StringToByteConverter:
这是一个示例
Thanks but I had tried it with system.outs. Values are send correctly by submit and returned right category object for the given value (#ID). but still having the same error.
Anyway.... I have found the problem. Converter works fine as expected.
Only thing that causes the error was that the field product."image" . I had no converter for image field. When i removed this line from JSF form, it is submitted without errors.
Type of image property is a byte and so it also needs to be converted in the JSF such as other fields that are not a String.
just a beginner error i know:)
StringToByteConverter:
here is an example