一、环境

1.1、Idea 2020.1

1.2、JDK 1.8

二、目的

spring boot 整合freemarker模板开发web项目

三、步骤

3.1、点击File -> New Project -> Spring Initializer,点击next

 

3.2、在对应地方修改自己的项目信息

3.3、选择Web依赖,选中Spring Web。可以选择Spring Boot版本,本次默认为2.2.6,点击Next

3.4、项目结构

四、添加文件

 
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.ouyushan</groupId>
<artifactId>spring-boot-web-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-web-freemarker</name>
<description>Freemarker project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
配置默认application.properties
# 设定ftl文件路径
spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.suffix=.ftl #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=false
#spring.freemarker.expose-session-attributes=false
#spring.freemarker.request-context-attribute=request
实现WelcomController
package org.ouyushan.springboot.web.freemarker.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.Date;
import java.util.Map; /**
* @Description:
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/30 11:35
*/
@Controller
public class WelComeController { @Value("${application.message:Default Value Hello World}")
private String message; @GetMapping("/")
public String error(Map<String,Object> model){
model.put("index",this.message);
return "index";
} @GetMapping("/hi")
public String welcome(Model model){
model.addAttribute("time",new Date());
model.addAttribute("message",this.message);
return "hello";
}
}
添加页面文件
templates/hello.ftl
<!DOCTYPE html>

<html lang="en">

<body>
Date: ${time ? date}
<br>
Time: ${time ? time}
<br>
Message: ${message}
</body> </html>
templates/index.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is index ${index}
</body>
</html>

五、接口测试

访问:
http://localhost:8080/
 

访问:
http://localhost:8080/hi

六、知识点

必须配置以下内容,否则报 no mapping found for ** 错误
spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.suffix=.ftl

Spring boot Sample 0010之spring-boot-web-freemarker的更多相关文章

  1. Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...

  2. [转]Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    Spring Boot——2分钟构建spring web mvc REST风格HelloWorld http://projects.spring.io/spring-boot/ http://spri ...

  3. [转]通过Spring Boot三分钟创建Spring Web项目

    来源:https://www.tianmaying.com/tutorial/project-based-on-spring-boot Spring Boot简介 接下来我们所有的Spring代码实例 ...

  4. 跟我学Spring Boot(三)Spring Boot 的web开发

    1.Web开发中至关重要的一部分,Web开发的核心内容主要包括内嵌Servlet容器和SpringMVC spring boot  提供了spring-boot-starter-web 为web开发提 ...

  5. 一、spring boot 1.5.4入门(web+freemarker)

    1.配置maven文件pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmln ...

  6. spring boot:创建一个简单的web(maven web project)

    1.新建一个maven web project; 2.在pom.xml文件中添加相应的依赖包: 3.新建一个HelloController请求控制类: 4.编写index.jsp页面: 5.编写启动类 ...

  7. 46. Spring Boot中使用AOP统一处理Web请求日志

    在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...

  8. Spring Boot-初学01 -使用Spring Initializer快速创建Spring Boot项目 -@RestController+spEL -实现简单SpringBoot的Web页面

    1.IDEA:使用 Spring Initializer快速创建项目 IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目: 选择我们需要的模块:向导会联网创建Spring ...

  9. Spring boot Sample 012之spring-boot-web-upload

    一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合web实现文件上传下载 三.步骤 3.1.点击File -> New Project -& ...

随机推荐

  1. win10卸载多余应用-通过命令行卸载

    Win10 APP 卸载方式和明细,网络最全.直接包含了编辑好的文件,可以打开编辑或直接删除,部份APP可卸载,但注释了,各位可以看情况是否删除.直接把代码复制保存扩展名为“ps1”即可直接运行,这是 ...

  2. 12_JavaScript基础入门(2)

    运算符 运算符(Operators,也翻译为操作符),是发起运算的最简单形式. 运算符的分类见仁见智,我们的课程对运算符进行如下分类: 数学运算符(Arithmetic operators)      ...

  3. Facebook 开源微光效果 Shimmer

    我的引言 晚上好,我是吴小龙同学,我的公众号「一分钟 GitHub」会推荐 GitHub 上好玩的项目,挖掘开源的价值,欢迎关注我. 今天要推荐的是 Facebook 开源的闪光效果:Shimmer, ...

  4. shell bash终端中输出的颜色和格式详解(超详细)

    文章目录 1) 格式 1.1 Set 1.2 Reset 2)8/16 Colors 2.1 前景(文字) 2.2 背景 3)88/256颜色 3.1 前景(文字) 3.2 背景色 4)组合属性 5) ...

  5. C# 数据操作系列 - 3. ADO.NET 离线查询

    0. 前言 在上一篇中,我故意留下了查询的示范没讲.虽然说可以通过以下代码获取一个DataReader: IDataReader reader = command.ExecuteReader(); 然 ...

  6. Codeforces Round #643 (Div.2)

    前言:这套cf我感觉出的很不错,AB就不说了,唯一有点欠缺的就是C和D的位置应该换一下,C出的挺不错,反正我当时没有想出来(赛后补题的时候其实也不难..听朋友说还可以FFT优化,然而我是个图论手并不会 ...

  7. 【题解】poj 3254 玉米田

    假如我们知道第i-1行的有x种放法,那么对于第i行的每一种放法都有x种,所以定义dp[i][j]表示第i行状态为j时的方法数,有转移方程:dp[i][j]=sum(dp[i-1][k]) k表示i-1 ...

  8. ql的python学习之路-day6

    字节编码: 这一节主要学习的是各种编码模式的相互转换,另外插两句话,今天的心情不是特别好,又没控制好自己的情绪,以后要心存阳光,好好的对待生活和身边的人. 废话不多说了直接贴码: #!/usr/bin ...

  9. Arthas 使用(二) —— 应用场景

    1. ognl获取bean SpringContextUtil,通常代码中会有类似这样的工具类用来获取 bean 实例 @Component public class SpringContextUti ...

  10. django 两种创建模型实例的方法

    1. 添加一个classmethod from django.db import models class Book(models.Model): title = models.CharField(m ...