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 博客的源码也在里面


原创粉丝点击