這篇文章主要介紹“spring如何配置多視圖實(shí)現(xiàn)的視圖解析器”,在日常操作中,相信很多人在spring如何配置多視圖實(shí)現(xiàn)的視圖解析器問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”spring如何配置多視圖實(shí)現(xiàn)的視圖解析器”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
樂都ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
步驟一、新建一個(gè)ViewResolverConfiguration
/** * 主要配置多視圖實(shí)現(xiàn)的視圖解析器相關(guān)bean實(shí)例 * * https://www.it399.com/ * * 其實(shí)關(guān)鍵點(diǎn)在于兩個(gè): * 1、配置order屬性 * 2、配置viewnames屬性 * * 注意: * return new ModelAndView("jsps/index");//或者return "jsps/index" * 對(duì)應(yīng) /WEB-INF/jsps/index.jsp * ========================== * 同理: * return "thymeleaf/index";//或者return “thymeleaf/index” * 對(duì)應(yīng) /WEB-INF/thymeleaf/index.html * * */ @Configuration public class ViewResolverConfiguration { @Configuration //用來定義 DispatcherServlet 應(yīng)用上下文中的 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); } } }
步驟二、測(cè)試類:
@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頁面 https://www.it399.com/index.jsp
thymeleaf模板頁面 https://www.it399.com/blog/web/201805031726
vue搭建的頁面https://www.it399.com/blog/web/Web201805041746
均正常顯示
到此,關(guān)于“spring如何配置多視圖實(shí)現(xiàn)的視圖解析器”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!