在Spring Boot中,要编写一个简单的网页而不使用HTML,可以使用Thymeleaf模板引擎。Thymeleaf是一个Java服务器端模板引擎,它允许开发者使用类似于HTML的标记语言来构建动态网页。

以下是一个简单的示例,展示了如何在Spring Boot中使用Thymeleaf来编写一个网页:

  1. 首先,在pom.xml文件中添加以下依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 创建一个Controller类,例如HomeController.java,其中包含一个处理请求的方法:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String home(Model model) {
        model.addAttribute("message", "Hello, World!");
        return "home";
    }
}
  1. 创建一个Thymeleaf模板文件,例如home.html,在src/main/resources/templates目录下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>

在这个例子中,我们使用Thymeleaf的语法将模型中的"message"属性绑定到h1标签上。

  1. 运行Spring Boot应用程序,并访问http://localhost:8080/,您将看到显示"Hello, World!"的网页。

这只是一个简单的示例,您可以根据您的需求在网页中添加更多的内容和功能。希望对您有所帮助

springboot 怎么写个简单的网页不使用html

原文地址: https://www.cveoy.top/t/topic/hWmW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录