您好,登录后才能下订单哦!
这篇文章主要介绍“spring如何配置多视图实现的视图解析器”,在日常操作中,相信很多人在spring如何配置多视图实现的视图解析器问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”spring如何配置多视图实现的视图解析器”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
步骤一、新建一个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如何配置多视图实现的视图解析器”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。