会话属性问题 Vaadin 24

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

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 = &quot;URL-of-page/:sampleClientID?&quot;, layout = MainLayout.class)
@RolesAllowed(&quot;USER&quot;)
public class DocumentazioneEntita extends Div implements BeforeEnterObserver {
@Override
public void beforeEnter(BeforeEnterEvent event) {
this.client = (SampleClient) VaadinSession.getCurrent().getAttribute(&quot;client&quot;);
System.out.println(&quot;client inside override &quot; + client);
if (this.client == null) {
Notification.show(&quot;No client selected&quot;, 3000, Notification.Position.BOTTOM_START);
navigateToMain(event);
return;
}
this.idClienteString = (String) VaadinSession.getCurrent().getAttribute(&quot;idCliente&quot;);
System.out.println(&quot;ID CLIENT STRING: &quot; + idClienteString);
Optional&lt;Long&gt; sampleClientId = event.getRouteParameters().get(SAMPLECLIENT_ID).map(Long::parseLong);
if (sampleClientId.isPresent()) {
Optional&lt;SampleClient&gt; sampleClientFromBackend = sampleClientService.get(sampleClientId.get());
if (sampleClientFromBackend.isPresent()) {
populateForm(sampleClientFromBackend.get());
} else {
Notification.show(
String.format(&quot;Client not found, ID = %d&quot;, sampleClientId.get()),
3000, Notification.Position.BOTTOM_START);
navigateToMain(event);
return;
}
refreshGridData();
} 
}
private Long idCliente = (Long) VaadinSession.getCurrent().getAttribute(&quot;IdCliente&quot;);
private SampleCoursesService sampleCoursesService;
private SampleContactsService sampleContactsService;
private SampleClientService sampleClientService;
private SampleContacts contact;
private SampleClient client;
private String SAMPLECLIENT_ID = &quot;sampleClientID&quot;;
private Checkbox showArchivedCheckbox; // Define showArchivedCheckbox as a field
private Grid&lt;SampleContacts&gt; dispositiviTable = new Grid&lt;&gt;(SampleContacts.class);; // Define dispositiviTable as a field
private List&lt;SampleContacts&gt; contactsList; // Define contactsList as a field
private Binder&lt;SampleClient&gt; binder = new Binder&lt;&gt;(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(&quot;documentazione-entita-view&quot;);
setSizeFull();
System.out.println(&quot;ID CLIENTE: &quot; + idCliente); 
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:

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 @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:

   private void navigateToEntityDocumentation(SampleClient client) {
System.out.println(&quot;go to: &quot; + client);
if (client != null) {
Long idCliente = client.getIdCliente();
if (idCliente != null) {
String idClienteString = idCliente.toString();
UI.getCurrent().navigate(&quot;documentazione-entita/&quot; + idClienteString);
UI.getCurrent().getSession().setAttribute(&quot;idCliente&quot;, idClienteString);
UI.getCurrent().getSession().setAttribute(&quot;client&quot;, client); 
} else {
// Log that idCliente is null
System.out.println(&quot;Cliente not found &quot; + client);
UI.getCurrent().navigate(&quot;#&quot;);
}
} else {
// Log that client is null
System.out.println(&quot;Client does not exists&quot;);
UI.getCurrent().navigate(&quot;#&quot;);
}
}

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.

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:

确定