创建项目
创建一个普通 Maven Java项目,项目结构如下图
引入依赖
pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> </parent>
<groupId>org.example</groupId> <artifactId>shsxt-springboot-freemarker-quickstart</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging>
<name>shsxt-springboot-freemarker-quickstart Maven Webapp</name> <url>http://www.example.com</url>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies>
<build> <finalName>shsxt-springboot-freemarker-quickstart</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
|
创建控制器
IndexController1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.example.controller;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping;
@Controller public class IndexController { @GetMapping("/") public String index(Model model){ model.addAttribute("msg", "Hello freemarker"); return "index"; } }
|
创建视图
index.ftlh1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello world</title> <#--引入方法一 --> <#-- <link rel="stylesheet" href="css/main.css">-->
<#--引入方法二 --> <#assign rootPath=springMacroRequestContext.contextPath /> <link rel="stylesheet" href="${rootPath}/css/main.css">
</head> <body> <h2>${msg}</h2>
<div> <img src="${rootPath}/images/hello.jpeg" alt="hello world"> </div>
<script src="${rootPath}/js/main.js"></script> </body> </html>
|
添加静态资源
Spring Boot 静态资源配置属性类为 ResourceProperties
(已废弃)
Sprin Boot 默认静态资源路径如下
- META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public/
静态资源路径可以通过配置修改
application.yml1 2 3 4 5 6 7 8
| spring: web: resources: static-locations: - classpath:/pubilc/ - classpath:/static/ - classpath:/os/
|
创建启动器
1 2 3 4 5 6 7 8 9 10 11
| package com.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class Starter { public static void main(String[] args) { SpringApplication.run(Starter.class); } }
|