Spring Boot应用程序白标签错误页面

huangapple go评论127阅读模式
英文:

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&lt;Student&gt; studentList = new ArrayList&lt;&gt;();

@GetMapping(&quot;/hello&quot;)
public String friendListing(Model model) {
	Student student1 = new Student(&quot;Kate&quot;, &quot;Cole&quot;);
	studentList.add(student1);
	Student student2 = new Student(&quot;Dan&quot;, &quot;Brown&quot;);
	studentList.add(student2);
	Student student3 = new Student(&quot;Mike&quot;, &quot;Mars&quot;);
	studentList.add(student3);
	model.addAttribute(&quot;studentList&quot;, studentList);
	return &quot;hello&quot;;
}}

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 + &quot; &quot; + lastName;
}}

And view;

&lt;!DOCTYPE HTML&gt;
&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
	&lt;head&gt;
		&lt;title&gt;Server Programming with Spring Boot&lt;/title&gt;
		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;tr th:each=&quot;student: ${studentList}&quot;&gt;
		&lt;td  th:text=&quot;${student.firstName}&quot;&gt;&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 0

你的主要应用程序类在哪里?

  • 使用@ComponentScan注解与你的控制器类包
  • 控制器类应该有@Controller注解。
英文:

Where is your main application class?

  • Use @ComponentScan annotation with you controller class package
  • Controller class should have @Controller annotation.

huangapple
  • 本文由 发表于 2020年9月16日 20:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63919728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定