spring如何配置多视图实现的视图解析器
这篇文章主要介绍“spring如何配置多视图实现的视图解析器”,在日常操作中,相信很多人在spring如何配置多视图实现的视图解析器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”spring如何配置多视图实现的视图解析器”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
乐都ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!
步骤一、新建一个ViewResolverConfiguration
/** * 主要配置多视图实现的视图解析器相关bean实例 * * https://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页面 https://www.it399.com/index.jsp
thymeleaf模板页面 https://www.it399.com/blog/web/201805031726
vue搭建的页面https://www.it399.com/blog/web/Web201805041746
均正常显示
到此,关于“spring如何配置多视图实现的视图解析器”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
本文名称:spring如何配置多视图实现的视图解析器
当前路径:http://pwwzsj.com/article/ghhsjd.html