创建名为“studentController”的 bean 时出错
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论