会话属性问题 Vaadin 24

huangapple go评论144阅读模式
英文:

Session Attribute issue Vaadin 24

问题

我有困难理解VaadinSessions和UI的工作原理。

我有一个类,它接收SampleClient的id和其他信息,并将其用作URL:

  1. @Route(value = "URL-of-page/:sampleClientID?", layout = MainLayout.class)
  2. @RolesAllowed("USER")
  3. public class DocumentazioneEntita extends Div implements BeforeEnterObserver {
  4. @Override
  5. public void beforeEnter(BeforeEnterEvent event) {
  6. this.client = (SampleClient) VaadinSession.getCurrent().getAttribute("client");
  7. System.out.println("client inside override " + client);
  8. if (this.client == null) {
  9. Notification.show("No client selected", 3000, Notification.Position.BOTTOM_START);
  10. navigateToMain(event);
  11. return;
  12. }
  13. this.idClienteString = (String) VaadinSession.getCurrent().getAttribute("idCliente");
  14. System.out.println("ID CLIENT STRING: " + idClienteString);
  15. Optional<Long> sampleClientId = event.getRouteParameters().get(SAMPLECLIENT_ID).map(Long::parseLong);
  16. if (sampleClientId.isPresent()) {
  17. Optional<SampleClient> sampleClientFromBackend = sampleClientService.get(sampleClientId.get());
  18. if (sampleClientFromBackend.isPresent()) {
  19. populateForm(sampleClientFromBackend.get());
  20. } else {
  21. Notification.show(
  22. String.format("Client not found, ID = %d", sampleClientId.get()),
  23. 3000, Notification.Position.BOTTOM_START);
  24. navigateToMain(event);
  25. return;
  26. }
  27. refreshGridData();
  28. }
  29. }
  30. private Long idCliente = (Long) VaadinSession.getCurrent().getAttribute("IdCliente");
  31. // 其他代码...
  32. }

为了更好地理解情况,我开始在控制台打印我拥有的数据。这是我的终端输出:

  1. ID CLIENTE: 1
  2. ID CLIENTE STRING: sotto autowired null
  3. 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]
  4. ID CLIENTE STRING: 37

所以在@Override内部,我有我的数据,而在外部则没有。为什么会这样?我希望能自由地在我的页面中使用SampleClient的数据,但我无法弄清楚为什么它只停留在beforeEnter类中。

值得注意的是,在前一个页面中,我通过Vaadin UI获得了所有必要的信息:

  1. private void navigateToEntityDocumentation(SampleClient client) {
  2. System.out.println("go to: " + client);
  3. if (client != null) {
  4. Long idCliente = client.getIdCliente();
  5. if (idCliente != null) {
  6. String idClienteString = idCliente.toString();
  7. UI.getCurrent().navigate("documentazione-entita/" + idClienteString);
  8. UI.getCurrent().getSession().setAttribute("idCliente", idClienteString);
  9. UI.getCurrent().getSession().setAttribute("client", client);
  10. } else {
  11. // Log that idCliente is null
  12. System.out.println("Cliente not found " + client);
  13. UI.getCurrent().navigate("#");
  14. }
  15. } else {
  16. // Log that client is null
  17. System.out.println("Client does not exists");
  18. UI.getCurrent().navigate("#");
  19. }
  20. }

这是我的System.out.println("go to: " + client);在终端输出的内容,证明我在页面之间有数据流动。

  1. 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:

  1. @Route(value = &quot;URL-of-page/:sampleClientID?&quot;, layout = MainLayout.class)
  2. @RolesAllowed(&quot;USER&quot;)
  3. public class DocumentazioneEntita extends Div implements BeforeEnterObserver {
  4. @Override
  5. public void beforeEnter(BeforeEnterEvent event) {
  6. this.client = (SampleClient) VaadinSession.getCurrent().getAttribute(&quot;client&quot;);
  7. System.out.println(&quot;client inside override &quot; + client);
  8. if (this.client == null) {
  9. Notification.show(&quot;No client selected&quot;, 3000, Notification.Position.BOTTOM_START);
  10. navigateToMain(event);
  11. return;
  12. }
  13. this.idClienteString = (String) VaadinSession.getCurrent().getAttribute(&quot;idCliente&quot;);
  14. System.out.println(&quot;ID CLIENT STRING: &quot; + idClienteString);
  15. Optional&lt;Long&gt; sampleClientId = event.getRouteParameters().get(SAMPLECLIENT_ID).map(Long::parseLong);
  16. if (sampleClientId.isPresent()) {
  17. Optional&lt;SampleClient&gt; sampleClientFromBackend = sampleClientService.get(sampleClientId.get());
  18. if (sampleClientFromBackend.isPresent()) {
  19. populateForm(sampleClientFromBackend.get());
  20. } else {
  21. Notification.show(
  22. String.format(&quot;Client not found, ID = %d&quot;, sampleClientId.get()),
  23. 3000, Notification.Position.BOTTOM_START);
  24. navigateToMain(event);
  25. return;
  26. }
  27. refreshGridData();
  28. }
  29. }
  30. private Long idCliente = (Long) VaadinSession.getCurrent().getAttribute(&quot;IdCliente&quot;);
  31. private SampleCoursesService sampleCoursesService;
  32. private SampleContactsService sampleContactsService;
  33. private SampleClientService sampleClientService;
  34. private SampleContacts contact;
  35. private SampleClient client;
  36. private String SAMPLECLIENT_ID = &quot;sampleClientID&quot;;
  37. private Checkbox showArchivedCheckbox; // Define showArchivedCheckbox as a field
  38. private Grid&lt;SampleContacts&gt; dispositiviTable = new Grid&lt;&gt;(SampleContacts.class);; // Define dispositiviTable as a field
  39. private List&lt;SampleContacts&gt; contactsList; // Define contactsList as a field
  40. private Binder&lt;SampleClient&gt; binder = new Binder&lt;&gt;(SampleClient.class);
  41. private GenerateReport generateReport;
  42. public String idClienteString;
  43. public DocumentazioneEntita(SampleCoursesService sampleCoursesService,
  44. SampleContactsService sampleContactsService,
  45. SampleClientService sampleClientService, GenerateReport generateReport) {
  46. this.sampleCoursesService = sampleCoursesService;
  47. this.sampleContactsService = sampleContactsService;
  48. this.sampleClientService = sampleClientService;
  49. this.generateReport = generateReport;
  50. setId(&quot;documentazione-entita-view&quot;);
  51. setSizeFull();
  52. System.out.println(&quot;ID CLIENTE: &quot; + idCliente);
  53. System.out.println(&quot;ID CLIENTE STRING: sotto autowired &quot; + idClienteString);

To better understand the situation i started debugging printing in the console the data i have.
Tthis is the output of my terminal:

  1. ID CLIENTE: 1
  2. ID CLIENTE STRING: sotto autowired null
  3. 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]
  4. ID CLIENTE STRING: 37

So inside the @Overridei 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:

  1. private void navigateToEntityDocumentation(SampleClient client) {
  2. System.out.println(&quot;go to: &quot; + client);
  3. if (client != null) {
  4. Long idCliente = client.getIdCliente();
  5. if (idCliente != null) {
  6. String idClienteString = idCliente.toString();
  7. UI.getCurrent().navigate(&quot;documentazione-entita/&quot; + idClienteString);
  8. UI.getCurrent().getSession().setAttribute(&quot;idCliente&quot;, idClienteString);
  9. UI.getCurrent().getSession().setAttribute(&quot;client&quot;, client);
  10. } else {
  11. // Log that idCliente is null
  12. System.out.println(&quot;Cliente not found &quot; + client);
  13. UI.getCurrent().navigate(&quot;#&quot;);
  14. }
  15. } else {
  16. // Log that client is null
  17. System.out.println(&quot;Client does not exists&quot;);
  18. UI.getCurrent().navigate(&quot;#&quot;);
  19. }
  20. }

This is what my System.out.println(&quot;go to: &quot; + client); outputs in the terminal, as a proof that i have a data flow from page to page.

  1. 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无关。

相反,问题在于代码的一部分使用&quot;IdCliente&quot;作为会话属性名称,而代码的另一部分使用&quot;idCliente&quot;。建议对于像这样的魔术字符串值使用常量,以避免这类错误。

英文:

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 &quot;IdCliente&quot; as the session attribute name and another part of the code uses &quot;idCliente&quot;. It's recommended to use a constant for magic string values like this to avoid these types of mistakes.

huangapple
  • 本文由 发表于 2023年8月8日 21:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定