SpringBoot 同時整合thymeleaf html、vue html和jsp-在線助手

###問題描述html

  • SpringBoot如何同時訪問html和jsp
  • SpringBoot訪問html頁面能夠,訪問jsp頁面報錯
  • SpringBoot如何同時整合thymeleaf html、vue html和jsp
  • java web spring mvc項目如何同時訪問html和jsp

###解決辦法:配置多視圖實現的視圖解析器vue

#####步驟1、新建一個ViewResolverConfiguration,下面是個人網站 在線助手|在線工具|在線生成|在線製做 的具體實現:java

/**
 * 主要配置多視圖實現的視圖解析器相關bean實例
 *
 * http://www.it399.com/
 *
 * 其實關鍵點在於兩個:
 * 一、配置order屬性
 * 二、配置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);
        }
    }


}

#####步驟2、測試類:web

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


}

#####測試結果:訪問 在線助手|在線工具|在線生成|在線製做,或者輸入如下連接spring

jsp頁面 http://www.it399.com/index.jsp thymeleaf模板頁面 http://www.it399.com/blog/web/201805031726 vue搭建的頁面http://www.it399.com/blog/web/Web201805041746springboot

均正常顯示mvc

#####這樣springboot就能夠同時支持訪問jsp頁面和html頁面了。app

本文同步發佈在 在線助手|在線工具|在線生成|在線製做轉載請註明來自 在線助手|在線工具|在線生成|在線製做 博客頻道【SpringBoot 同時整合thymeleaf html、vue html和jsp】,原文連接:http://www.it399.com/blog/web/201805041746jsp

相關文章
相關標籤/搜索