springboot整合jsp,thymeleaf.源代码下载
By:Roy.LiuLast updated:2019-09-26
在整合项目过程中,还有可能遇到有用 jsp 写的,有用thymeleaf写的。这时候也需要整合. 整合方式很简单,spring mvc 本身就支持,所以在springboot中一样支持,只不过是xml配置,变成了一个启动时加载的类而已。在我的例子用的是AppConfig类.
1. 整个项目工程结构
用了之前用来测试springboot 整个hibernate, mybatis的例子。
2. AppConfig类,用来配置视图解析,以及静态资源文件映射
3. 运行起来,测试运行结果:
4. 项目源代码下载:
springboot integrate jsp and thymeleaf source code download
1. 整个项目工程结构
用了之前用来测试springboot 整个hibernate, mybatis的例子。
2. AppConfig类,用来配置视图解析,以及静态资源文件映射
package com.yihaomen.hm; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring5.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ITemplateResolver; @Configuration public class AppConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations( "classpath:/static/" ); } @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/"); resolver.setSuffix(".jsp"); resolver.setViewNames("jsp/*"); resolver.setOrder(2); return resolver; } @Bean public ITemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setTemplateMode("HTML5"); templateResolver.setPrefix("/WEB-INF/"); templateResolver.setSuffix(".html"); templateResolver.setCharacterEncoding("utf-8"); templateResolver.setCacheable(false); return templateResolver; } @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); return templateEngine; } @Bean public ThymeleafViewResolver viewResolverThymeLeaf() { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine()); viewResolver.setCharacterEncoding("utf-8"); viewResolver.setViewNames(new String[]{"html/*"}); viewResolver.setOrder(1); return viewResolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }
3. 运行起来,测试运行结果:
4. 项目源代码下载:
springboot integrate jsp and thymeleaf source code download
From:一号门
Next:用注解生成整个系统的权限菜单等数据
COMMENTS