idea + Spring Boot + freemarker 一学就会

1.idea2018,1版本-file-new-project

2.

3.

4.配置pom文件



    4.0.0

    com.King
    testSpringBoot
    1.0-SNAPSHOT

    
        
            org.springframework.boot
            spring-boot
            2.0.1.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-web
            2.0.1.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-freemarker
            2.0.1.RELEASE
        

        
            org.springframework.boot
            spring-boot-starter-test
            2.0.1.RELEASE
            test
        
    


5.在resources目录下面分别添加 webapp目录 和application.yml文件

server:
  port: 10001

spring:
  profiles: default
  freemarker:
    template-loader-path: classpath:/webapp/WEB-INF/ftl/
    cache: true
    check-template-location: true
    content-type: text/html; charset=UTF-8
    expose-request-attributes: true
    expose-session-attributes: true
    request-context-attribute: request
    suffix: .ftl

6.建立包目录,并建立相应的类

package com.king.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * create by wu  2018/5/29
 */
@Controller
@RequestMapping("/index/")
public class IndexController {
    @RequestMapping(value = "test/")
    public String test(Model model){
        String s= "this is from Server";
        model.addAttribute("str",s);
        return "index";
    }
}

7.在web目录下 新建springboot启动类

package com.king.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * create by wu  2018/5/29
 */
@SpringBootApplication
public class WebIndexApplication {
    public static void main(String[] args){
        SpringApplication.run(WebIndexApplication.class,args);
    }
}

8.在webapp目录下新建WEB-INF 目录和子目录ftl  在ftl目录下新建 index.ftl文件




    
    Title


${str}

9.启动springboot应用

10.在浏览器中输入localhost:10001/index/test/  查看效果

11.最终文件目录结构

成功之后记得点赞!

 

 

你可能感兴趣的:(idea + Spring Boot + freemarker 一学就会)