gpt4 book ai didi

java - 自定义 Vaadin 导航器

转载 作者:行者123 更新时间:2023-12-01 09:13:16 25 4
gpt4 key购买 nike

我刚刚扩展了 Vaadin 的导航器并重写了一些方法:

public class MyNavigator extends Navigator {

final SpringViewProvider viewProvider;

public MyNavigator(UI ui, SingleComponentContainer container, SpringViewProvider viewProvider) {
super(ui, container);
this.viewProvider = viewProvider;
addProvider(this.viewProvider);
}

@Override
public void navigateTo(String navigationState) {
if(ViewTokens.AVAILABLE_TOKENS.contains(navigationState)) {
if(viewProvider.getView(navigationState) == null) {
String uriFragment = parseViewToken();
if(uriFragment.equals(navigationState)) {
super.navigateTo(ViewTokens.HOME);
}
} else {
//if nothing found, redirect
super.navigateTo(ViewTokens.HOME);
}
}
}

private String parseViewToken() {
<ommitted>
}

}

这就是我设置导航器的方式:

@SpringUI(path="/")
@Theme(ValoTheme.THEME_NAME)
public class MyFancyUI extends UI {

@Autowired
SpringViewProvider viewProvider;

@Override
protected void init(VaadinRequest vaadinRequest) {
MyNavigator navigator = new MyNavigator(UI.getCurrent(), this, viewProvider);
setNavigator(navigator);
navigator.navigateTo(ViewTokens.SIGNIN);
}

}

这就是我尝试获取导航器并将 View 重定向到另一个导航器的方式:

@UIScope
@SpringView(name=ViewTokens.HOME)
public class SplashScreen extends VerticalLayout implements View {
private ComponentHelper componentHelper;
private Button toLoginPage;

public SplashScreen() {
initComponents();
buildSplashView();
}

protected void initComponents() {
componentHelper = ComponentHelper.getInstance();
toLoginPage = componentHelper.createFriendlyButton("To Login Page",220, 60);
toLoginPage.addClickListener(this::redirect);
}

private void buildSplashView() {
addComponent(toLoginPage);
setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
setHeight(100, Unit.PERCENTAGE);
}

public void redirect(Button.ClickEvent event) {
getUI().getNavigator().navigateTo(ViewTokens.SIGNIN);
}

@Override
public void enter(ViewChangeListener.ViewChangeEvent viewChangeEvent){

}
}

由于某种原因,按钮对点击事件没有反应。

替换为标准导航器 - 它有效。

我做错了什么?

感谢您的建议

最佳答案

我本以为你可以使用 Vaadin 的导航器和 View 更改监听器,如下所示:

        navigator.addViewChangeListener(new ViewChangeListener() {

@Override
public boolean beforeViewChange(ViewChangeEvent event) {
// check you're happy to change

if (ViewTokens.AVAILABLE_TOKENS.contains(navigationState)) {
if (viewProvider.getView(event.getViewName()) == null) {
String uriFragment = parseViewToken();
if (uriFragment.equals(event.getViewName())) {
navigator.navigateTo(ViewTokens.HOME);
return false;
}
} else {
// if nothing found, redirect
navigator.navigateTo(ViewTokens.HOME);
return false;
}
// Let the view change happen.
return true;
}

}

@Override
public void afterViewChange(ViewChangeEvent event) {

}
});

如果您在 beforeViewChange 中返回 false,它将阻止 View 更改发生。

关于java - 自定义 Vaadin 导航器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40781806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com