现在,当我打开root /
网址时,我会看到index.html
个内容,但网址为/
当我打开/index.html
时,我看到相同的内容,而网址为/index.html
当我打开/
和/index.html
时,我需要看到相同的网址,因此我认为我需要添加重定向:
registry
.addViewController("/").setViewName("forward:/index.html");
.addRedirectViewController("/index.html", "/");
但似乎是循环电话。
我该怎么做,/
和/index.html
在我的配置中显示来自同一网址/
的相同内容?
webapp目录的一部分:
配置的一部分:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**")
.addResourceLocations("/webapp/")
.setCachePeriod(604800)
.resourceChain(true)
.addResolver(new GzipResourceResolver())
.addResolver(new PathResourceResolver());
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry
.addViewController("/").setViewName("forward:/index.html");
}
UPD
我在提问之前尝试了来自@Asgeirr的建议,但我得到了ERR_TOO_MANY_REDIRECTS。
我的服务器配置可能有问题吗?
@Bean
public Server server(ApplicationContext rootCtx){
Server server = new Server(serverPort);
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.addCustomizer(new SecureRequestCustomizer());
ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
httpConnector.setPort(serverPort);
httpConnector.setIdleTimeout(connectorIdleTime);
server.setConnectors(new Connector[]{httpConnector});
ServletContextHandler apiHandler = new ServletContextHandler();
apiHandler.setContextPath("/");
apiHandler.setResourceBase(".");
HashSessionIdManager idManager = new HashSessionIdManager();
server.setSessionIdManager(idManager);
HashSessionManager manager = new HashSessionManager();
manager.setMaxInactiveInterval(sessionTimeout);
SessionHandler sessions = new SessionHandler(manager);
apiHandler.setHandler(sessions);
AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
webCtx.setServletContext(apiHandler.getServletContext());
webCtx.register(WebConfiguration.class);
webCtx.setParent(rootCtx);
webCtx.refresh();
// Adding gzip compression
FilterHolder holder = new FilterHolder(GzipFilter.class);
holder.setInitParameter("deflateCompressionLevel", "9");
holder.setInitParameter("minGzipSize", "0");
holder.setInitParameter("mimeTypes", "text/html,text/css,image/svg+xml,text/xhtml,text/plain,text/xml," +
"text/javascript,application/xhtml+xml,application/xml,application/javascript,application/json");
apiHandler.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
apiHandler.addFilter(new FilterHolder(characterEncodingFilter), "/*", EnumSet.allOf(DispatcherType.class));
apiHandler.addServlet(new ServletHolder(new DispatcherServlet(webCtx)), "/");
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.addHandler(apiHandler);
server.setHandler(handlerCollection);
return server;
}
网络日志,当我拨打/index.html
答案 0 :(得分:1)
您可以进行重定向。
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry
.addViewController("/").setViewName("redirect:/index.html");
}
答案 1 :(得分:0)
您可以在getmapping中重定向,如下所示:
@GetMapping("/index.html")
public String getIndex() {
return "redirect:/";
}
和root的getter看起来像:
@GetMapping("/")
public String getIndex() {
return "index.html";
}
或从建议的addViewControllers中删除转发,以摆脱无限循环调用:
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index.html");
registry.addRedirectViewController("/index.html", "/");
}