SpringBoot 同时整合thymeleaf html、vue html和jsp

时间:2021-03-06 10:06:16

问题描述

  • SpringBoot如何同时访问html和jsp
  • SpringBoot访问html页面可以,访问jsp页面报错
  • SpringBoot如何同时整合thymeleaf html、vue html和jsp
  • java web spring mvc项目如何同时访问html和jsp

解决办法:配置多视图实现的视图解析器

步骤一、新建一个ViewResolverConfiguration,下面是我的网站 在线助手|在线工具|在线生成|在线制作 的具体实现:

/**
 * 主要配置多视图实现的视图解析器相关bean实例
 *
 * http://www.it399.com/
 *
 * 其实关键点在于两个:
 * 1、配置order属性
 * 2、配置viewnames属性
 *
 * 注意:
 * return new ModelAndView("jsps/index");//或者return "jsps/index"
 * 对应 /WEB-INF/jsps/index.jsp
 * ==========================
 * 同理:
 * return "thymeleaf/index";//或者return “thymeleaf/index”
 * 对应 /WEB-INF/thymeleaf/index.html
 *
 *
 */
@Configuration
public class ViewResolverConfiguration {

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

}
测试结果:访问 在线助手|在线工具|在线生成|在线制作,或者输入以下链接

jsp页面 http://www.it399.com/index.jsp

thymeleaf模板页面 http://www.it399.com/blog/web/201805031726

vue搭建的页面http://www.it399.com/blog/web/Web201805041746

均正常显示

这样springboot就可以同时支持访问jsp页面和html页面了。

本文同步发布在 在线助手|在线工具|在线生成|在线制作转载请注明来自 在线助手|在线工具|在线生成|在线制作 博客频道【SpringBoot 同时整合thymeleaf html、vue html和jsp】,原文链接:**http://www.it399.com/blog/web/201805041746**