Binder.BindingBuilder<Livre,Stock> 类型中的 withConverter(Converter<Stock,NEWTARGET>) 方法不适用于参数

问题描述

Binder.BindingBuilder 类型中的 withConverter(Converter) 方法不适用于参数 (LivreForm.MyConverter)。由于在 vaadin crm 教程应用程序中实现了 vaading MyConverter 类,这个错误一直“困扰”我。这个想法是它会接受 Stock::getQuantiteStock 提供的 Integer 值并吐出一个字符串。我对它为什么会出现以及如何避免它感到困惑。我欢迎任何建议,并添加我的完整表单类供您检查:

package com.vaadin.tutorial.crm.UI.views.list;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.ComponentEventListener;
import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.EmailField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.Result;
import com.vaadin.flow.data.binder.ValidationException;
import com.vaadin.flow.data.binder.ValueContext;
import com.vaadin.flow.shared.Registration;
import com.vaadin.tutorial.crm.backend.entity.Livre;
import com.vaadin.tutorial.crm.backend.entity.Livre.Categorie;
import com.vaadin.tutorial.crm.backend.entity.Stock;
import com.vaadin.flow.data.converter.Converter;
import com.vaadin.flow.data.converter.StringToIntegerConverter;

import java.util.List;

public class LivreForm extends FormLayout{
    TextField titreLivre = new TextField("titreLivre");
    TextField description = new TextField("description");
    TextField auteur = new TextField("auteur");
    TextField refeni = new TextField("refeni");
    TextField isbn = new TextField("isbn");
    ComboBox<Livre.Categorie> categorie = new ComboBox<>("Categorie");
    ComboBox<Livre.Status> status = new ComboBox<>("Status");
    ComboBox<Stock> stock = new ComboBox<>("Stock");
    ComboBox<Livre.Campus> campus = new ComboBox<>("Campus");
    
    Button save = new Button("Save"); 
    Button delete = new Button("Delete");
    Button close = new Button("Cancel");
    Binder<Livre> binder = new BeanValidationBinder<>(Livre.class);
    private Livre livre;
    
    public LivreForm(List<Stock> stocks) {
        addClassName("livre-form");
        binder.forField(stock)
          .withConverter(new MyConverter())
          .bind(Stock::getQuantiteStock,Stock::setQuantiteStock);
        binder.bindInstanceFields(this);
        stock.setItems(stocks);
        status.setItems(Livre.Status.values());
        categorie.setItems(Livre.Categorie.values());
        add(titreLivre,description,auteur,refeni,isbn,categorie,status,campus,stock,createButtonsLayout()); 
      }
    
    public void setLivre(Livre livre) {
        this.livre = livre; 
        binder.readBean(livre); 
    }
    
    
    
    
    private Component createButtonsLayout() {
        save.addThemeVariants(ButtonVariant.LUMO_PRIMARY); 
        delete.addThemeVariants(ButtonVariant.LUMO_ERROR);
        close.addThemeVariants(ButtonVariant.LUMO_TERTIARY);

        save.addClickShortcut(Key.ENTER); 
        close.addClickShortcut(Key.ESCAPE);
        
        save.addClickListener(event -> validateAndSave()); 
        delete.addClickListener(event -> fireEvent(new DeleteEvent(this,livre))); 
        close.addClickListener(event -> fireEvent(new CloseEvent(this))); 
        
        binder.addStatusChangeListener(e -> save.setEnabled(binder.isValid()));

        return new HorizontalLayout(save,delete,close); 
      }
    
    private void validateAndSave() {
          try {
            binder.writeBean(livre); 
            fireEvent(new SaveEvent(this,livre)); 
          } catch (ValidationException e) {
            e.printStackTrace();
          }
        }
    
    public static abstract class LivreFormEvent extends ComponentEvent<LivreForm> {
          

        /**
         * 
         */
        private static final long serialVersionUID = -7236023661050023675L;
        private Livre livre;

          protected LivreFormEvent(LivreForm source,Livre livre) { 
            super(source,false);
            this.livre = livre;
          }

          public Livre getLivre() {
            return livre;
          }
        }

        public static class SaveEvent extends LivreFormEvent {
          SaveEvent(LivreForm source,Livre livre) {
            super(source,livre);
          }
        }

        public static class DeleteEvent extends LivreFormEvent {
          DeleteEvent(LivreForm source,livre);
          }

        }

        public static class CloseEvent extends LivreFormEvent {
          CloseEvent(LivreForm source) {
            super(source,null);
          }
        }

        public <T extends ComponentEvent<?>> Registration addListener(Class<T> eventType,ComponentEventListener<T> listener) { 
          return getEventBus().addListener(eventType,listener);
        }
        
        class MyConverter implements Converter<String,Integer> {
              @Override
              public Result<Integer> convertToModel(String fieldValue,ValueContext context) {
                // Produces a converted value or an error
                try {
                  // ok is a static helper method that creates a Result
                  return Result.ok(Integer.valueOf(fieldValue));
                } catch (NumberFormatException e) {
                  // error is a static helper method that creates a Result
                  return Result.error("Please enter a number");
                }
              }

              @Override
              public String convertToPresentation(Integer integer,ValueContext context) {
                // Converting to the field type should always succeed,// so there is no support for returning an error Result.
                return String.valueOf(integer);
              }
            }
    

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...