如何将springboot错误页面重定向到vaadin错误UI?

时间:2018-01-06 18:45:40

标签: spring-boot vaadin8

我在vaadin UI中有一个错误页面。但有时我们可以写一个错误的URL,我们会看到springboot错误页面。

我想在这种情况下显示vaadin UI错误页面。顺便说一句,我已经为springboot提供了一个渲染的404页面。但我不想表现出来。

这是我的vaadin错误用户界面。但这适用于应用程序。 (http://localhost:7001/MyApplication/#!error

有时我会写一个无效的网址:http://localhost:7001/MyApplication/blablablabla

在这种情况下,我想重定向vaadin错误页面(http://localhost:7001/MyApplication/#!error)但是springboot会将我重定向到渲染的404页面。

有可能吗?

@UIScope
@SpringView(name = ErrorView.VIEW_NAME)
public class ErrorView extends VerticalLayout implements View {

    public static final String VIEW_NAME = "error";

    private Label explanation;

    public ErrorView() {
        Label header = new Label("The view could not be found");
        header.addStyleName(MaterialTheme.LABEL_H1);
        addComponent(header);
        addComponent(explanation = new Label());
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        explanation.setValue(String.format("You tried to navigate to a view ('%s') that does not exist.", event.getViewName()));
        getUI().getNavigator().navigateTo(ErrorView.VIEW_NAME);
    }
}

2 个答案:

答案 0 :(得分:0)

你可以把404/500页面放在resources / error /文件夹下,Spring boot会在出错时自动重定向那些页面

答案 1 :(得分:0)

我认为SpringNavigator可以解决您的问题。在定义导航器时,您还可以定义错误视图。请参阅下面的示例

@SpringUI(path = "ui")
public class DemoUI extends com.vaadin.ui.UI {
    @Override
    protected void init(VaadinRequest vaadinRequest) {
        SpringNavigator navigator = new SpringNavigator();
        navigator.setErrorView(new ErrorView());
        setNavigator(navigator);
    }
}