spring boot 配置freemarker及如何使用freemarker渲染页面
1.springboot 中自带的页面渲染工具为thymeleaf 还有freemarker 这两种模板引擎 简单比较下两者不同
1.1freemaker 优点
freemarker 不足:thymeleaf由于使用了标签属性做为语法,模版页面直接用浏览器渲染,使得前端和后端可以并行开发。freemarket使用</>这样的语法,就无法直接使浏览器渲染出原本页面的样子。
thymeleaf优点:
静态html嵌入标签属性,浏览器可以直接打开模板文件,便于前后端联调。 springboot官方推荐方案。
thymeleaf缺点:
模板必须符合xml规范 比较下两者
1.从写code的习惯角度可能freemarker更习惯于我们的思维。
2.不过从前后分离开发的角度看thymeleaf更合适,值的绑定都是基于html的dom元素属性的,适合前后联调。
还是回归下主题
开始编码:
1.引入pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2.向yml格式配置文件添加内容
spring: freemarker: request-context-attribute: req #req访问request suffix: .html #后缀名 content-type: text/html enabled: true cache: false #缓存配置 template-loader-path: classpath:/templates/ #模板加载路径 按需配置 charset: UTF-8 #编码格式 settings: number_format: '0.##' #数字格式化,无小数点
3.测试接口如图
4.index文件位置如图
index 内容
5.启动项目如图
index页面渲染成功
网上收集 propertity的freemarker 配置 (propertity格式配置文件)
# FREEMARKER (FreeMarkerAutoConfiguration)
spring.freemarker.allow-request-override=false # Set whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.allow-session-override=false # Set whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
spring.freemarker.cache=false # Enable template caching.
spring.freemarker.charset=UTF-8 # Template encoding.
spring.freemarker.check-template-location=true # Check that the templates location exists.
spring.freemarker.content-type=text/html # Content-Type value.
spring.freemarker.enabled=true # Enable MVC view resolution for this technology.
spring.freemarker.expose-request-attributes=false # Set whether all request attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-session-attributes=false # Set whether all HttpSession attributes should be added to the model prior to merging with the template.
spring.freemarker.expose-spring-macro-helpers=true # Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
spring.freemarker.prefer-file-system-access=true # Prefer file system access for template loading. File system access enables hot detection of template changes.
spring.freemarker.prefix= # Prefix that gets prepended to view names when building a URL.
spring.freemarker.request-context-attribute= # Name of the RequestContext attribute for all views.
spring.freemarker.settings.*= # Well-known FreeMarker keys which will be passed to FreeMarker's Configuration.
spring.freemarker.suffix= # Suffix that gets appended to view names when building a URL.
spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
spring.freemarker.view-names= # White list of view names that can be resolved.
另外需要使用thymeleaf 可以使用如下配置(yml 格式配置文件方式)
##视图模型 spring: thymeleaf: prefix: classpath:/templates/ suffix: .html cache: false encoding: utf-8 content-type: text/html check-template-location: true
到此这篇关于spring boot 配置freemarker及使用freemarker渲染页面的文章就介绍到这了,更多相关spring boot 配置freemarker内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
@Accessors(chain = true)注解报错的解决方案
这篇文章主要介绍了@Accessors(chain = true)注解报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-06-06集合框架(Collections Framework)详解及代码示例
这篇文章主要介绍了集合框架(Collections Framework)详解及代码示例,文章涉及集合数组的区别,collection接口,iterator迭代器,list接口及其用法,LinkedHashSet集合等有关内容,具有一定参考价值,需要的朋友可以了解下。2017-11-11
最新评论