springboot html form表单
时间: 2023-11-16 07:53:29 浏览: 112
在Spring Boot中使用HTML表单需要进行以下步骤:
1. 在pom.xml文件中添加Thymeleaf依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 在application.properties文件中配置Thymeleaf模板引擎:
```properties
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
```
3. 在src/main/resources/templates目录下创建HTML模板文件,例如form.html:
```html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Form</title>
</head>
<body>
<h1>Form</h1>
<form th:action="@{/submit}" method="post">
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<button type="submit">Submit</button>
</form>
</body>
</html>
```
4. 创建Controller处理表单提交:
```java
@Controller
public class FormController {
@GetMapping("/form")
public String showForm() {
return "form";
}
@PostMapping("/submit")
public String submitForm(@RequestParam String username, @RequestParam String password) {
// 处理表单提交逻辑
return "success"; // 返回成功页面
}
}
```
现在你可以通过访问http://localhost:8080/form来展示表单,提交后会调用submitForm方法进行处理,可以在方法中添加相应的业务逻辑。
阅读全文
相关推荐


















