一、环境

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. 第 4 篇:用类视图实现首页 API

    作者:HelloGitHub-追梦人物 文中所涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 django-rest-framework 类视图拓展自 django 的类视图,只 ...

  2. 如何发挥Visual Studio 2019强大的编辑功能轻松编辑Keil项目

    本文地址:https://www.cnblogs.com/jqdy/p/12565161.html 习惯了VS的强大编辑功能,对Keil 5越来越深恶痛绝.查阅网络文章后按图索骥初步实现了VS编辑Ke ...

  3. Day_14【IO流】扩展案例1_缓冲区字符输入、输出流,键盘录入,contains()方法的统一运用

    分析以下需求,并用代码实现 实现一个验证码小程序,要求如下: 1. 在项目根目录下新建一个文件:data.txt,键盘录入3个字符串验证码,并存入data.txt中,要求一个验证码占一行: 2. 键盘 ...

  4. 《ES6标准入门》读书笔记 第5章 - 正则增强

    第五章 - 正则增强 构造函数增强 允许覆写修饰符,如new RegExp(someRegex, 'ig') 字符串上的正则方法 原先match.replace等可以调用正则的方法在String的原型 ...

  5. 设计模式之GOF23原型模式02

    利用序列化和反序列化完成深复制 ByteArrayOutputStream bos=new ByteArrayOutputStream();  ObjectOutputStream oos=new O ...

  6. Semaphore和AQS

    Semaphore意思的信号量,它的作用是控制访问特定资源的线程数量 构造方法: public Semaphore(int permits) public Semaphore(int permits, ...

  7. select 标签的数据绑定

    修改数据的页面 进入页面绑定select的值 会value绑定但是没有显示相应的option <script> $("#id option[value=${item.decora ...

  8. Android 组件间通信--事件驱动

    在android中,组件间通信常用的方式: 1.使用广播机制:在主页面中监听特定的广播事件,进行业务逻辑的操作,其他页面只需要根据需求发送广播即可 例如:常用app结构中,左边通常为菜单栏,点击菜单栏 ...

  9. ESXI 6.5利用Centos7重置root密码

    ESXI6.5宿主机,很久没有登录,再次登录的时候,发现忘记root密码了 1.先将刻录一个CentOS7的启动光盘或U盘,并将服务器的启动项修改为光盘 2.保存BIOS重启后,选择Troublesh ...

  10. ql的python学习之路-day3

    字典操作 特性: 1.无序的 2.key是唯一的 , ,,], ,,], ,,], }, ,,], 'bbb' : ['a', 'b', 'c'], }}