'null Converter'的转换错误设置值'52'

lv1*_*v10 8 xhtml jsf converter java-ee backing-beans

我是JSF的新手,我一直在尝试存储使用ah:selectOneMenu来获取产品类别的表单中的数据.h:selectOneMenu正在从DB填充,但是,当尝试在DB中存储产品时,我收到一个错误:转换错误设置值'52'表示'null Converter'.我已经在线查看了StackOverflow和教程中的类似问题,但我仍然收到错误.

这是xhtml:

<h:selectOneMenu id="category_fk" value="#{productController.product.category_fk}" 
                                 converter="#{categoryConverter}" title="Category_fk" >
                    <!-- DONE: update below reference to list of available items-->
                    <f:selectItems value="#{productController.categoryList}" var="prodCat"
                                   itemValue="#{prodCat}" itemLabel="#{prodCat.name}"/>
                </h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)

这是产品控制器:

@Named
@RequestScoped
public class ProductController {

    @EJB
    private ProductEJB productEjb;
    @EJB
    private CategoryEJB categoryEjb;

    private Product product = new Product();
    private List<Product> productList = new ArrayList<Product>();

    private Category category;
    private List<Category> categoryList = new ArrayList<Category>();

    public String doCreateProduct()
    {
        product = productEjb.createProduct(product);
        productList = productEjb.findAllProducts();
        return "listProduct";
    }

    @PostConstruct
    public void init()
    {
        categoryList = categoryEjb.findAllCategory();
        productList = productEjb.findAllProducts();
    }        

    // Getters/Setters and other methods omitted for simplicity
Run Code Online (Sandbox Code Playgroud)

为简单起见,这是简化的EJB:

   @Stateless
    public class ProductEJB{

        @PersistenceContext(unitName = "luavipuPU")
        private EntityManager em;

        public List<Product> findAllProducts()
        {
            TypedQuery<Product> query = em.createNamedQuery("findAllProducts", Product.class);
            return query.getResultList();
        }

        public Product createProduct(Product product)
        {
            em.persist(product);
            return product;
        }    

    }
Run Code Online (Sandbox Code Playgroud)

为简单起见,这是简化的产品实体:

@Entity
@NamedQueries({
    @NamedQuery(name="findAllProducts", query = "SELECT p from Product p")
})
public class Product implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id @GeneratedValue(strategy= GenerationType.AUTO)
    private int product_id;
    private String name;
    private String description;
    protected byte[] imageFile;
    private Float price;
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateAdded;
    @ManyToOne    
    private Category category_fk;
    @ManyToOne
    private SaleDetails saleDetails_fk;
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的更新转换器:

@ManagedBean
@FacesConverter(value="categoryConverter")
public class CategoryConverter implements Converter{

    @PersistenceContext
    private transient EntityManager em;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return em.find(Category.class, new Integer(value));
    }

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

        Category category;
        category = (Category) value;
        return String.valueOf(category.getCategory_id());

    }

}
Run Code Online (Sandbox Code Playgroud)

代码已经从Orignal问题更新,代码现在完美有效.

kol*_*sus 11

如果不创建JSF 转换器,则无法在<h:selectOneMenu/>组件(或任何组件)上使用自定义类型.JSF中存在转换器,以帮助将基本上自定义或服务器端项目/构造转换为网页友好/人类可读格式,并且还能够将选择从客户端保存回服务器端.那么错误消息本质上告诉你的是,从客户端提交的值作为对象/类型将其保存到服务器端是不够的.<h:selectXXXX/>52Category

所以你需要做的是创建一个JSF的实现,converter它实质上帮助JSF理解你的Category对象.

以下是您需要做的粗略估计:

  1. 为您的Category类型实现转换器:

    // You must annotate the converter as a managed bean, if you want to inject
    // anything into it, like your persistence unit for example.
    @ManagedBean(name = "categoryConverterBean") 
    @FacesConverter(value = "categoryConverter")
    public class CategoryConverter implements Converter {
    
        @PersistenceContext(unitName = "luavipuPU")
        // I include this because you will need to 
        // lookup  your entities based on submitted values
        private transient EntityManager em;  
    
        @Override
        public Object getAsObject(FacesContext ctx, UIComponent component,
                String value) {
          // This will return the actual object representation
          // of your Category using the value (in your case 52) 
          // returned from the client side
          return em.find(Category.class, new BigInteger(value)); 
        }
    
        @Override
        public String getAsString(FacesContext fc, UIComponent uic, Object o) {
            //This will return view-friendly output for the dropdown menu
            return ((Category) o).getId().toString(); 
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 参考您的新转换器 <h:selectOneMenu/>

    <h:selectOneMenu id="category_fk" 
      converter="#{categoryConverterBean}"
      value="#productController.product.category_fk}" 
      title="Category_fk" >
    <!-- DONE: update below reference to list of available items-->
        <f:selectItems value="#{productController.categoryList}" 
          var="prodCat" itemValue="#{prodCat.category_id}" 
          itemLabel="#{prodCat.name}"/>
    </h:selectOneMenu>
    
    Run Code Online (Sandbox Code Playgroud)

我们正在使用该名称,categoryConverterBean因为我们希望利用您在转换器中提取的实体管理器技巧.在任何其他情况下,您将使用您在转换器注释上设置的名称.