springboot集成freemarker

背景:

springboot支持JSP.jsp)、thymeleaf.html)和freemarker.ftl)这三种前端界面展示形式。这篇文章简单讲述下springboot如何集成freemarker

第一步:添加maven依赖

    <!-- 继承springboot,不加这个parent标签就会报错 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
	</parent>

	<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>
		<!-- 添加freemarker的maven依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>
	</build>

第二步:配置application.properties

# 是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性。
spring.freemarker.allow-request-override=false

# 是否允许HttpSession属性覆盖(隐藏)控制器生成的同名模型属性。
spring.freemarker.allow-session-override=false

# 是否启用模板缓存。
spring.freemarker.cache=false

# 模板编码。
spring.freemarker.charset=UTF-8

# 是否检查模板位置是否存在
spring.freemarker.check-template-location=true

# Content-Type value.
spring.freemarker.content-type=text/html

# 是否启用freemarker
spring.freemarker.enabled=true

# 设定所有request的属性在merge到模板的时候,是否要都添加到model中.
spring.freemarker.expose-request-attributes=false

# 是否在merge模板的时候,将HttpSession属性都添加到model中
spring.freemarker.expose-session-attributes=false

# 设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
spring.freemarker.expose-spring-macro-helpers=true

# 是否优先从文件系统加载template,以支持热加载,默认为true
spring.freemarker.prefer-file-system-access=true

# 设定模板的前缀.
spring.freemarker.prefix=

# 是否在FreeMaker中使用 request.contextPath
spring.freemarker.request-context-attribute=true

# 设定FreeMarker keys.
spring.freemarker.settings.*=

# 设定模板的后缀.
spring.freemarker.suffix=.ftl

# 设定模板的加载路径,多个以逗号分隔,默认:
spring.freemarker.template-loader-path=classpath:/templates/

# 设定默认的视图解析地址
spring.freemarker.view-names=

工程的目录结构如下:

第三步:编写相关代码和文件

TestController代码如下:

@Controller
@RequestMapping("/html")
public class TestController {

	@RequestMapping("/hello")
	public String hello(Model model) {
		model.addAttribute("name", "张三");
		model.addAttribute("age", "30");
		return "hello";
	}
}

App代码如下:

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
    	//将springboot应用驱动起来
        SpringApplication.run(App.class, args);
    }
}

hello.ftl代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
</head>
<body>
    name:${name}
    age:${age}
</body>
</html>

在界面请求http://localhost:8080/html/hello即可:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值