如何将 springboot 错误页面重定向到 vaadin 错误 UI?
How to redirect springboot error page to vaadin error UI?
我在 vaadin 中有一个错误页面 UI。但是有时我们可以写错url,然后我们看到springboot错误页面。
在这种情况下,我想显示 vaadin UI 错误页面。顺便说一句,我已经有一个springboot渲染的404页面了。但是我不想表现出来。
这是我的 vaadin 错误 UI。但这适用于应用程序。 (http://localhost:7001/MyApplication/#!error)
有时我会这样写一个无效的url: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);
}
}
您可以将 404/500 页面放在 resources/error/ 文件夹下,Spring 启动时会在出现错误时自动重定向这些页面
我觉得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);
}
}
我在 vaadin 中有一个错误页面 UI。但是有时我们可以写错url,然后我们看到springboot错误页面。
在这种情况下,我想显示 vaadin UI 错误页面。顺便说一句,我已经有一个springboot渲染的404页面了。但是我不想表现出来。
这是我的 vaadin 错误 UI。但这适用于应用程序。 (http://localhost:7001/MyApplication/#!error)
有时我会这样写一个无效的url: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);
}
}
您可以将 404/500 页面放在 resources/error/ 文件夹下,Spring 启动时会在出现错误时自动重定向这些页面
我觉得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);
}
}