开启SpringBoot的Histoty模式
程序员文章站
2022-03-29 16:14:14
...
通过history api,可以丢弃丑陋的#。
以下是springboot开启history模式的配置:
springBoot 1.x开启history模式(springBoot默认匹配不到URL时,会返回一个默认的页面,如index.html):
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Component
public class WebConfiguration implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/index.html"));
}
}
springBoot 2.x开启history模式(springBoot默认匹配不到URL时,会返回一个默认的页面,如index.html):
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@SuppressWarnings("rawtypes")
@Component
public class WebConfiguration implements WebServerFactoryCustomizer {
@Override
public void customize(WebServerFactory factory) {
((ErrorPageRegistry) factory).addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/index.html"));
}
}