Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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 Vaadin参考路线布局图中的主布局图_Java_Cookies_Buffer_Vaadin - Fatal编程技术网

Java Vaadin参考路线布局图中的主布局图

Java Vaadin参考路线布局图中的主布局图,java,cookies,buffer,vaadin,Java,Cookies,Buffer,Vaadin,我需要在应用程序屏幕之间创建一个缓冲区。 我想在主布局中创建一个缓冲区,但我无法从子层访问它 我试图通过“静态”来实现,然后缓冲区对于所有用户都是通用的,这是不正确的 Cookie也不合适,因为数据结构很复杂 带有静态缓冲区的代码: 在本例中,创建了一个表,当您选择表中的元素时,它们将写入缓冲区,如果缓冲区中有元素,则在表中标记它们 MainLayout.java public class MainLayout extends AppLayout implements RouterLayout

我需要在应用程序屏幕之间创建一个缓冲区。 我想在主布局中创建一个缓冲区,但我无法从子层访问它

我试图通过“静态”来实现,然后缓冲区对于所有用户都是通用的,这是不正确的

Cookie也不合适,因为数据结构很复杂

带有静态缓冲区的代码:

在本例中,创建了一个表,当您选择表中的元素时,它们将写入缓冲区,如果缓冲区中有元素,则在表中标记它们

MainLayout.java

public class MainLayout extends AppLayout implements RouterLayout {


    public static Set<Test> buffer;

    public MainLayout() {
        buffer = new HashSet<Test>();
        /*...*/
    }
}
public类MainLayout扩展AppLayout实现RouterLayout{
公共静态设置缓冲区;
公共主布局图(){
buffer=newhashset();
/*...*/
}
}
BasicView.java

@Route(value = "BasicView", layout = MainLayout.class)
@RouteAlias(value = "/BasicView", layout = MainLayout.class)
public class BasicView extends VerticalLayout {

    private final Grid<Test> tests;

    public BasicView(@Autowired TestService){
        /*...*/
        tests = new Grid<>(Test.class, false);
        tests.addSelectionListener(event -> {
        MainLayout.buffer = event.getAllSelectedItems();});
        /*...*/
        for(Test el : MainLayout.buffer)
        {
           tests.select(el);
        }
        /*...*/
    }
}
@Route(value=“BasicView”,layout=MainLayout.class)
@RouteAlias(value=“/BasicView”,layout=MainLayout.class)
公共类BasicView扩展了垂直布局{
私人最终网格测试;
公共基本视图(@autowiredtestservice){
/*...*/
测试=新网格(Test.class,false);
tests.addSelectionListener(事件->{
MainLayout.buffer=event.getAllSelectedItems();});
/*...*/
用于(测试el:MainLayout.buffer)
{
测试。选择(el);
}
/*...*/
}
}

您实际上可以将布局的父级遍历到主布局。你可以在下面做点什么

    Optional<Component> parent = this.getParent();
    Buffer buffer = null;
    while (parent.isPresent()) {
        Component p = parent.get();
        if (p instanceof MainLayout) {
            MainLayout main = (MainLayout) p;
            buffer = main.getBuffer();
        }
        parent = p.getParent();
    }
Optional parent=this.getParent();
缓冲区=空;
while(parent.isPresent()){
组件p=parent.get();
if(主布局的p实例){
主布局main=(主布局)p;
buffer=main.getBuffer();
}
parent=p.getParent();
}
然而,我不确定这是否是最好的办法。如果您碰巧使用了Spring Boot,那么将该缓冲区作为VaadinSessionScoped bean,并在需要时自动连接它会更自然。Vaadin的下一个版本还将添加特定的RouteScope,如果需要,它允许更多的针点范围


另请参阅旧的Vaadin论坛讨论:

您是否可以添加代码(例如,尽管失败但带有静态“缓冲区”的代码),以便我们可以对其进行改进。至少对我来说,还不清楚“缓冲区”是什么意思。是的,我使用Spring Boot和autowired来提供DB查询服务,谢谢你对beans的建议,我会尽力弄清楚的。