SpringBoot2整合Freemarker模板

目录

 

pom.xml代码

application.yml配置文件代码

Controller控制器代码

.ftl前端页面代码

SpringBoot的三种启动方式


pom.xml代码


    org.springframework.boot
    spring-boot-starter-parent
    2.0.0.RELEASE


 
    
        junit
        junit
        4.11
        test
    

    
        org.springframework.boot
        spring-boot-starter-web
        2.0.0.RELEASE
        compile
    
    
        org.springframework.boot
        spring-boot-starter
        2.0.1.RELEASE
        compile
    

    
    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    

    
    
        org.yaml
        snakeyaml
        1.10
    

    
    
        org.springframework.boot
        spring-boot-starter-freemarker
    

application.yml配置文件代码

spring:
  http:
    encoding:
      force: true
      ###模板引擎编码UTF-8
      charset: UTF-8
  freemarker:
    allow-request-override: false
    cache: false
    check-template-location: true
    charset: UTF-8
    content-type: text/html;charset=UTF-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    ###模板文件以.ftl
    suffix: .ftl
    ###模板目录
    template-loader-path: classpath:/templates

Controller控制器代码

package com.xyt.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;

@Controller
public class IndexController {

    @RequestMapping("/index")
    @ResponseBody
    public String index() {
        return "努力学习天天向上";
    }


    @RequestMapping("/freemarkerIndex")
    public String freemarkerIndex(Map resultMap) {
        resultMap.put("name", "SpringBoot2.0");
        resultMap.put("sex", "1");
        ArrayList objects = new ArrayList<>();
        objects.add("SpringBoot1.0");
        objects.add("SpringBoot2.0");
        resultMap.put("objects", objects);
        return "freemarkerIndex";
    }
} 
  

.ftl前端页面代码




    
    首页


${name}
<#if sex=="1">
    男
<#elseif sex=="2">
    女
<#else>
    其他


<#list objects as user>
    ${user}


SpringBoot的三种启动方式

  1. SpringBoot第一种启动方式

    Springboot默认端口号为8080

    
    @RestController
    
    @EnableAutoConfiguration
    
    public class HelloController {
    
    @RequestMapping("/hello")
    
    public String index() {
    
    return "Hello World";
    
    }
    
    public static void main(String[] args) {
    
    SpringApplication.run(HelloController.class, args);
    
    }
    
    }

     

  2. SpringBoot第二种启动方式

    @ComponentScan(basePackages = "com.itmayiedu.controller")---控制器扫包范围

    
    @ComponentScan(basePackages = "com.itmayiedu.controller")
    
    @EnableAutoConfiguration
    
    public class App {
    
    public static void main(String[] args) {
    
    SpringApplication.run(App.class, args);
    
    }
    
    }

     
  3. SpringBoot的第三种启动方式 

@SpringBootApplication

@SpringBootApplication 被 @Configuration、@EnableAutoConfiguration、@ComponentScan 注解所修饰,换言之 Springboot 提供了统一的注解来替代以上三个注解

扫包范围:在启动类上加上@SpringBootApplication注解,当前包下或者子包下所有的类都可以扫到。

package com.xyt.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
public class AppSpringBoot {
    public static void main(String[] args) {
        SpringApplication.run(AppSpringBoot.class, args);
    }
}

最后结果

SpringBoot2整合Freemarker模板_第1张图片

你可能感兴趣的:(Java,springBoot,Freemarker)