idea + Spring Boot + freemarker 一学就会

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

2.

3.

4.配置pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.King</groupId>
    <artifactId>testSpringBoot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.0.1.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

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文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    ${str}
</div>
</body>
</html>

9.启动springboot应用

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

11.最终文件目录结构

成功之后记得点赞!

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值