Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库中获取用户并将属性放入标签中_Java_Spring Boot_Vaadin_Spring Webflux - Fatal编程技术网

Java 从数据库中获取用户并将属性放入标签中

Java 从数据库中获取用户并将属性放入标签中,java,spring-boot,vaadin,spring-webflux,Java,Spring Boot,Vaadin,Spring Webflux,我有一个简单的Vaadin GUI,我想在localhost:8080: @Route("hello") public class EmployeeGui extends VerticalLayout { private final WebClient webClient = WebClient.create("http://localhost:8080"); public EmployeeGui() { TextField textEmployee = ne

我有一个简单的Vaadin GUI,我想在localhost:8080:

@Route("hello")
public class EmployeeGui extends VerticalLayout {

    private final WebClient webClient = WebClient.create("http://localhost:8080");

    public EmployeeGui() {
        TextField textEmployee = new TextField("Give id of user");
        Button buttonOK = new Button("OK");
        Label label = new Label();

        buttonOK.addClickListener(buttonClickEvent -> {
            this.webClient.get()
                    .uri(uriBuilder -> uriBuilder
                            .path("/employee/{id}")
                            .build(textEmployee.getValue()))
                            .retrieve()
                            .bodyToMono(EmployeeTo.class)
                            .subscribe(emp -> {
                                   label.setText(emp.getName());
                                });
                            });

        add(textEmployee,buttonOK, label);
    }
}
在本地主机上:8080运行我的后端应用程序,它给我RESTAPI从数据库中检索一些数据

在文本字段中,我们可以输入用户id,然后单击“确定”按钮。然后在标签中设置用户名。不幸的是,我遇到了异常(在
label.setText(emp.getName());
行中):

java.lang.IllegalStateException:如果不锁定会话,则无法访问VaadinSession或UI中的状态


我明白了,但我怎么能忽略这个问题呢?单击“确定”按钮后,如何放置用户Id并将用户属性返回到标签?

当您对API的请求做出反应时,来自Vaadin的“请求”是 已经做了。如果您想实现这一点,您必须启用
@Push
(这样Vaadin可以在发生更改时从服务器发送更改)并确保 以安全的方式访问UI(有关详细信息,请参阅 详情)

或者您实际上不需要异步和 以阻塞方式使用web客户机(因此客户机阻塞,标签更改
在按钮触发的“请求”内发生。

问题的直接答案是在主请求已被响应时使用
@Push
(,)更新ui(因为您的
webClient.get()
是异步的)。问题的解决方法如下所示:

@Push
@Route("hello")
public class EmployeeGui extends VerticalLayout {

    private UI ui;
    private final WebClient webClient = WebClient.create("http://localhost:8080");

    public EmployeeGui() {
        TextField textEmployee = new TextField("Give id of user");
        Button buttonOK = new Button("OK");
        Label label = new Label();

        // keep instance of UI in a field,
        // and update it whenever the EmployeeGui is (re-)attached to the page
        // (important when using @PreserveOnRefresh or RouterLayout)
        addAttachListener(event -> {
            this.ui = event.getUI();
        });

        buttonOK.addClickListener(buttonClickEvent -> {
            this.webClient.get()
                    .uri(uriBuilder -> uriBuilder
                            .path("/employee/{id}")
                            .build(textEmployee.getValue()))
                            .retrieve()
                            .bodyToMono(EmployeeTo.class)
                            .subscribe(emp -> {
                                   // use ui.access to obtain lock on UI, perform updates within
                                   getUI().access(() -> label.setText(emp.getName()));
                                });
                            });

        add(textEmployee,buttonOK, label);
    }

    private UI getUI(){
        return this.ui;
    }
}
但是,根据您希望对应用程序执行的操作,我可以建议使用来让用户登录,这样您就可以轻松直接地访问当前用户名