SpringBoot设置首页(默认页)跳转功能的实现方案

欢迎访问我的个人博客

方案1:controller里添加一个"/"的映射路径

1 2 3 4 5 @RequestMapping("/") public String index(Model model, HttpServletResponse response) { model.addAttribute("name", "simonsfan"); return "/index"; }

方案二:设置默认的View跳转页面

1 2 3 4 5 6 7 8 9 @Configuration public class DefaultView extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); super.addViewControllers(registry); } }

如上两种方式均可实现在springboot中设置默认页面跳转。

ps:SpringBoot设置默认首页

1 2 3 4 5 6 7 8 9 @Configuration public class DefaultView extends WebMvcConfigurerAdapter{ @Override public void addViewControllers(ViewControllerRegistry registry) { super.addViewControllers(registry); //主页 registry.addViewController("/").setViewName("forward:/index"); } }
经验分享 程序员 微信小程序 职场和发展