Spring Boot学习(四)之web开发渲染页面 -- Freemarker

上篇编写了Spring Boot学习(四)之web开发渲染页面 -- Thymeleaf

接下来我们来看一下Freemarker整合springboot

来看controller:

    @RequestMapping("/")
    public String index(ModelMap map) {
        map.addAttribute("name", "筱进GG");
        return "index";
    }

pom.xml引入:


		org.springframework.boot
		spring-boot-starter-parent
		1.3.2.RELEASE
		 
	

	
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-freemarker
		

	
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					true
				
			
		
	

 编写一个hello.ftl文件,此文件的路径在src/main/resources/templates下,其中hello.ftl文件的内容如下:




    
    


FreeMarker模板引擎
welcome ${name} to freemarker!

我们就可以启动我们的程序进行测试了,访问地址:

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=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.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> friends =new ArrayList>();
        Map friend = new HashMap();
        friend.put("name", "张三");
        friend.put("age", 20);
        friends.add(friend);
        friend = new HashMap();
        friend.put("name", "李四");
        friend.put("age", 22);
        friends.add(friend);
        map.put("friends", friends);
        return "index";
    }

修改页面




    
    


    FreeMarker模板引擎
    

welcome ${name} to freemarker!

性别: <#if gender==0> 女 <#elseif gender==1> 男 <#else> 保密

我的好友:

<#list friends as item> 姓名:${item.name} , 年龄${item.age}

重启运行项目:

http://127.0.0.1:8080/hello可以看到

FreeMarker模板引擎

welcome 筱进GG to freemarker!

性别: 男

我的好友:

姓名:张三 , 年龄20 
姓名:李四 , 年龄22 

 

至此,freemarker整合springboot就完成了!

 

你可能感兴趣的:(Spring,Boot)