我只是延長Vaadin的導航和重寫一些一些方法:定製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);
}
}
而且這是我如何獲得導航器並將視圖重定向到另一個:
@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){
}
}
由於某種原因,按鈕不響應點擊事件。
替換爲標準導航器 - 它的工作原理。
我做錯了什麼?
感謝建議
您應該查看視圖更改偵聽器。你不需要重寫導航器。我會張貼一些代碼,但我今天不在我的電腦附近。 –
@ChrisM聽起來很有趣,我會繼續關注。 – Reborn
還有SpringNavigator類,其中包括SpringViewProvider。 – nyg