我无法理解 VaadinSessions 和 UI 的工作原理。
我的类获取 SampleClient 的 id 和其他信息并将其用作 URL:
@Route(value = "URL-of-page/:sampleClientID?", layout = MainLayout.class)
@RolesAllowed("USER")
public class DocumentazioneEntita extends Div implements BeforeEnterObserver {
@Override
public void beforeEnter(BeforeEnterEvent event) {
this.client = (SampleClient) VaadinSession.getCurrent().getAttribute("client");
System.out.println("client inside override " + client);
if (this.client == null) {
Notification.show("No client selected", 3000, Notification.Position.BOTTOM_START);
navigateToMain(event);
return;
}
this.idClienteString = (String) VaadinSession.getCurrent().getAttribute("idCliente");
System.out.println("ID CLIENT STRING: " + idClienteString);
Optional<Long> sampleClientId = event.getRouteParameters().get(SAMPLECLIENT_ID).map(Long::parseLong);
if (sampleClientId.isPresent()) {
Optional<SampleClient> sampleClientFromBackend = sampleClientService.get(sampleClientId.get());
if (sampleClientFromBackend.isPresent()) {
populateForm(sampleClientFromBackend.get());
} else {
Notification.show(
String.format("Client not found, ID = %d", sampleClientId.get()),
3000, Notification.Position.BOTTOM_START);
navigateToMain(event);
return;
}
refreshGridData();
}
}
private Long idCliente = (Long) VaadinSession.getCurrent().getAttribute("IdCliente");
private SampleCoursesService sampleCoursesService;
private SampleContactsService sampleContactsService;
private SampleClientService sampleClientService;
private SampleContacts contact;
private SampleClient client;
private String SAMPLECLIENT_ID = "sampleClientID";
private Checkbox showArchivedCheckbox; // Define showArchivedCheckbox as a field
private Grid<SampleContacts> dispositiviTable = new Grid<>(SampleContacts.class);; // Define dispositiviTable as a field
private List<SampleContacts> contactsList; // Define contactsList as a field
private Binder<SampleClient> binder = new Binder<>(SampleClient.class);
private GenerateReport generateReport;
public String idClienteString;
public DocumentazioneEntita(SampleCoursesService sampleCoursesService,
SampleContactsService sampleContactsService,
SampleClientService sampleClientService, GenerateReport generateReport) {
this.sampleCoursesService = sampleCoursesService;
this.sampleContactsService = sampleContactsService;
this.sampleClientService = sampleClientService;
this.generateReport = generateReport;
setId("documentazione-entita-view");
setSizeFull();
System.out.println("ID CLIENTE: " + idCliente);
System.out.println("ID CLIENTE STRING: sotto autowired " + idClienteString);
为了更好地了解情况,我开始调试在控制台中打印我拥有的数据。 这是我终端的输出:
ID CLIENTE: 1
ID CLIENTE STRING: sotto autowired null
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA SampleClient [capCliente=null, cfCliente=XYZ789, descrizioneCliente=null, [email protected], idCliente=37, indirizzoCliente=789 Main St, insolvente=false, localitaCliente=New York, nomeCliente=John Smith, noteCliente=null, provinciaCliente=null, pivaCliente=1.23456789E8, sdiCliente=SDI123, telefonoCliente=1234567890]
ID CLIENTE STRING: 37
所以在
@Override
里面我有我的数据,在外面我没有。这是为什么呢?值得注意的是,在上一页中我通过 Vaadin UI 获取了所有必要的信息:
private void navigateToEntityDocumentation(SampleClient client) {
System.out.println("go to: " + client);
if (client != null) {
Long idCliente = client.getIdCliente();
if (idCliente != null) {
String idClienteString = idCliente.toString();
UI.getCurrent().navigate("documentazione-entita/" + idClienteString);
UI.getCurrent().getSession().setAttribute("idCliente", idClienteString);
UI.getCurrent().getSession().setAttribute("client", client);
} else {
// Log that idCliente is null
System.out.println("Cliente not found " + client);
UI.getCurrent().navigate("#");
}
} else {
// Log that client is null
System.out.println("Client does not exists");
UI.getCurrent().navigate("#");
}
}
这就是我的
System.out.println("go to: " + client);
在终端中输出的内容,作为我拥有从一个页面到另一个页面的数据流的证明。
go to: SampleClient [capCliente=null, cfCliente=XYZ789, descrizioneCliente=null, [email protected], idCliente=37, indirizzoCliente=789 Main St, insolvente=false, localitaCliente=New York, nomeCliente=John Smith, noteCliente=null, provinciaCliente=null, pivaCliente=1.23456789E8, sdiCliente=SDI123, telefonoCliente=1234567890]
正如评论中指出的,这里的问题与
VaadinSession
无关。
相反,问题在于代码的一部分使用
"IdCliente"
作为会话属性名称,而代码的另一部分使用 "idCliente"
。建议对像这样的魔术字符串值使用常量,以避免此类错误。