Vaadin 14嵌套路线可避免在​​导航中刷新

时间:2019-09-22 18:37:40

标签: java spring routes vaadin vaadin-flow

每次我单击OperationsLayoutFactory中的“添加”按钮时,页面都会刷新并在此之后为空。
我只想将“ textfieldName”插入选项卡的page1中的Div中。
当我看到空白页时,可以在浏览器中单击“上一步”。之后,正确添加。
如何避免加载空白的新页面?我的路线有问题吗?

点击添加之前 Before "Add"-Click

点击添加并返回浏览器后 After Add-Click and Browser-Back


我的路线布局
-UniversMainUI
--OperationsLayout(路由名称:Operations),父级:UniversMainUI
--- AddUniversitaetLayout(路由名称:justtest),父级:OperationsLayoutFactory

UniversMainUI

@Route("ui")
@RouteAlias("")
@PageTitle("U N I V E R S")
@Theme(value = Lumo.class, variant = Lumo.DARK)
public class UniversMainUI extends Composite<VerticalLayout> implements HasComponents, RouterLayout {

@Autowired
private UniversLogoLayoutFactory universLogoLayoutFactory;

@Autowired
private UniversMenuFactory universMenuFactory;

Component logo;
Component menueLinks;
Component rechtsDatenfeld;
Component studentRemove;

private Div childWrapper = new Div();

public UniversMainUI() {

}

@PostConstruct // autowired-Components sind erst nach Konstruktordurchlauf verfuegbar
private void init() {

    logo = universLogoLayoutFactory.createComponent();
    add(logo);

    menueLinks = universMenuFactory.createComponent();
    add(menueLinks);


    Stream<Component> children = menueLinks.getChildren();
    List<Component> listchildren = children.collect(Collectors.toList());
    ArrayList<Component> arraylistchildren = new ArrayList<Component>(listchildren);

    HorizontalLayout h1 = new HorizontalLayout(menueLinks, childWrapper);
    h1.setHeight("75%");
    h1.setWidth("100%");
    getContent().setSizeFull(); // wichtig fuer Ermoeglichung prozentualer Angabe der Komponentenausmasse
    add(h1);

    for (Component c : arraylistchildren) {
        if (c.getId().get().equals("menueTreeGrid")) // auf Werte vom Typ Optional kann mittels name.get zugegriffen werden
        {
            TreeGrid<Ebene> myTreeGrid = (TreeGrid<Ebene>) arraylistchildren.get(0);
            myTreeGrid.addItemClickListener(event -> {
                if (event.getItem().getBezeichnung().equals(LangStrings.MENU_ADD_STUDENT.toString())) {
                    myTreeGrid.getUI().ifPresent(ui -> ui.navigate("StudentLayout"));
                }
                if (event.getItem().getBezeichnung().equals(LangStrings.MENU_REMOVE_STUDENT.toString())) {
                    myTreeGrid.getUI().ifPresent(ui -> ui.navigate("DeleteStudent"));
                }
                if (event.getItem().getBezeichnung().equals(LangStrings.MENU_OPERATIONS.toString())) {
                    myTreeGrid.getUI().ifPresent(ui -> ui.navigate("Operations"));
                }

                ;
            });
        }
    }
}

@Override
public void showRouterLayoutContent(HasElement content) {
    childWrapper.getElement().appendChild(content.getElement());
}
}

OperationsLayout

@org.springframework.stereotype.Component // spring-Component und NICHT vaadin-component (aufpassen bei Import)
public class OperationsLayoutFactory extends Composite<VerticalLayout> implements 
UIComponentBuilder,RouterLayout {
@Autowired
AddUniversitaetLayoutFactory addUniversitaetLayoutFactory;
Div childChildWrapper=new Div();

@ParentLayout(UniversMainUI.class)
@Route(value = "Operations", layout = UniversMainUI.class)
private class OperationsLayout extends VerticalLayout {
    Tab tab1;
    Tab tab2;
    Tab tab3;
    Div page1;
    Div page2;
    Div page3;
    Map<Tab, Component> tabsToPages;
    Tabs tabs;
    Div pages;

    Button addButton;

    public OperationsLayout() {
        init().layout();
    }


    public OperationsLayout init() {
        tab1 = new Tab("Hinzufuegen");
        tab2 = new Tab("Alle Anzeigen");
        tab3 = new Tab("Statistiken");

        page1 = new Div();
        page2 = new Div();
        page3 = new Div();



        tabsToPages = new HashMap<>();

        tabs = new Tabs(tab1, tab2, tab3);
        pages = new Div(page1, page2, page3);


        addButton=new Button("Add");
        addButton.addClickListener(e -> {
            page1.getUI().ifPresent(ui -> ui.navigate("justtest"));
        });



        return this;
    }

    public OperationsLayout layout() {

        page1.setText("Erste Seite");
        page1.add(addButton);

        page2.setText("Zweite Seite");
        page2.setVisible(false);

        page3.setText("Dritte Seite");
        page3.setVisible(false);

        tabsToPages.put(tab1, page1);
        tabsToPages.put(tab2, page2);
        tabsToPages.put(tab3, page3);

        pages.setWidth("100%"); // Nutzung verfuegbarer Breite fuer Tabinhalte
        tabs.setWidth("100%");
        tabs.setFlexGrowForEnclosedTabs(1);

        Set<Component> gezeigteSeiten = Stream.of(page1).collect(Collectors.toSet());

        tabs.addSelectedChangeListener(event -> {
            gezeigteSeiten.forEach(page -> page.setVisible(false));
            gezeigteSeiten.clear();
            Component ausgewaehlteSeite = tabsToPages.get(tabs.getSelectedTab());
            ausgewaehlteSeite.setVisible(true);
            gezeigteSeiten.add(ausgewaehlteSeite);
        });

        HorizontalLayout h1=new HorizontalLayout();
        childChildWrapper.setWidth("100%");
        childChildWrapper.setHeight("100%");
        h1.add(childChildWrapper);
        page1.add(h1);
        setSizeFull();
        add(tabs);
        add(pages);
        return this;
    }

}

@Override
public com.vaadin.flow.component.Component createComponent() {

    return new OperationsLayout().init().layout();

}

@Override
public void showRouterLayoutContent(HasElement content) {
    System.out.println("zu childchildwrapper: "+childChildWrapper.getElement()+" wird jetzt hinzugefuegt: "+content.getElement());
    childChildWrapper.getElement().appendChild(content.getElement());
}
}

添加大学布局

@org.springframework.stereotype.Component // spring-Component und NICHT vaadin-component (aufpassen bei Import)
public class justTest extends Composite<VerticalLayout> implements UIComponentBuilder { //TODO: Listener einbauen

@Route(value="justtest", layout=OperationsLayoutFactory.class)
private class AddUniversitaetLayout extends Composite<VerticalLayout>{
    TextField textfieldName;

    public AddUniversitaetLayout() {
        // TODO Auto-generated constructor stub
        init().bind().layout();
    }


    public AddUniversitaetLayout init() {

        textfieldName=new TextField("Test erfolgreich!");
        return this;
    }

    public AddUniversitaetLayout bind() {
        return this;
    }

    public AddUniversitaetLayout layout() {


        getContent().add(textfieldName);
        return this;

    }

}

@Override
public com.vaadin.flow.component.Component createComponent() {
    return new AddUniversitaetLayout().init().bind().layout();
}

1 个答案:

答案 0 :(得分:1)

我的应用程序中也嵌套了布局。

我的路线布局

  • -MainUI
  • -BookingsPage(路线名称:预订),父级:MainUI
  • ---预订(路线名称:预订),父级:BookingsPage
  • ---搜索(路线名称:搜索),父级:BookingsPage

我的MainUI.class没有@Route注释,但是实现了RouterLayout

我的BookingsPage.class作为这些注释。

given T(n)=T(√n)+n
        =>T(n-1)=T(√(n-1))+(n-1)  
        T(n)=T(sqrt(n-1))+(n-1)+n; 
        similarily evaluated T(n-2),
        T(n-3),.......

        => T(n)=T(√(n-k))+(n-k)+(n-(k-1))+.......+(n-1)+n
         assumed n-k=0
        =>n=k;
        =>T(n)=T(√(k-k))+(n-(n-1))+(n-(n-2))+......+(n-1)+n
        =>T(n)=T(0)+1+2+3+......+n
        =>T(n)=base case + Σn
        =>T(n)=constant + n(n+1)/2
        =>T(n)=O(n^2)

我的Booking.class具有以下注释:

@RoutePrefix(value="bookings")
@ParentLayout(MainUI.class)

我的Search.class具有以下注释:

@Route(value="booking", layout=BookingsPage.class)

因此您可以导航到

@Route(value="search", layout=BookingsPage.class)

页面没有刷新。例如

/bookings/booking
/bookings/search

我也有

UI.getCurrent().navigate("bookings/search");

在Search.class页面中,以便您可以导航到

@RouteAlias(value="",layout=BookingsPage.class) 

并到达搜索页面(以及导航到/ bookings / search,因此,与/ bookings的默认页面一样是搜索页面)。

希望这会有所帮助。

斯图尔特