將bean添加到配置中以添加視圖控制器。這必須擴展WebMvcConfigurerAdapter
並簡單地覆蓋addViewControllers
方法。
@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/manage/health");
registry.addViewController("/health").setViewName("forward:/manage/health");
}
}
或者,如果你想強制重定向使用addRedirectViewController
代替addViewController
。
@Configuration
public class AliasWebConfig extends WebMvcConfigurerAdapter {
public void addViewControllers(ViewControllerRegistry registry) {
registry. addRedirectViewController("/", "/manage/health");
registry.addRedirectViewController("/health","/manage/health");
}
}
爲這些URL配置視圖控制器,並轉發或重定向到正確的URL。 –