Spring Boot学习(四)之web开发渲染页面 -- Freemarker
来源:互联网 发布:sketchup看图软件 编辑:程序博客网 时间:2024/06/05 02:07
上篇编写了Spring Boot学习(四)之web开发渲染页面 -- Thymeleaf
接下来我们来看一下Freemarker整合springboot
来看controller:
@RequestMapping("/") public String index(ModelMap map) { map.addAttribute("name", "筱进GG"); return "index"; }
pom.xml引入:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.3.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork></configuration></plugin></plugins></build>
编写一个hello.ftl文件,此文件的路径在src/main/resources/templates下,其中hello.ftl文件的内容如下:
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" /> <title></title></head><body>FreeMarker模板引擎welcome ${name} to freemarker!</body></html>
我们就可以启动我们的程序进行测试了,访问地址:
http://127.0.0.1:8080/hello ,如果你在浏览器中看到如下信息:
FreeMarker模板引擎 welcome 筱进GG to freemarker!
那么说明你的 ok 了。
freemarker配置:
在spring boot的application.properties属性文件中为freemarker提供了一些常用的配置,如下:
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.
参数默认配置,可写可不写
spring.freemarker.allow-request-override=falsespring.freemarker.cache=truespring.freemarker.check-template-location=truespring.freemarker.charset=UTF-8spring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=falsespring.freemarker.expose-spring-macro-helpers=false#spring.freemarker.prefix=#spring.freemarker.request-context-attribute=#spring.freemarker.settings.*=#spring.freemarker.suffix=.ftl#spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list#spring.freemarker.view-names= # whitelist of view names that can be resolved
修改index方法:
@RequestMapping("/") public String index(ModelMap map) { map.addAttribute("name", "筱进GG"); map.put("gender",1);//gender:性别,1:男;0:女; List<Map<String,Object>> friends =new ArrayList<Map<String,Object>>(); Map<String,Object> friend = new HashMap<String,Object>(); friend.put("name", "张三"); friend.put("age", 20); friends.add(friend); friend = new HashMap<String,Object>(); friend.put("name", "李四"); friend.put("age", 22); friends.add(friend); map.put("friends", friends); return "index"; }
修改页面
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" /> <title></title></head><body> FreeMarker模板引擎 <p> welcome ${name} to freemarker! </p> <p>性别: <#if gender==0> 女 <#elseif gender==1> 男 <#else> 保密 </#if> </p> <h4>我的好友:</h4> <#list friends as item> 姓名:${item.name} , 年龄${item.age} <br> </#list></body></html>
重启运行项目:
http://127.0.0.1:8080/hello可以看到
FreeMarker模板引擎
welcome 筱进GG to freemarker!
性别: 男
我的好友:
姓名:张三 , 年龄20
姓名:李四 , 年龄22
至此,freemarker整合springboot就完成了!
欢迎大家一起交流学习SpringBoot,java等领域的技术,交流群 : 587674051 博客的源码也在里面
阅读全文
0 0
- Spring Boot学习(四)之web开发渲染页面 -- Freemarker
- Spring Boot学习(四)之web开发渲染页面 -- Thymeleaf
- Spring Boot学习(四)之web开发渲染页面 -- Velocity
- Spring Boot 使用 FreeMarker 渲染页面
- spring boot 配置freemarker及使用freemarker渲染页面
- spring boot之FreeMarker
- spring boot框架学习9-spring boot的web开发(5)-错误解决及跳转页面
- Spring Boot基础6-web应用开发-模板引擎FreeMarker
- Spring Boot 教程(四) -- 开发 Web 应用
- spring-boot集成Freemarker开发
- Spring Boot 13 之freemarker
- Spring学习笔记之渲染Web视图
- Spring Boot学习之旅:(十三)模版引擎-FreeMarker
- Spring boot 页面分离 (freemarker 分离)
- Java Web开发之集成Spring Boot
- Spring Boot系列之二 Web开发
- spring boot之web开发(二)
- Spring Boot学习(四)
- QTP触发了场景恢复,但是没有重复当前步骤并继续
- 2017双十一复盘--开发经验总结
- 关于内部函数与外部函数
- 1004. 成绩排名 (20)
- Java判断密码强度工具类
- Spring Boot学习(四)之web开发渲染页面 -- Freemarker
- 用echarts撸了个Nginx连接数监控
- Namespace Mapping
- python学习—Day20—模块:logging、os、command、sys
- 修改jar内容并重新打包为jar
- 处理时间戳
- 斐波那契数列的两种实现呢
- Spring Boot学习(四)之web开发渲染页面 -- Velocity
- python判断list是否为空