首页 > 解决方案 > 阅读 Spring Boot in Action 一书

问题描述

我遇到了白标错误页面的问题,我不知道如何解决这个问题。

我编写了本书中的所有代码:


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import newmind.entity.Book;
import newmind.entity.ReadingListRepository;

import java.util.List;

@Controller
@RequestMapping("/readingList")
public class ReadingListController {
    
    private ReadingListRepository readingListRepository;
    
    
    @Autowired
    public ReadingListController(
            ReadingListRepository readingListRepository) {
        this.readingListRepository = readingListRepository;
    }
    
    @RequestMapping(value="/{reader}", method=RequestMethod.GET)
    public String readersBooks(@PathVariable ("reader") String reader, Model model) {
        List<Book> readingList = readingListRepository.findByReader(reader);
        if (readingList != null) {
            model.addAttribute("books", readingList);
        }
        return "readingList";
    }

    @RequestMapping(value="/{reader}", method=RequestMethod.POST)
    public String addToReadingList(@PathVariable("reader") String reader, Book book) {
        book.setReader(reader);
        readingListRepository.save(book);
        return "redirect:/{reader}";
    }
}

和 spring boot bootstrap 应用程序:

package newmind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages= {"newmind"})
public class ReadingListApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReadingListApplication.class, args);
    }
}

这是域类:

package newmind.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String reader;
    private String isbn;
    private String author;
    private String description;
    public Long getId() {
        return id;
    }
    public String getReader() {
        return reader;
    }
    public String getIsbn() {
        return isbn;
    }
    public String getAuthor() {
        return author;
    }
    public String getDescription() {
        return description;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public void setReader(String reader) {
        this.reader = reader;
    }
    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

和存储库:

package newmind.entity;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ReadingListRepository extends JpaRepository<Book, Long> {
    
    List<Book> findByReader(String reader);
}

我在 src/main/resources 目录中创建错误 html 页面和主页 html 页面:

这是error.html

<!DOCTYPE html>
<html>
<body>
    Something went wrong: {{status}} {{error}}
</body>
</html>

和主页html页面:

<!DOCTYPE html>
<html>
    <head>
        <title>Reading List</title>
        <style>
        body {
            background-color: #cccccc;
            font-family: arial,helvetica,sans-serif;
        }
        </style>
    </head>
    
    <body>
        <h2>Your Reading List</h2>
        <div th:unless="${#lists.isEmpty(books) }">
        </div>
    </body>
</html>

现在,当我运行程序以侦听端口 8080 时,它向我显示:出了点问题:{{status}} {{error}}

你能帮我解决这个问题吗,因为我在家学习(书)。感谢一切。并且请不要报告此帖子,因为没有人可以解决此问题。

标签: javaspringspring-boothibernatespring-mvc

解决方案


推荐阅读