Vaadin:绑定一个 byte[] 字段

Vaadin: Bind a byte[] field

我有一个bean如下:

public class Step implements Serializable {

    private int stepSequence;
    private byte[] text;
    private byte[] expectedResult;
    private String notes;
...
    getters and setters
}

我正在尝试这样绑定:

BeanFieldGroup binder = new BeanFieldGroup(s.getClass());
        binder.setItemDataSource(s);
        Field<?> sequence = binder.buildAndBind("Sequence", "stepSequence");
        layout.addComponent(sequence);
//        Field<?> text = binder.buildAndBind("Text", "text");
//        layout.addComponent(text);
//        Field<?> result = binder.buildAndBind("Expected Result", "expectedResult");
//        layout.addComponent(result);
        Field<?> notes = binder.buildAndBind("Notes", "notes");
        layout.addComponent(notes);

补充字段出现以下错误:

com.vaadin.data.Buffered$SourceException
    at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:667)
    at com.vaadin.data.fieldgroup.FieldGroup.bind(FieldGroup.java:274)
    at com.vaadin.data.fieldgroup.BeanFieldGroup.bind(BeanFieldGroup.java:155)
    at com.vaadin.data.fieldgroup.FieldGroup.buildAndBind(FieldGroup.java:1207)
    at com.vaadin.data.fieldgroup.BeanFieldGroup.buildAndBind(BeanFieldGroup.java:162)
    at com.vaadin.data.fieldgroup.FieldGroup.buildAndBind(FieldGroup.java:1185)
    at net.sourceforge.javydreamercsw.validation.manager.web.ValidationManagerUI.displayStep(ValidationManagerUI.java:205)
    at net.sourceforge.javydreamercsw.validation.manager.web.ValidationManagerUI.displayStep(ValidationManagerUI.java:193)
    at net.sourceforge.javydreamercsw.validation.manager.web.ValidationManagerUI.lambda$buildProjectTreea09dcf(ValidationManagerUI.java:721)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:200)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:163)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1015)
    at com.vaadin.ui.AbstractField.fireValueChange(AbstractField.java:1164)
    at com.vaadin.ui.AbstractField.setValue(AbstractField.java:572)
    at com.vaadin.ui.AbstractSelect.setValue(AbstractSelect.java:739)
    at com.vaadin.ui.AbstractField.setValue(AbstractField.java:469)
    at com.vaadin.ui.AbstractSelect.changeVariables(AbstractSelect.java:549)
    at com.vaadin.ui.Tree.changeVariables(Tree.java:568)
    at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:608)
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:458)
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:408)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:273)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:90)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1422)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:379)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type [B to presentation type class java.lang.String. No converter is set and the types are not compatible.
    at com.vaadin.data.util.converter.ConverterUtil.convertFromModel(ConverterUtil.java:118)
    at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:738)
    at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:723)
    at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:659)
    ... 55 more

如何添加上述转换器?

还有其他方法可以实现吗?

我一直在四处寻找,并且能够让它发挥作用。以下是找到此问题的其他人的答案:

    TextField text = new TextField("Text");
    text.setConverter(new ByteToStringConverter());
    binder.bind(text, "text");
    layout.addComponent(text);
    TextField result = new TextField("Expected Result");
    result.setConverter(new ByteToStringConverter());
    binder.bind(result, "expectedResult");
    layout.addComponent(result);

和转换器:

private static class ByteToStringConverter implements Converter<String, byte[]> {

    @Override
    public byte[] convertToModel(String value,
            Class<? extends byte[]> targetType,
            Locale locale) throws ConversionException {
        try {
            return value.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Exceptions.printStackTrace(ex);
        }
        return null;
    }

    @Override
    public String convertToPresentation(byte[] value,
            Class<? extends String> targetType, Locale locale)
            throws ConversionException {
        return new String(value, StandardCharsets.UTF_8);
    }

    @Override
    public Class<byte[]> getModelType() {
        return byte[].class;
    }

    @Override
    public Class<String> getPresentationType() {
        return String.class;
    }
}