筱进GG的gravatar头像
筱进GG2017-11-08 11:47:07
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引入:

<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=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<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就完成了!

源码链接分享:Spring Boot学习(四)之web开发渲染页面 -- Freemarker博客源码分享


打赏

已有2人打赏

mySong的gravatar头像最代码官方的gravatar头像

分享到:

最近浏览
1158756616 LV77月13日
月亮星星星星星星
zhangbendong LV87月8日
月亮月亮
dns10007月3日
暂无贡献等级
浙江螃蟹 LV37月2日
星星星星星星
zeng_qs LV16月14日
星星
zhos0212 LV155月10日
月亮月亮月亮星星星星星星
liujiajin LV114月9日
月亮月亮星星星星星星
dragonn88 LV53月27日
月亮星星
764982099 LV83月17日
月亮月亮
yutons LV71月24日
月亮星星星星星星
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友