ViewControllerRegistry用法
当项目中涉及大量的页面跳转,并且这个页面跳转没有任何业务逻辑过程,只是简单的点击一个按钮跳转到一个页面,我们可以使用 ViewControllerRegistry的addViewControllers方法实现无业务逻辑跳转,减少控制器代码的编写
🦆平时写法
@Controller
public class IndexController{
@RequestMapping({"/","/index.html"})
public String index(){
return "index";
}
}
🦆好方法
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
}