首先添加freemark依赖
<!--freemarker-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
配置freemarker模板
spring.freemarker.suffix=.ftl
spring.freemarker.templateEncoding=UTF-8
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.content-type=text/html
spring.freemarker.request-context-attribute=request
总体代码如下
application.properties
###修改端口号
#server.port=8082
###应用上下文
##server.servlet.context-path=/crm
##spring.main.banner-mode=off
#db.username=root
#db.password=123456
#db.url=jdbc:mysql://localhost:3306/opinion?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
#db.driverClassName=com.mysql.jdbc.Driver
#spring.devtools.restart.triggerFile=trigger.file
#数据源配置
spring.datasource.druid.username=root
spring.datasource.druid.password=123456
spring.datasource.druid.url=jdbc:mysql://localhost:3306/crm?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.initial-size=5
#mybatis配置
mybatis.config.location=classpath:mybatis.cfg.xml
#别名配置
mybatis.type-aliases-package=com.helloworld.mybatis.domain
#xml文件扫描
mybatis.mapper-locations=classpath:com/helloworld/mybatis/mapper/*Mapper.xml
#开启日志
logging.level.com.helloworld.mybatis.mapper=debug
#freemarker配置
spring.freemarker.suffix=.ftl
spring.freemarker.templateEncoding=UTF-8
spring.freemarker.templateLoaderPath=classpath:/templates/
spring.freemarker.content-type=text/html
spring.freemarker.request-context-attribute=request
hello.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FreeMarker的集成</title>
</head>
<body>
Hello ${name}
</body>
</html>
FreeMarkerController
package com.helloworld.freemarker;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class FreeMarkerController {
@RequestMapping("/hello")
public String helloPage(Model model){
model.addAttribute("name","FreeMarker");
return "hello";
}
}
AppConfig
package com.helloworld.freemarker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppConfig {
public static void main(String[] args) {
SpringApplication.run(AppConfig.class,args);
}
}
springboot集成freemarker技术
最新推荐文章于 2023-06-11 22:58:40 发布