gpt4 book ai didi

java - 在 Vaadin 表中设置字符串到 LocalDate 转换器

转载 作者:行者123 更新时间:2023-11-30 07:18:09 25 4
gpt4 key购买 nike

继“学习 Vaadin 7,第二版”一书之后,我现在尝试在表格中显示简单的 beans。然而,本书只介绍了旧的 java.util.Date 类的用法。我正在尝试使用转换器显示 LocalDate 属性。

我试图显示的bean(人):

public class Person {

private long id;
private String firstName;
private String lastName;
private LocalDate birthdate;
private Gender gender;

// .. GETTERS & SETTERS

我编写了一个 LocalDateToStringConverter,实现了 com.vaadin.data.util.converter.Converter。

package be.kapture.converters;

import com.vaadin.data.util.converter.Converter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;

public class LocalDateToStringConverter implements Converter<String, LocalDate> {

@Override
public LocalDate convertToModel(String value, Class<? extends LocalDate> targetType, Locale locale) throws ConversionException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
try {
return LocalDate.parse(value, formatter);

} catch (DateTimeParseException ex) {
return null;
}
}

@Override
public String convertToPresentation(LocalDate value, Class<? extends String> targetType, Locale locale) throws ConversionException {
return value.toString();
}

@Override
public Class<LocalDate> getModelType() {
return LocalDate.class;
}

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

在用户界面中,这是我设置转换器的代码片段:

...
Table table = new Table("", container);
table.setConverter("birthdate", new LocalDateToStringConverter());

verticalLayout.addComponent(table);

'container' 是一个 BeanItemContainer,我在其中放置了一些示例 Person 对象。在浏览器中访问 Vaadin 应用程序时,出现以下异常:

jun 27, 2016 1:56:45 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [HelloVaadinServlet] in context with path [] threw exception [com.vaadin.server.ServiceException: com.vaadin.ui.Table$CacheUpdateException: Error during Table cache update. Additional causes not shown.] with root cause
com.vaadin.data.util.converter.Converter$ConversionException: Unable to convert value of type java.time.LocalDate 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:116)
at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:736)
at com.vaadin.ui.AbstractField.convertFromModel(AbstractField.java:721)
at com.vaadin.ui.AbstractField.setPropertyDataSource(AbstractField.java:657)
at com.vaadin.ui.Table.bindPropertyToField(Table.java:4140)
at com.vaadin.ui.Table.getPropertyValue(Table.java:4109)
at com.vaadin.ui.Table.parseItemIdToCells(Table.java:2386)
at com.vaadin.ui.Table.getVisibleCellsNoCache(Table.java:2225)
at com.vaadin.ui.Table.refreshRenderedCells(Table.java:1745)
at com.vaadin.ui.Table.refreshRowCache(Table.java:2691)
at com.vaadin.ui.Table.containerItemSetChange(Table.java:4587)
at com.vaadin.data.util.AbstractContainer.fireItemSetChange(AbstractContainer.java:242)
at com.vaadin.data.util.AbstractInMemoryContainer.fireItemsAdded(AbstractInMemoryContainer.java:1012)
at com.vaadin.data.util.AbstractInMemoryContainer.fireItemAdded(AbstractInMemoryContainer.java:994)
at com.vaadin.data.util.AbstractInMemoryContainer.internalAddItemAtEnd(AbstractInMemoryContainer.java:884)
at com.vaadin.data.util.AbstractBeanContainer.addItem(AbstractBeanContainer.java:533)
at com.vaadin.data.util.AbstractBeanContainer.addBean(AbstractBeanContainer.java:598)
at com.vaadin.data.util.BeanItemContainer.addItem(BeanItemContainer.java:227)
at be.kapture.MyUI.init(MyUI.java:88)
at com.vaadin.ui.UI.doInit(UI.java:682)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:214)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:74)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1409)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:364)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
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:141)
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:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
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)

为什么它不注册转换器来执行字符串到 LocalDate 的转换(如果需要,反之亦然)?我尝试使用匿名内部类作为 Table.setConverter 方法中的参数,得到相同的结果。我在这里做错了什么?

更新(根据要求):

这是使用表、容器和转换器的完整 UI 代码。注意:这是一个“HelloVaadin”沙箱项目,没有实际目标。它是专门为了像本期这样的目的而设置的,试图将 Java 8 的 LocalDate 集成到 Vaadin 项目中。

package be.kapture;

import be.kapture.converters.LocalDateToDateConverter;
import be.kapture.converters.LocalDateToStringConverter;
import be.kapture.entities.Person;
import be.kapture.util.CustomFieldGroupFieldFactory;
import com.vaadin.annotations.*;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;

import javax.servlet.annotation.WebServlet;
import java.time.LocalDate;
import java.util.Arrays;

import static com.vaadin.data.Property.ValueChangeListener;


@Theme("mytheme")
@Widgetset("be.kapture.MyAppWidgetset")
@PreserveOnRefresh
@Title("Hello Vaadin!")

public class MyUI extends UI implements Window.CloseListener {

private static final Person person1 = new Person(1L, "John", "DOE", LocalDate.of(70, 1, 1));
private static final Person person2 = new Person(2L, "Jane", "doe", LocalDate.of(70, 1, 1));
private static final Person person3 = new Person(3L, "jules", "winnf", LocalDate.of(48, 11, 21));
private static final Person person4 = new Person(4L, "vincent", "Vega", LocalDate.of(54, 2, 17));

private static final BeanItemContainer<Person> container = new BeanItemContainer<>(Person.class);

static {
container.addAll(Arrays.asList(person1, person2, person3, person4));
}

private final VerticalLayout verticalLayout = new VerticalLayout();

@Override
protected void init(VaadinRequest vaadinRequest) {

Person person = new Person(1L);
person.setFirstName("John");
person.setLastName("Doe");
person.setBirthdate(LocalDate.now());

BeanItem<Person> beanItem = new BeanItem<>(person);

FieldGroup group = new FieldGroup(beanItem);
group.setFieldFactory(new CustomFieldGroupFieldFactory());

Field<?> id = group.buildAndBind("id");
Field<?> firstName = group.buildAndBind("firstName");
Field<?> lastName = group.buildAndBind("lastName");

Field<?> birthdate = group.buildAndBind("birthdate");

Field<?> gender = group.buildAndBind("gender");

// birthdate.setConverter(new LocalDateToDateConverter());
// birthdate.setPropertyDataSource(item.getItemProperty("birthdate"));
// FormLayout layout = new FormLayout(id, firstName, lastName,
// birthdate);
// layout.setMargin(true);
// setContent(layout);

verticalLayout.setMargin(true);
verticalLayout.setSpacing(true);

verticalLayout.addComponents(id, firstName, lastName, birthdate, gender);

// Define a person which cannot exist
Person nullPerson = new Person(-1L);
nullPerson.setFirstName("Test");
container.addItem(nullPerson);
final ListSelect select = new ListSelect("", container);

// Send events on directly when clicked
select.setImmediate(true);

// Handle the value of the person as null
select.setNullSelectionItemId(nullPerson);
select.setItemCaptionPropertyId("firstName");
select.addValueChangeListener((ValueChangeListener) event -> System.out.println(select.getValue()));

verticalLayout.addComponent(select);

Table table = new Table("");
table.setEditable(true);
table.setConverter(LocalDateToDateConverter.class);
table.setContainerDataSource(container);

verticalLayout.addComponent(table);

setContent( verticalLayout);
}

@Override
public void windowClose(Window.CloseEvent e) {
Notification.show("Window closed.");
}

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}

最佳答案

我很好奇并找出了你的问题。方法 setConverter(Object, Converter) 仅允许更改该列中的值向用户显示的方式(文本表示)。这就是为什么第二个参数的类型是 Converter<java.lang.String,?> .

在您的示例中,您启用了表中的编辑。这需要 Vaadin 知道它将如何为您的 LocalDate 提供表格单元格编辑器。柱子。默认情况下,该表不知道 LocalDate 。我知道您有两个选择:

  1. 实现converter factory返回 Converter对于 LocalDateString 。然后,表格组件能够显示一个文本字段,您可以在其中根据转换器中的格式输入日期。我没有尝试当用户输入无效字符串时会发生什么。
  2. 实现table field factory并调用setTableFieldFactory在表组件上。现场工厂将显示 PopupDateField或类似的东西。这样您就可以实现类型安全并可以使用内置日期字段。

在我看来,后者会带来更好的用户体验,但当然需要付出更多的开发努力。

关于java - 在 Vaadin 表中设置字符串到 LocalDate 转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38053540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com