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

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

Spring boot application whitelabel error page

问题

以下是翻译好的部分:

我在使用我的Spring Boot应用程序时遇到了白标签错误页面404。我正在硬编码一个学生列表,应该按照名字的方式显示在页面上。我尝试了不同的模板方法,但似乎没有任何效果。我还尝试了使用/*端点,但也没有任何效果。我找不到任何解决这个问题的相关问题。以下是不同的类;

控制器(Controller):

  1. public class StudentController {
  2. private List<Student> studentList = new ArrayList<>();
  3. @GetMapping("/hello")
  4. public String friendListing(Model model) {
  5. Student student1 = new Student("Kate", "Cole");
  6. studentList.add(student1);
  7. Student student2 = new Student("Dan", "Brown");
  8. studentList.add(student2);
  9. Student student3 = new Student("Mike", "Mars");
  10. studentList.add(student3);
  11. model.addAttribute("studentList", studentList);
  12. return "hello";
  13. }
  14. }

模型(Model):

  1. public class Student {
  2. private String firstName;
  3. private String lastName;
  4. public Student(String fn, String ln) {
  5. this.firstName = fn;
  6. this.lastName = ln;
  7. }
  8. public String getFirstName() {
  9. return firstName;
  10. }
  11. public void setFirstName(String firstName) {
  12. this.firstName = firstName;
  13. }
  14. public String getLastName() {
  15. return lastName;
  16. }
  17. public void setLastName(String lastName) {
  18. this.lastName = lastName;
  19. }
  20. @Override
  21. public String toString() {
  22. return firstName + " " + lastName;
  23. }
  24. }

视图(View):

  1. <!DOCTYPE HTML>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>使用Spring Boot进行服务器编程</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. </head>
  7. <body>
  8. <tr th:each="student: ${studentList}">
  9. <td th:text="${student.firstName}"></td>
  10. </tr>
  11. </body>
  12. </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;

  1. public class StudentController {
  2. private List&lt;Student&gt; studentList = new ArrayList&lt;&gt;();
  3. @GetMapping(&quot;/hello&quot;)
  4. public String friendListing(Model model) {
  5. Student student1 = new Student(&quot;Kate&quot;, &quot;Cole&quot;);
  6. studentList.add(student1);
  7. Student student2 = new Student(&quot;Dan&quot;, &quot;Brown&quot;);
  8. studentList.add(student2);
  9. Student student3 = new Student(&quot;Mike&quot;, &quot;Mars&quot;);
  10. studentList.add(student3);
  11. model.addAttribute(&quot;studentList&quot;, studentList);
  12. return &quot;hello&quot;;
  13. }}

Model;

  1. public class Student {
  2. private String firstName;
  3. private String lastName;
  4. public Student(String fn, String ln) {
  5. this.firstName = fn;
  6. this.lastName = ln;
  7. }
  8. public String getFirstName() {
  9. return firstName;
  10. }
  11. public void setFirstName(String firstName) {
  12. this.firstName = firstName;
  13. }
  14. public String getLastName() {
  15. return lastName;
  16. }
  17. public void setLastName(String lastName) {
  18. this.lastName = lastName;
  19. }
  20. @Override
  21. public String toString() {
  22. return firstName + &quot; &quot; + lastName;
  23. }}

And view;

  1. &lt;!DOCTYPE HTML&gt;
  2. &lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;Server Programming with Spring Boot&lt;/title&gt;
  5. &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
  6. &lt;/head&gt;
  7. &lt;body&gt;
  8. &lt;tr th:each=&quot;student: ${studentList}&quot;&gt;
  9. &lt;td th:text=&quot;${student.firstName}&quot;&gt;&lt;/td&gt;
  10. &lt;/tr&gt;
  11. &lt;/body&gt;
  12. &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:

确定