在intellij上使用spring boot和html,我无法通过html将数据添加到我的数据库

问题描述 投票:0回答:1

我在intellij上创建了一个简单的结构,使用spring boot,java和html和mysql来创建数据库。我想要做的是从html填充格式并将数据保存在我的数据库中。 当我使用存储库运行 @springboot 测试时,数据将保存在我的数据库中。但是当我尝试从 html 保存数据时什么也没有发生。我从谷歌浏览器打开 html 文件。

下面我给出了控制器、实体、存储库、资源和 sql 模式。

@RestController
public class PersonController {

    @Autowired
    private PersonRepository personRepository;

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("person", new Person());
        model.addAttribute("people", personRepository.findAll());
        return "index";
    }

    @PostMapping("/addPerson")
    public String addPerson(@ModelAttribute Person person) {
        personRepository.save(person);
        return "redirect:/";
    }
}
@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    private String email;

    private LocalDate dob; // Date of Birth

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public LocalDate getDob() {
        return dob;
    }

    public void setDob(LocalDate dob) {
        this.dob = dob;
    }
}
public interface PersonRepository extends JpaRepository<Person, Long> { }
<!DOCTYPE html> <html xmlns:th="thymeleaf.org"> 
<head> <title>Person Management</title> 
</head> 
<body> <h1>Person Management</h1> <form th:action="@{/addPerson}" th:object="${person}" method="post"> 
<label>Name:</label> <input type="text" th:field="*{name}" /><br> 
<label>Email:</label> <input type="email" th:field="*{email}" /><br> 
<label>Date of Birth:</label> <input type="date" th:field="*{dob}" /><br> 
<button type="submit">Add Person</button> 
</form>
<h2>People List</h2> 
<table> <thead> 
<tr> <th>Name</th> 
<th>Email</th> 
<th>Date of Birth</th> 
</tr> 
</thead> 
<tbody> <tr th:each="person : ${people}"> <td th:text="${person.name}"></td> <td th:text="${person.email}"></td> 
<td th:text="${person.dob}"></td> </tr> </tbody> 
</table> 
</body> 
</html>
spring.application.name=demo # MySQL Database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/banking spring.datasource.username=root 
spring.datasource.password=Boprelen26! 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
# Hibernate settings for schema generation 
spring.jpa.hibernate.ddl-auto=update 
spring.jpa.show-sql=true
java html spring thymeleaf
1个回答
0
投票

添加以下依赖项并重试:

<dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-jasper</artifactId>
   <version>9.0.1</version>
</dependency>

另外将

@RestController
替换为
@Controller

请参阅以下网页..

https://websparrow.org/spring/jsp-page-file-is-not-rendering-in-spring-boot-application

© www.soinside.com 2019 - 2024. All rights reserved.