英文:
Session Attribute issue Vaadin 24
问题
我有困难理解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");
// 其他代码...
}
为了更好地理解情况,我开始在控制台打印我拥有的数据。这是我的终端输出:
ID CLIENTE: 1
ID CLIENTE STRING: sotto autowired null
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA SampleClient [capCliente=null, cfCliente=XYZ789, descrizioneCliente=null, emailCliente=bellali@it.it, 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
内部,我有我的数据,而在外部则没有。为什么会这样?我希望能自由地在我的页面中使用SampleClient的数据,但我无法弄清楚为什么它只停留在beforeEnter类中。
值得注意的是,在前一个页面中,我通过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, emailCliente=bellali@it.it, 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]
英文:
I have trouble understading how VaadinSessions and UI works.
I have my class that takes the id and other information of SampleClient and use it as a 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);
To better understand the situation i started debugging printing in the console the data i have.
Tthis is the output of my terminal:
ID CLIENTE: 1
ID CLIENTE STRING: sotto autowired null
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA SampleClient [capCliente=null, cfCliente=XYZ789, descrizioneCliente=null, emailCliente=bellali@it.it, 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
So inside the @Override
i have my data outside of it i do not. why is the reason for that?
I wanted to be able to use the data of my SampleClient in my page freely but i can't figure it out why it just stays inside the before enter class.
Is worth noticing that in the previous page i got all the necessary information through 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("#");
}
}
This is what my System.out.println("go to: " + client);
outputs in the terminal, as a proof that i have a data flow from page to page.
go to: SampleClient [capCliente=null, cfCliente=XYZ789, descrizioneCliente=null, emailCliente=bellali@it.it, 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]
答案1
得分: 2
正如评论中指出的那样,这里的问题与VaadinSession
无关。
相反,问题在于代码的一部分使用"IdCliente"
作为会话属性名称,而代码的另一部分使用"idCliente"
。建议对于像这样的魔术字符串值使用常量,以避免这类错误。
英文:
As pointed out in a comment, the problem here has nothing to do with VaadinSession
.
Instead, the problem is that one part of the code uses "IdCliente"
as the session attribute name and another part of the code uses "idCliente"
. It's recommended to use a constant for magic string values like this to avoid these types of mistakes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论