创建名为“studentController”的 bean 时出错

发布于 2025-01-13 16:58:30 字数 5731 浏览 5 评论 0原文

我正在使用 mysql 和 jdbc 在 spring boot 中开发一个项目。我只是尝试注册选项,然后显示所有学生页面。我在这里到处查看来解决我的问题,但无论我做什么,我总是得到同样的错误:

UnsatisfiedDependencyExepption Error creating bean with name 'studentController'

这是我的代码:

学生模型:

@Component("student")
public class Student {

    public Student() {}

    private String userName;
    private String studentName;
    private String password;
    private String city;
    private String location;
    private String phoneNum;
    private String gender;
    
    // getter and setters and constructors
    
 }

学生控制器:

@RestController
@RequestMapping("/students")
public class StudentController {

    ArrayList<Student> studentList;
    Student student;

    @Autowired
    private StudentRepository studentRepo;

    public StudentController(StudentRepository studentRepo) {
        this.studentRepo = studentRepo;
    }

    @GetMapping("/signup")
    public String showSignUpPage(Student student){
        return "add-student";
    }

    @GetMapping("/addStudent")
    public String addStudent(@Valid Student student, BindingResult result) throws Exception {
        if(result.hasErrors())
            return "add-student";

        // testing
        student.setStudentName("Harry Potter");
        student.setPassword("1234");

        studentRepo.addStudent(student);
        return "redirect:/index";
    }

    @GetMapping("/index")
    public String showUserList() throws Exception {
        studentList = studentRepo.getStudents();
        return "index";
    }



}

学生存储库:

@Repository
public class StudentRepository  {

    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;
    private DbConnection dbCon;

    public StudentRepository() throws Exception {
        dbCon = new DbConnection();
    }
    
    //functions that go to the DB, using JCBC and mysql
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:switch="${studentList}">
    <h2 th:case="null">No students found</h2>
    <div th:case="*">
        <h2>Students</h2>
        <table>
            <thead>
            <tr>
                <th>Username</th>
                <th>Student Name</th>
                <th>City</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="student : ${studentList}">
                <td th:text="${student.username}"></td>
                <td th:text="${student.studentName}"></td>
                <td th:text="${student.city}"></td>
            </tr>
            </tbody>
        </table>
    </div>
    <p><a href="/signup">Add a new Student</a></p>
</div>
</body>
</html>

add-student.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="#" th:action="@{/addStudent}" th:object="${student}" method="post">
    <label for="name">Username</label>
    <input type="text" th:field="*{username}" id="name" placeholder="username">
    <span th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></span>
    <input type="submit" value="Add Student">
</form>
</body>
</html>

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-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

I'm working on a project in spring boot using mysql and jdbc. I just trying to to a signup option and then displaying all students page. I looked on everywhere here to solve my problem but no matter what I do, I always get the same error:

UnsatisfiedDependencyExepption Error creating bean with name 'studentController'

here is my code:

student model:

@Component("student")
public class Student {

    public Student() {}

    private String userName;
    private String studentName;
    private String password;
    private String city;
    private String location;
    private String phoneNum;
    private String gender;
    
    // getter and setters and constructors
    
 }

student controller:

@RestController
@RequestMapping("/students")
public class StudentController {

    ArrayList<Student> studentList;
    Student student;

    @Autowired
    private StudentRepository studentRepo;

    public StudentController(StudentRepository studentRepo) {
        this.studentRepo = studentRepo;
    }

    @GetMapping("/signup")
    public String showSignUpPage(Student student){
        return "add-student";
    }

    @GetMapping("/addStudent")
    public String addStudent(@Valid Student student, BindingResult result) throws Exception {
        if(result.hasErrors())
            return "add-student";

        // testing
        student.setStudentName("Harry Potter");
        student.setPassword("1234");

        studentRepo.addStudent(student);
        return "redirect:/index";
    }

    @GetMapping("/index")
    public String showUserList() throws Exception {
        studentList = studentRepo.getStudents();
        return "index";
    }



}

Student Repository:

@Repository
public class StudentRepository  {

    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;
    private DbConnection dbCon;

    public StudentRepository() throws Exception {
        dbCon = new DbConnection();
    }
    
    //functions that go to the DB, using JCBC and mysql
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:switch="${studentList}">
    <h2 th:case="null">No students found</h2>
    <div th:case="*">
        <h2>Students</h2>
        <table>
            <thead>
            <tr>
                <th>Username</th>
                <th>Student Name</th>
                <th>City</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="student : ${studentList}">
                <td th:text="${student.username}"></td>
                <td th:text="${student.studentName}"></td>
                <td th:text="${student.city}"></td>
            </tr>
            </tbody>
        </table>
    </div>
    <p><a href="/signup">Add a new Student</a></p>
</div>
</body>
</html>

add-student.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="#" th:action="@{/addStudent}" th:object="${student}" method="post">
    <label for="name">Username</label>
    <input type="text" th:field="*{username}" id="name" placeholder="username">
    <span th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></span>
    <input type="submit" value="Add Student">
</form>
</body>
</html>

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-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文