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

陳守印發表於2018-05-23
問題描述
SpringBoot如何同時訪問html和jsp
SpringBoot訪問html頁面可以,訪問jsp頁面報錯
SpringBoot如何同時整合thymeleaf html、vue html和jsp
java web spring mvc專案如何同時訪問html和jsp


解決辦法:配置多檢視實現的檢視解析器

步驟一、新建一個ViewResolverConfiguration,下面是網站 的具體實現:

/**
 * 主要配置多檢視實現的檢視解析器相關bean例項
 *
 *
 *
 * 其實關鍵點在於兩個:
 * 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頁面
thymeleaf模板頁面 blog/web/201805031726vue搭建的頁面 blog/web/Web201805041746

均正常顯示

這樣springboot就可以同時支援訪問jsp頁面和html頁面了。


本文同步釋出在 ,轉載請註明來自 部落格頻道【SpringBoot 同時整合thymeleaf html、vue html和jsp】,原文連結 blog/web/201805041746

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28470105/viewspace-2154966/,如需轉載,請註明出處,否則將追究法律責任。

相關文章