Spring Boot 3.x 提供了强大的 Servlet Web 应用程序开发支持,使用 Spring MVC 可以轻松构建基于 Servlet 的 Web 应用程序。以下是 Spring Boot 3.x 使用 Spring MVC 开发 Web 应用程序的详细指南。
1. 创建 Spring Boot 项目
首先,通过 Spring Initializr 创建一个新的 Spring Boot 项目。选择以下依赖:
- Spring Web
- Thymeleaf (如果需要模板引擎)
- Spring Boot DevTools (可选,用于开发时热部署)
2. 配置项目
在项目的 pom.xml
或 build.gradle
文件中包含必要的依赖:
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- 其他必要的依赖 -->
</dependencies>
3. 创建控制器
创建一个控制器类来处理 HTTP 请求。
package com.example.demo.controller;
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", "Welcome to Spring Boot 3.x!");
return "home";
}
}
4. 创建视图
在 src/main/resources/templates
目录下创建一个名为 home.html
的 Thymeleaf 模板文件。
home.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
5. 配置应用程序属性
在 src/main/resources
目录下创建 application.properties
文件,配置应用程序属性。
application.properties:
spring.thymeleaf.cache=false
server.port=8080
6. 启动应用程序
创建一个主类,用于启动 Spring Boot 应用程序。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
7. 运行和测试
运行应用程序,打开浏览器访问 http://localhost:8080
,你应该会看到页面显示 “Welcome to Spring Boot 3.x!”。
8. 处理表单提交
创建一个表单页面和相应的控制器方法来处理表单提交。
form.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Form</title>
</head>
<body>
<form action="#" th:action="@{/submit}" th:object="${user}" method="post">
<label for="name">Name:</label>
<input type="text" id="name" th:field="*{name}"/>
<button type="submit">Submit</button>
</form>
</body>
</html>
User.java:
package com.example.demo.model;
public class User {
private String name;
// getters and setters
}
HomeController.java:
package com.example.demo.controller;
import com.example.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("user", new User());
return "form";
}
@PostMapping("/submit")
public String submit(@ModelAttribute User user, Model model) {
model.addAttribute("message", "Hello, " + user.getName() + "!");
return "home";
}
}
9. 静态资源处理
将静态资源(如 CSS、JS、图片)放置在 src/main/resources/static
目录下,Spring Boot 会自动为你处理这些静态资源。
示例:
在 src/main/resources/static/css
目录下创建一个 styles.css
文件:
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
然后在你的 Thymeleaf 模板中引用该 CSS 文件:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="/css/styles.css"/>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
10. 结论
通过以上步骤,你已经学会了如何使用 Spring Boot 3.x 和 Spring MVC 开发一个简单的 Servlet Web 应用程序。Spring Boot 3.x 提供了强大的自动配置功能,使得 Web 应用程序开发变得更加简单高效。根据需要,你可以进一步扩展和定制你的应用程序,包括集成数据库、实现安全认证、添加 RESTful API 等。