Spring Boot Mustache
最后修改于 2023 年 7 月 28 日
在本文中,我们使用Mustache模板引擎创建一个简单的Spring Boot Web应用程序。
Mustache
Mustache是一个简单的Web模板系统。它适用于许多编程语言,包括Java。Mustache被描述为无逻辑的,因为它没有任何显式的控制流语句,例如if和else条件语句或for循环。可以使用节标签处理列表和lambda表达式来实现循环和条件评估。
Spring Boot Mustache示例
以下示例是一个使用Mustache模板引擎的Spring Boot Web应用程序。
build.gradle
...
src
├── main
│ ├── java
│ │ └── com
│ │ └── zetcode
│ │ ├── Application.java
│ │ └── controller
│ │ └── HelloController.java
│ └── resources
│ └── templates
│ └── hello.mustache
└── test
├── java
│ └── com
│ └── zetcode
│ └── controller
│ └── HelloControllerTest.java
└── resources
这是项目结构。模板文件具有.mustache后缀;它们默认位于src/main/resources/templates目录中。当Spring Boot在Gradle构建文件中找到依赖项时,它会自动配置Mustache。
plugins {
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}
group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-mustache'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
这是Gradle构建文件。spring-boot-starter-mustache是使用Mustache构建Spring MVC应用程序的启动器。
package com.zetcode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping(value = "/hello")
public String hello(Model model) {
var msg = "Hello there!";
model.addAttribute("message", msg);
return "hello";
}
}
我们有一个简单的HelloController,我们将/hello URL路径映射到hello.mustache视图。我们在模型中添加一个短文本属性,以便在视图中呈现。
return "hello";
返回值是要呈现的视图的名称(不带后缀)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
{{ message }}
</body>
</html>
hello.mustache是一个Mustache模板文件,其中包含要用模型中的数据填充的占位符。Mustache使用{{ }}语法。
package com.zetcode.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
@SpringBootTest
@AutoConfigureMockMvc
class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHome() throws Exception {
var expected = "Hello there!";
this.mockMvc.perform(get("/hello"))
.andExpect(content().string(containsString(expected)));
}
}
这是一个HelloControllerTest,它检查响应中是否存在hello消息。
package com.zetcode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我们设置Spring Boot应用程序。 @SpringBootApplication 注解启用自动配置和组件扫描。
$ ./gradlew bootRun
我们运行应用程序并导航到localhost:8080/hello。
在本文中,我们使用Mustache创建了一个Spring Boot Web应用程序。