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";
}