SpringBoot整合Freemarker

326 阅读1分钟

SpringBoot整合Freemarker

1、创建maven的jar项目工程

2、添加pom的依赖

<!-- springboot不建议使用jsp,使用模板引擎,
      比如themleaf,velocity,freemarker
      整合freemarker -->
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
      </dependency>

3、启动类

/**
* 启动类
* @author Administrator
*
*/
@SpringBootApplication
public class FirstApplication {
   public static void main(String[] args) {
       SpringApplication.run(FirstApplication.class, args);
   }
}

4、在resources创建application.properties配置文件

#springboot整合freemarker
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.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates

5、在resources创建templates文件

6、创建indexController

@Controller
public class IndexController {

	@RequestMapping("index")
	public String index(Model model) {
		model.addAttribute("msg", "测试index freemarker页面");
		return "index";
	}
}

7、请求路径http://localhost:8080/index