SpringBoot之集成FreeMarker

目录:

一.运行springboot_freemarker工程

二.Springboot-freemarker工程配置详解

三.小结


一.运行springboot_freemarker工程

    git clone 下载工程springboot_repository,项目地址见[email protected]:snail-wj/springboot_repository.git

    1.数据库准备

        建库
        create database springbootdb;
        建表
        create TABLE person(
id int AUTO_INCREMENT PRIMARY KEY,
person_name VARCHAR(50) NOT NULL ,
person_gender VARCHAR(50) NOT NULL ,
description VARCHAR(50) NOT NULL
);
    2.项目结构介绍
org.spring.springboot.controller - Controller 层
org.spring.springboot.dao - 数据操作层 DAO
org.spring.springboot.domain - 实体类
org.spring.springboot.service - 业务逻辑层
Application - 应用启动类
resources/application.properties - 应用配置文件,应用启动会自动读取配置
resources/web - *.ftl文件,是 FreeMarker 文件配置路径。在 application.properties 配置
resources/mapper - DAO Maper XML 文件
    3.运行工程
        右键运行springboot-freemarker工程Application应用启动类的main函数,然后在浏览器访问:
        获取ID编号为1的城市信息
localhost:8080/api/city/1
        
        获取城市列表页面
localhost:8080/api/city

二.Springboot-freemarker工程配置详解

        1.pom.xml文件详解
        

	
		4.0.0
	 
		springboot
		springboot-freemarker
		0.0.1-SNAPSHOT
		springboot-freemarker :: Spring Boot 集成 FreeMarker 案例
	 
		
		
			org.springframework.boot
			spring-boot-starter-parent
			1.5.1.RELEASE
		
	 
		
			1.2.0
			5.1.39
		
	 
		
			
			
				org.springframework.boot
				spring-boot-starter-freemarker
			
	 
			
			
				org.springframework.boot
				spring-boot-starter-web
			
	 
			
			
				org.springframework.boot
				spring-boot-starter-test
				test
			
	 
			
			
				org.mybatis.spring.boot
				mybatis-spring-boot-starter
				${mybatis-spring-boot}
			
	 
			
			
				mysql
				mysql-connector-java
				${mysql-connector}
			
	 
			
			
				junit
				junit
				4.12 
			
		
	
        我们在pom.xml中增加Spring Boot FreeMarker依赖
        
    2.配置FreeMarker
        然后在application.properties中加入FreeMarker相关配置:
## Freemarker 配置
## 文件配置路径
spring.freemarker.template-loader-path=classpath:/web/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
    3.展示层Controller详解
            
	/**
		 * @author WJ
		 * @date 2018/04/26
		 */
		@Controller
		public class CityController {

			@Autowired
			private CityService cityService;

			@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
			public String findOne(Model model, @PathVariable("id") Long id){
				model.addAttribute("city" , cityService.findOne(id));
				return "city";
			}


			@RequestMapping(value = "/api/city", method = RequestMethod.GET)
			public String findAll(Model model){
				List cityList = cityService.findAll();
				model.addAttribute("cityList", cityList);
				return "cityList";
			}
		}
        a.这里不是走HTTP+JSON模式,使用了@Controller而不是先前的@RestController
        b.方法返回值是String类型,和.application.properties配置的Freemarker文件
配置路径下的各个*.ftl文件名一致,这样才会准确的把数据渲染到ftl文件里面进
行展示
        c.用Model类,向Model加入数据,并指定在该数据在Freemarker取值指定的名称。


三.小结

    再次感谢泥瓦匠 BySocket


        


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