springBoot的mcv(视图)控制
新建个类,继承WebMvcConfigurer
类名上加注解@Configuration
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
//视图
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//当return:“test”时就会跳转到“/”
registry.addViewController("/").setViewName("test");
registry.addViewController("index.html").setViewName("test2");
}
}
例子如下
@RequestMapping("/test2")
public String test(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
model.addAttribute("msg","<h1>hushuai</h1>");
model.addAttribute("users", Arrays.asList("hu","shuai"));
request.getRequestDispatcher("/index.html").forward(request,response);
return "test";
}
SpringBoot MVC配置:视图控制器实现页面跳转
本文介绍了如何在SpringBoot中通过继承WebMvcConfigurer并实现addViewControllers方法,配置视图控制器,实现当请求特定路径时跳转到预设的视图。示例中,当返回'test'时,会重定向到'/', 'index.html'也对应相同的视图。
1万+

被折叠的 条评论
为什么被折叠?



