@Configuration//用来定义 DispatcherServlet 应用上下文中的 bean
@EnableWebMvc
@ComponentScan("com.csy.spring")
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
// resolver.setPrefix("/WEB-INF/");
// resolver.setSuffix(".jsp");
// resolver.setViewNames("jsps/*");
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
resolver.setViewNames("*");
resolver.setOrder(2);
return resolver;
}
@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("utf-8");
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
// templateEngine
return templateEngine;
}
@Bean
public ThymeleafViewResolver viewResolverThymeLeaf() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("utf-8");
viewResolver.setOrder(1);
//viewResolver.setViewNames(new String[]{"thyme/*"});
viewResolver.setViewNames(new String[]{"thymeleaf/*","vue/*"});
return viewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
}
}
}
步骤二、测试类:
@Controller
public class IndexController {
@GetMapping("/testJsp")
public String testJsp(Model model) {
model.addAttribute("message", "this is index jsp page");
return "index";
}
@GetMapping("/testThemleaf")
public String testThemleaf(Model model) {
model.addAttribute("message", "this is index jsp page");
return "thymeleaf/test";
}
@GetMapping("/testVue")
public String testVue(Model model) {
model.addAttribute("message", "this is index jsp page");
return "vue/testVue";
}