英文:
Spring boot application whitelabel error page
问题
以下是翻译好的部分:
我在使用我的Spring Boot应用程序时遇到了白标签错误页面404。我正在硬编码一个学生列表,应该按照名字的方式显示在页面上。我尝试了不同的模板方法,但似乎没有任何效果。我还尝试了使用/*端点,但也没有任何效果。我找不到任何解决这个问题的相关问题。以下是不同的类;
控制器(Controller):
public class StudentController {
    private List<Student> studentList = new ArrayList<>();
    @GetMapping("/hello")
    public String friendListing(Model model) {
        Student student1 = new Student("Kate", "Cole");
        studentList.add(student1);
        Student student2 = new Student("Dan", "Brown");
        studentList.add(student2);
        Student student3 = new Student("Mike", "Mars");
        studentList.add(student3);
        model.addAttribute("studentList", studentList);
        return "hello";
    }
}
模型(Model):
public class Student {
    
    private String firstName;
    private String lastName;
    
    public Student(String fn, String ln) {
        this.firstName = fn;
        this.lastName = ln;
    }
    
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    @Override
    public String toString() {
        return firstName + " " + lastName;
    }
}
视图(View):
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>使用Spring Boot进行服务器编程</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <tr th:each="student: ${studentList}">
            <td th:text="${student.firstName}"></td>
        </tr>
    </body>
</html>
英文:
I'm getting whitelabel error page 404 with my springboot application. I am hardcoding a list of students and they should pop as firstname basis to the page. I've tried different approaches with the template but nothing seems to work. I also tried to do /* endpoint but didn't have any effect aswell. I couldn't find any relevant questions for this problem that would fix my problems. These are the different classes;
Controller;
public class StudentController {
private List<Student> studentList = new ArrayList<>();
@GetMapping("/hello")
public String friendListing(Model model) {
	Student student1 = new Student("Kate", "Cole");
	studentList.add(student1);
	Student student2 = new Student("Dan", "Brown");
	studentList.add(student2);
	Student student3 = new Student("Mike", "Mars");
	studentList.add(student3);
	model.addAttribute("studentList", studentList);
	return "hello";
}}
Model;
public class Student {
private String firstName;
private String lastName;
public Student(String fn, String ln) {
	this.firstName = fn;
	this.lastName = ln;
}
public String getFirstName() {
	return firstName;
}
public void setFirstName(String firstName) {
	this.firstName = firstName;
}
public String getLastName() {
	return lastName;
}
public void setLastName(String lastName) {
	this.lastName = lastName;
}
@Override
public String toString() {
	return firstName + " " + lastName;
}}
And view;
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<title>Server Programming with Spring Boot</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>
	<body>
		<tr th:each="student: ${studentList}">
		<td  th:text="${student.firstName}"></td>
		</tr>
	</body>
</html>
答案1
得分: 0
你的主要应用程序类在哪里?
- 使用
@ComponentScan注解与你的控制器类包 - 控制器类应该有
@Controller注解。 
英文:
Where is your main application class?
- Use 
@ComponentScanannotation with you controller class package - Controller class should have 
@Controllerannotation. 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论